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('''