Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 582727a

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Tests for TypeName resolution, fixes for NNBD opt-in/out mixes.
Change-Id: Ifb119a266306dbdf8a712d3110038179ee3cd7f8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132965 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 3679239 commit 582727a

File tree

7 files changed

+488
-23
lines changed

7 files changed

+488
-23
lines changed

pkg/analyzer/lib/src/dart/element/type_algebra.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ abstract class _TypeSubstitutor extends DartTypeVisitor<DartType> {
524524
}
525525

526526
return NamedTypeBuilder(
527-
type.isNNBD,
527+
type.typeSystem,
528528
type.element,
529529
arguments,
530530
type.nullabilitySuffix,

pkg/analyzer/lib/src/dart/resolver/type_name_resolver.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,12 @@ class TypeNameResolver {
325325
node,
326326
element.typeParameters.length,
327327
);
328-
return element.instantiate(
328+
var type = element.instantiate(
329329
typeArguments: typeArguments,
330330
nullabilitySuffix: nullability,
331331
);
332+
type = typeSystem.toLegacyType(type);
333+
return type;
332334
} else if (element is NeverElementImpl) {
333335
_buildTypeArguments(node, 0);
334336
return element.instantiate(

pkg/analyzer/lib/src/generated/type_system.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -724,17 +724,21 @@ class Dart2TypeSystem extends TypeSystem {
724724
if (classElement != null) {
725725
var typeParameters = classElement.typeParameters;
726726
var typeArguments = _defaultTypeArguments(typeParameters);
727-
return classElement.instantiate(
727+
var type = classElement.instantiate(
728728
typeArguments: typeArguments,
729729
nullabilitySuffix: nullabilitySuffix,
730730
);
731+
type = toLegacyType(type);
732+
return type;
731733
} else if (functionTypeAliasElement != null) {
732734
var typeParameters = functionTypeAliasElement.typeParameters;
733735
var typeArguments = _defaultTypeArguments(typeParameters);
734-
return functionTypeAliasElement.instantiate(
736+
var type = functionTypeAliasElement.instantiate(
735737
typeArguments: typeArguments,
736738
nullabilitySuffix: nullabilitySuffix,
737739
);
740+
type = toLegacyType(type);
741+
return type;
738742
} else {
739743
throw ArgumentError('Missing element');
740744
}
@@ -1744,6 +1748,11 @@ class Dart2TypeSystem extends TypeSystem {
17441748
.refineBinaryExpressionType(leftType, operator, rightType, currentType);
17451749
}
17461750

1751+
DartType toLegacyType(DartType type) {
1752+
if (isNonNullableByDefault) return type;
1753+
return NullabilityEliminator.perform(typeProvider, type);
1754+
}
1755+
17471756
/**
17481757
* Merges two types into a single type.
17491758
* Compute the canonical representation of [T].
@@ -1791,9 +1800,7 @@ class Dart2TypeSystem extends TypeSystem {
17911800
) {
17921801
return typeParameters.map((typeParameter) {
17931802
var typeParameterImpl = typeParameter as TypeParameterElementImpl;
1794-
var typeArgument = typeParameterImpl.defaultType;
1795-
typeArgument = _toLegacyType(typeArgument);
1796-
return typeArgument;
1803+
return typeParameterImpl.defaultType;
17971804
}).toList();
17981805
}
17991806

@@ -2291,11 +2298,6 @@ class Dart2TypeSystem extends TypeSystem {
22912298
return false;
22922299
}
22932300

2294-
DartType _toLegacyType(DartType type) {
2295-
if (isNonNullableByDefault) return type;
2296-
return NullabilityEliminator.perform(typeProvider, type);
2297-
}
2298-
22992301
DartType _typeParameterResolveToObjectBounds(DartType type) {
23002302
var element = type.element;
23012303
type = type.resolveToBound(typeProvider.objectType);

pkg/analyzer/lib/src/summary2/named_type_builder.dart

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/type.dart';
99
import 'package:analyzer/src/dart/element/element.dart';
1010
import 'package:analyzer/src/dart/element/type.dart';
1111
import 'package:analyzer/src/dart/element/type_algebra.dart';
12+
import 'package:analyzer/src/generated/type_system.dart';
1213
import 'package:analyzer/src/summary2/lazy_ast.dart';
1314
import 'package:analyzer/src/summary2/type_builder.dart';
1415
import 'package:meta/meta.dart';
@@ -17,8 +18,8 @@ import 'package:meta/meta.dart';
1718
class NamedTypeBuilder extends TypeBuilder {
1819
static DynamicTypeImpl get _dynamicType => DynamicTypeImpl.instance;
1920

20-
/// Indicates whether the library is opted into NNBD.
21-
final bool isNNBD;
21+
/// The type system of the library with the type name.
22+
final TypeSystemImpl typeSystem;
2223

2324
@override
2425
final Element element;
@@ -40,11 +41,11 @@ class NamedTypeBuilder extends TypeBuilder {
4041
DartType _type;
4142

4243
NamedTypeBuilder(
43-
this.isNNBD, this.element, this.arguments, this.nullabilitySuffix,
44+
this.typeSystem, this.element, this.arguments, this.nullabilitySuffix,
4445
{this.node});
4546

4647
factory NamedTypeBuilder.of(
47-
bool isNNBD,
48+
TypeSystemImpl typeSystem,
4849
TypeName node,
4950
Element element,
5051
NullabilitySuffix nullabilitySuffix,
@@ -57,7 +58,7 @@ class NamedTypeBuilder extends TypeBuilder {
5758
arguments = <DartType>[];
5859
}
5960

60-
return NamedTypeBuilder(isNNBD, element, arguments, nullabilitySuffix,
61+
return NamedTypeBuilder(typeSystem, element, arguments, nullabilitySuffix,
6162
node: node);
6263
}
6364

@@ -71,25 +72,29 @@ class NamedTypeBuilder extends TypeBuilder {
7172
if (element is ClassElement) {
7273
var parameters = element.typeParameters;
7374
var arguments = _buildArguments(parameters);
74-
_type = element.instantiate(
75+
var type = element.instantiate(
7576
typeArguments: arguments,
7677
nullabilitySuffix: nullabilitySuffix,
7778
);
79+
type = typeSystem.toLegacyType(type);
80+
_type = type;
7881
} else if (element is GenericTypeAliasElement) {
7982
var rawType = _getRawFunctionType(element);
8083
if (rawType is FunctionType) {
8184
var parameters = element.typeParameters;
8285
var arguments = _buildArguments(parameters);
8386
var substitution = Substitution.fromPairs(parameters, arguments);
8487
var instantiated = substitution.substituteType(rawType) as FunctionType;
85-
_type = FunctionTypeImpl(
88+
var type = FunctionTypeImpl(
8689
typeFormals: instantiated.typeFormals,
8790
parameters: instantiated.parameters,
8891
returnType: instantiated.returnType,
8992
nullabilitySuffix: nullabilitySuffix,
9093
element: element,
9194
typeArguments: arguments,
9295
);
96+
type = typeSystem.toLegacyType(type);
97+
_type = type;
9398
} else {
9499
_type = _dynamicType;
95100
}
@@ -126,7 +131,7 @@ class NamedTypeBuilder extends TypeBuilder {
126131
return this;
127132
}
128133

129-
return NamedTypeBuilder(isNNBD, element, arguments, nullabilitySuffix,
134+
return NamedTypeBuilder(typeSystem, element, arguments, nullabilitySuffix,
130135
node: node);
131136
}
132137

@@ -224,7 +229,7 @@ class NamedTypeBuilder extends TypeBuilder {
224229
NullabilitySuffix _getNullabilitySuffix(bool hasQuestion) {
225230
if (hasQuestion) {
226231
return NullabilitySuffix.question;
227-
} else if (isNNBD) {
232+
} else if (typeSystem.isNonNullableByDefault) {
228233
return NullabilitySuffix.none;
229234
} else {
230235
return NullabilitySuffix.star;

pkg/analyzer/lib/src/summary2/reference_resolver.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analyzer/src/dart/ast/ast.dart';
1111
import 'package:analyzer/src/dart/element/element.dart';
1212
import 'package:analyzer/src/dart/element/type.dart';
1313
import 'package:analyzer/src/dart/resolver/scope.dart';
14+
import 'package:analyzer/src/generated/type_system.dart';
1415
import 'package:analyzer/src/summary/idl.dart';
1516
import 'package:analyzer/src/summary2/function_type_builder.dart';
1617
import 'package:analyzer/src/summary2/lazy_ast.dart';
@@ -30,6 +31,7 @@ import 'package:analyzer/src/summary2/types_builder.dart';
3031
/// the type is set, otherwise we keep it empty, so we will attempt to infer
3132
/// it later).
3233
class ReferenceResolver extends ThrowingAstVisitor<void> {
34+
final TypeSystemImpl _typeSystem;
3335
final NodesToBuildType nodesToBuildType;
3436
final LinkedElementFactory elementFactory;
3537
final LibraryElement _libraryElement;
@@ -54,7 +56,8 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
5456
this.unitReference,
5557
this.isNNBD,
5658
this.scope,
57-
) : reference = unitReference;
59+
) : _typeSystem = _libraryElement.typeSystem,
60+
reference = unitReference;
5861

5962
@override
6063
void visitBlockFunctionBody(BlockFunctionBody node) {}
@@ -514,7 +517,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
514517
);
515518
} else {
516519
var builder = NamedTypeBuilder.of(
517-
isNNBD,
520+
_typeSystem,
518521
node,
519522
element,
520523
nullabilitySuffix,

pkg/analyzer/test/src/dart/resolution/test_all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import 'property_access_test.dart' as property_access;
4949
import 'simple_identifier_test.dart' as simple_identifier;
5050
import 'top_type_inference_test.dart' as top_type_inference;
5151
import 'type_inference/test_all.dart' as type_inference;
52+
import 'type_name_test.dart' as type_name;
5253

5354
main() {
5455
defineReflectiveSuite(() {
@@ -93,6 +94,7 @@ main() {
9394
property_access.main();
9495
simple_identifier.main();
9596
top_type_inference.main();
97+
type_name.main();
9698
type_inference.main();
9799
}, name: 'resolution');
98100
}

0 commit comments

Comments
 (0)