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 <paulberry@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index 693953a..3543c8e 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -9572,10 +9572,11 @@
}
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 d43701a..0861f94 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/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 @@
''');
}
+ test_type_param_generic_function_type_nullability_legacy() async {
+ featureSet = disableNnbd;
+ var library = await checkLibrary('''
+T f<T>(T t) {}
+var g = f;
+''');
+ checkElementText(library, '''
+T Function<T>(T) g;
+T f<T>(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 t) {}
+var g = f;
+''');
+ checkElementText(library, '''
+T Function<T>(T) g;
+T f<T>(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('''