Skip to content

Commit

Permalink
Tests for TypeName resolution, fixes for NNBD opt-in/out mixes.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
scheglov authored and commit-bot@chromium.org committed Jan 23, 2020
1 parent 3679239 commit 582727a
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/element/type_algebra.dart
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ abstract class _TypeSubstitutor extends DartTypeVisitor<DartType> {
}

return NamedTypeBuilder(
type.isNNBD,
type.typeSystem,
type.element,
arguments,
type.nullabilitySuffix,
Expand Down
4 changes: 3 additions & 1 deletion pkg/analyzer/lib/src/dart/resolver/type_name_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,12 @@ class TypeNameResolver {
node,
element.typeParameters.length,
);
return element.instantiate(
var type = element.instantiate(
typeArguments: typeArguments,
nullabilitySuffix: nullability,
);
type = typeSystem.toLegacyType(type);
return type;
} else if (element is NeverElementImpl) {
_buildTypeArguments(node, 0);
return element.instantiate(
Expand Down
22 changes: 12 additions & 10 deletions pkg/analyzer/lib/src/generated/type_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -724,17 +724,21 @@ class Dart2TypeSystem extends TypeSystem {
if (classElement != null) {
var typeParameters = classElement.typeParameters;
var typeArguments = _defaultTypeArguments(typeParameters);
return classElement.instantiate(
var type = classElement.instantiate(
typeArguments: typeArguments,
nullabilitySuffix: nullabilitySuffix,
);
type = toLegacyType(type);
return type;
} else if (functionTypeAliasElement != null) {
var typeParameters = functionTypeAliasElement.typeParameters;
var typeArguments = _defaultTypeArguments(typeParameters);
return functionTypeAliasElement.instantiate(
var type = functionTypeAliasElement.instantiate(
typeArguments: typeArguments,
nullabilitySuffix: nullabilitySuffix,
);
type = toLegacyType(type);
return type;
} else {
throw ArgumentError('Missing element');
}
Expand Down Expand Up @@ -1744,6 +1748,11 @@ class Dart2TypeSystem extends TypeSystem {
.refineBinaryExpressionType(leftType, operator, rightType, currentType);
}

DartType toLegacyType(DartType type) {
if (isNonNullableByDefault) return type;
return NullabilityEliminator.perform(typeProvider, type);
}

/**
* Merges two types into a single type.
* Compute the canonical representation of [T].
Expand Down Expand Up @@ -1791,9 +1800,7 @@ class Dart2TypeSystem extends TypeSystem {
) {
return typeParameters.map((typeParameter) {
var typeParameterImpl = typeParameter as TypeParameterElementImpl;
var typeArgument = typeParameterImpl.defaultType;
typeArgument = _toLegacyType(typeArgument);
return typeArgument;
return typeParameterImpl.defaultType;
}).toList();
}

Expand Down Expand Up @@ -2291,11 +2298,6 @@ class Dart2TypeSystem extends TypeSystem {
return false;
}

DartType _toLegacyType(DartType type) {
if (isNonNullableByDefault) return type;
return NullabilityEliminator.perform(typeProvider, type);
}

DartType _typeParameterResolveToObjectBounds(DartType type) {
var element = type.element;
type = type.resolveToBound(typeProvider.objectType);
Expand Down
23 changes: 14 additions & 9 deletions pkg/analyzer/lib/src/summary2/named_type_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_algebra.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:analyzer/src/summary2/lazy_ast.dart';
import 'package:analyzer/src/summary2/type_builder.dart';
import 'package:meta/meta.dart';
Expand All @@ -17,8 +18,8 @@ import 'package:meta/meta.dart';
class NamedTypeBuilder extends TypeBuilder {
static DynamicTypeImpl get _dynamicType => DynamicTypeImpl.instance;

/// Indicates whether the library is opted into NNBD.
final bool isNNBD;
/// The type system of the library with the type name.
final TypeSystemImpl typeSystem;

@override
final Element element;
Expand All @@ -40,11 +41,11 @@ class NamedTypeBuilder extends TypeBuilder {
DartType _type;

NamedTypeBuilder(
this.isNNBD, this.element, this.arguments, this.nullabilitySuffix,
this.typeSystem, this.element, this.arguments, this.nullabilitySuffix,
{this.node});

factory NamedTypeBuilder.of(
bool isNNBD,
TypeSystemImpl typeSystem,
TypeName node,
Element element,
NullabilitySuffix nullabilitySuffix,
Expand All @@ -57,7 +58,7 @@ class NamedTypeBuilder extends TypeBuilder {
arguments = <DartType>[];
}

return NamedTypeBuilder(isNNBD, element, arguments, nullabilitySuffix,
return NamedTypeBuilder(typeSystem, element, arguments, nullabilitySuffix,
node: node);
}

Expand All @@ -71,25 +72,29 @@ class NamedTypeBuilder extends TypeBuilder {
if (element is ClassElement) {
var parameters = element.typeParameters;
var arguments = _buildArguments(parameters);
_type = element.instantiate(
var type = element.instantiate(
typeArguments: arguments,
nullabilitySuffix: nullabilitySuffix,
);
type = typeSystem.toLegacyType(type);
_type = type;
} else if (element is GenericTypeAliasElement) {
var rawType = _getRawFunctionType(element);
if (rawType is FunctionType) {
var parameters = element.typeParameters;
var arguments = _buildArguments(parameters);
var substitution = Substitution.fromPairs(parameters, arguments);
var instantiated = substitution.substituteType(rawType) as FunctionType;
_type = FunctionTypeImpl(
var type = FunctionTypeImpl(
typeFormals: instantiated.typeFormals,
parameters: instantiated.parameters,
returnType: instantiated.returnType,
nullabilitySuffix: nullabilitySuffix,
element: element,
typeArguments: arguments,
);
type = typeSystem.toLegacyType(type);
_type = type;
} else {
_type = _dynamicType;
}
Expand Down Expand Up @@ -126,7 +131,7 @@ class NamedTypeBuilder extends TypeBuilder {
return this;
}

return NamedTypeBuilder(isNNBD, element, arguments, nullabilitySuffix,
return NamedTypeBuilder(typeSystem, element, arguments, nullabilitySuffix,
node: node);
}

Expand Down Expand Up @@ -224,7 +229,7 @@ class NamedTypeBuilder extends TypeBuilder {
NullabilitySuffix _getNullabilitySuffix(bool hasQuestion) {
if (hasQuestion) {
return NullabilitySuffix.question;
} else if (isNNBD) {
} else if (typeSystem.isNonNullableByDefault) {
return NullabilitySuffix.none;
} else {
return NullabilitySuffix.star;
Expand Down
7 changes: 5 additions & 2 deletions pkg/analyzer/lib/src/summary2/reference_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/resolver/scope.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:analyzer/src/summary/idl.dart';
import 'package:analyzer/src/summary2/function_type_builder.dart';
import 'package:analyzer/src/summary2/lazy_ast.dart';
Expand All @@ -30,6 +31,7 @@ import 'package:analyzer/src/summary2/types_builder.dart';
/// the type is set, otherwise we keep it empty, so we will attempt to infer
/// it later).
class ReferenceResolver extends ThrowingAstVisitor<void> {
final TypeSystemImpl _typeSystem;
final NodesToBuildType nodesToBuildType;
final LinkedElementFactory elementFactory;
final LibraryElement _libraryElement;
Expand All @@ -54,7 +56,8 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
this.unitReference,
this.isNNBD,
this.scope,
) : reference = unitReference;
) : _typeSystem = _libraryElement.typeSystem,
reference = unitReference;

@override
void visitBlockFunctionBody(BlockFunctionBody node) {}
Expand Down Expand Up @@ -514,7 +517,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
);
} else {
var builder = NamedTypeBuilder.of(
isNNBD,
_typeSystem,
node,
element,
nullabilitySuffix,
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/test_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import 'property_access_test.dart' as property_access;
import 'simple_identifier_test.dart' as simple_identifier;
import 'top_type_inference_test.dart' as top_type_inference;
import 'type_inference/test_all.dart' as type_inference;
import 'type_name_test.dart' as type_name;

main() {
defineReflectiveSuite(() {
Expand Down Expand Up @@ -93,6 +94,7 @@ main() {
property_access.main();
simple_identifier.main();
top_type_inference.main();
type_name.main();
type_inference.main();
}, name: 'resolution');
}
Loading

0 comments on commit 582727a

Please sign in to comment.