From f930d674ca43336aaa85a26b1a2cf4765a469f20 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Sat, 18 May 2019 15:05:26 +0000 Subject: [PATCH] Restore old nullability behavior of TypeParameterElement.type. It doesn't work to make TypeParameterElement.type choose nullability/non-nullability based on the enclosing library, because for type parameters of synthetic function types, there is no enclosing library. Change-Id: Ic74cc639534e3c03f3c46e7eacfb5655ca74019a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/102981 Auto-Submit: Paul Berry Commit-Queue: Brian Wilkerson Reviewed-by: Brian Wilkerson --- .../lib/src/dart/element/element.dart | 9 ++--- .../test/src/summary/resynthesize_common.dart | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 693953a4f42..3543c8ec192 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -9572,10 +9572,11 @@ class TypeParameterElementImpl extends ElementImpl } TypeParameterType get type { - return _type ??= new TypeParameterTypeImpl(this, - nullabilitySuffix: library.isNonNullableByDefault - ? NullabilitySuffix.none - : NullabilitySuffix.star); + // Note: TypeParameterElement.type has nullability suffix `star` regardless + // of whether it appears in a migrated library. This is because for type + // parameters of synthetic function types, the ancestor chain is broken and + // we can't find the enclosing library to tell whether it is migrated. + return _type ??= new TypeParameterTypeImpl(this); } void set type(TypeParameterType type) { diff --git a/pkg/analyzer/test/src/summary/resynthesize_common.dart b/pkg/analyzer/test/src/summary/resynthesize_common.dart index d43701a301e..0861f94e256 100644 --- a/pkg/analyzer/test/src/summary/resynthesize_common.dart +++ b/pkg/analyzer/test/src/summary/resynthesize_common.dart @@ -8,9 +8,11 @@ import 'package:analyzer/dart/analysis/declared_variables.dart'; import 'package:analyzer/dart/analysis/features.dart'; import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/file_system/file_system.dart'; import 'package:analyzer/src/context/context.dart'; import 'package:analyzer/src/dart/element/element.dart'; +import 'package:analyzer/src/dart/element/type.dart'; import 'package:analyzer/src/generated/source.dart'; import 'package:analyzer/src/summary/idl.dart'; import 'package:analyzer/src/summary/resynthesize.dart'; @@ -9438,6 +9440,40 @@ dynamic v; '''); } + test_type_param_generic_function_type_nullability_legacy() async { + featureSet = disableNnbd; + var library = await checkLibrary(''' +T f(T t) {} +var g = f; +'''); + checkElementText(library, ''' +T Function(T) g; +T f(T t) {} +'''); + var g = library.definingCompilationUnit.topLevelVariables[0]; + var t = (g.type as FunctionType).typeFormals[0]; + // TypeParameterElement.type has a nullability suffix of `star` regardless + // of whether it appears in a migrated library. + expect((t.type as TypeImpl).nullabilitySuffix, NullabilitySuffix.star); + } + + test_type_param_generic_function_type_nullability_migrated() async { + featureSet = enableNnbd; + var library = await checkLibrary(''' +T f(T t) {} +var g = f; +'''); + checkElementText(library, ''' +T Function(T) g; +T f(T t) {} +'''); + var g = library.definingCompilationUnit.topLevelVariables[0]; + var t = (g.type as FunctionType).typeFormals[0]; + // TypeParameterElement.type has a nullability suffix of `star` regardless + // of whether it appears in a migrated library. + expect((t.type as TypeImpl).nullabilitySuffix, NullabilitySuffix.star); + } + test_type_param_ref_nullability_none() async { featureSet = enableNnbd; var library = await checkLibrary('''