Skip to content

Commit 3392647

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate ConstructorReferenceResolver.
Change-Id: Ib9debc57f199cf33529807350a443b0db94e208d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/406460 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 366f33f commit 3392647

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4472,6 +4472,10 @@ final class ConstructorNameImpl extends AstNodeImpl implements ConstructorName {
44724472
@override
44734473
ConstructorElement2? get element => staticElement?.asElement2;
44744474

4475+
set element(ConstructorElement2? element) {
4476+
staticElement = element?.asElement;
4477+
}
4478+
44754479
@override
44764480
Token get endToken {
44774481
if (name case var name?) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,30 @@ class ConstructorMember extends ExecutableMember
197197
substitution: Substitution.fromInterfaceType(definingType),
198198
);
199199
}
200+
201+
/// If the given [constructor]'s type is different when any type parameters
202+
/// from the defining type's declaration are replaced with the actual type
203+
/// arguments from the [definingType], create a constructor member
204+
/// representing the given constructor. Return the member that was created, or
205+
/// the original constructor if no member was created.
206+
static ConstructorElement2 from2(
207+
ConstructorElement2 constructor, InterfaceType definingType) {
208+
if (definingType.typeArguments.isEmpty) {
209+
return constructor;
210+
}
211+
212+
var augmentationSubstitution = Substitution.empty;
213+
if (constructor is ConstructorMember) {
214+
augmentationSubstitution = constructor.augmentationSubstitution;
215+
constructor = constructor.baseElement;
216+
}
217+
218+
return ConstructorMember(
219+
declaration: constructor.asElement,
220+
augmentationSubstitution: augmentationSubstitution,
221+
substitution: Substitution.fromInterfaceType(definingType),
222+
);
223+
}
200224
}
201225

202226
/// An executable element defined in a parameterized type where the values of

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// ignore_for_file: analyzer_use_new_elements
6-
7-
import 'package:analyzer/dart/element/element.dart';
5+
import 'package:analyzer/dart/element/element2.dart';
86
import 'package:analyzer/dart/element/type.dart';
97
import 'package:analyzer/src/dart/ast/ast.dart';
108
import 'package:analyzer/src/dart/element/member.dart';
@@ -30,10 +28,10 @@ class ConstructorReferenceResolver {
3028
);
3129
}
3230
node.constructorName.accept(_resolver);
33-
var element = node.constructorName.staticElement;
31+
var element = node.constructorName.element;
3432
if (element != null && !element.isFactory) {
35-
var enclosingElement = element.enclosingElement3;
36-
if (enclosingElement is ClassElement && enclosingElement.isAbstract) {
33+
var enclosingElement = element.enclosingElement2;
34+
if (enclosingElement is ClassElement2 && enclosingElement.isAbstract) {
3735
_resolver.errorReporter.atNode(
3836
node,
3937
CompileTimeErrorCode
@@ -52,19 +50,19 @@ class ConstructorReferenceResolver {
5250
//
5351
// Only report errors when the constructor tearoff feature is enabled,
5452
// to avoid reporting redundant errors.
55-
var enclosingElement = node.constructorName.type.element;
56-
if (enclosingElement is TypeAliasElement) {
53+
var enclosingElement = node.constructorName.type.element2;
54+
if (enclosingElement is TypeAliasElement2) {
5755
var aliasedType = enclosingElement.aliasedType;
5856
enclosingElement =
59-
aliasedType is InterfaceType ? aliasedType.element : null;
57+
aliasedType is InterfaceType ? aliasedType.element3 : null;
6058
}
6159
// TODO(srawlins): Handle `enclosingElement` being a function typedef:
6260
// typedef F<T> = void Function(); var a = F<int>.extensionOnType;`.
6361
// This is illegal.
64-
if (enclosingElement is InterfaceElement) {
65-
var method = enclosingElement.getMethod(name.name) ??
66-
enclosingElement.getGetter(name.name) ??
67-
enclosingElement.getSetter(name.name);
62+
if (enclosingElement is InterfaceElement2) {
63+
var method = enclosingElement.getMethod2(name.name) ??
64+
enclosingElement.getGetter2(name.name) ??
65+
enclosingElement.getSetter2(name.name);
6866
if (method != null) {
6967
var error = method.isStatic
7068
? CompileTimeErrorCode.CLASS_INSTANTIATION_ACCESS_TO_STATIC_MEMBER
@@ -79,7 +77,7 @@ class ConstructorReferenceResolver {
7977
_resolver.errorReporter.atNode(
8078
node,
8179
CompileTimeErrorCode.CLASS_INSTANTIATION_ACCESS_TO_UNKNOWN_MEMBER,
82-
arguments: [enclosingElement.name, name.name],
80+
arguments: [enclosingElement.name3!, name.name],
8381
);
8482
}
8583
}
@@ -102,7 +100,7 @@ class ConstructorReferenceResolver {
102100
// Otherwise we'll have a ConstructorElement, and we can skip inference
103101
// because there's nothing to infer in a non-generic type.
104102
if (elementToInfer != null &&
105-
elementToInfer.typeParameters.isNotEmpty &&
103+
elementToInfer.typeParameters2.isNotEmpty &&
106104
constructorName.type.typeArguments == null) {
107105
// TODO(leafp): Currently, we may re-infer types here, since we
108106
// sometimes resolve multiple times. We should really check that we
@@ -114,7 +112,7 @@ class ConstructorReferenceResolver {
114112
// Get back to the uninstantiated generic constructor.
115113
// TODO(jmesserly): should we store this earlier in resolution?
116114
// Or look it up, instead of jumping backwards through the Member?
117-
var rawElement = elementToInfer.element;
115+
var rawElement = elementToInfer.element2;
118116
var constructorType = elementToInfer.asType;
119117

120118
var inferred = _resolver.inferenceHelper.inferTearOff(
@@ -127,16 +125,16 @@ class ConstructorReferenceResolver {
127125
// Update the static element as well. This is used in some cases, such
128126
// as computing constant values. It is stored in two places.
129127
var constructorElement =
130-
ConstructorMember.from(rawElement, inferredReturnType);
128+
ConstructorMember.from2(rawElement, inferredReturnType);
131129

132-
constructorName.staticElement = constructorElement.declaration;
133-
constructorName.name?.staticElement = constructorElement.declaration;
130+
constructorName.element = constructorElement.baseElement;
131+
constructorName.name?.element = constructorElement.baseElement;
134132
node.recordStaticType(inferred, resolver: _resolver);
135133
// The NamedType child of `constructorName` doesn't have a static type.
136134
constructorName.type.type = null;
137135
}
138136
} else {
139-
var constructorElement = constructorName.staticElement;
137+
var constructorElement = constructorName.element;
140138
node.recordStaticType(
141139
constructorElement == null
142140
? InvalidTypeImpl.instance

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import 'package:analyzer/dart/analysis/features.dart';
88
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/element2.dart';
910
import 'package:analyzer/dart/element/nullability_suffix.dart';
1011
import 'package:analyzer/dart/element/type.dart';
1112
import 'package:analyzer/error/listener.dart';
@@ -16,6 +17,7 @@ import 'package:analyzer/src/dart/element/type_constraint_gatherer.dart';
1617
import 'package:analyzer/src/dart/element/type_system.dart';
1718
import 'package:analyzer/src/dart/resolver/invocation_inferrer.dart';
1819
import 'package:analyzer/src/generated/resolver.dart';
20+
import 'package:analyzer/src/utilities/extensions/element.dart';
1921

2022
/// Information about a constructor element to instantiate.
2123
///
@@ -54,6 +56,12 @@ class ConstructorElementToInfer {
5456
nullabilitySuffix: NullabilitySuffix.none,
5557
);
5658
}
59+
60+
ConstructorElement2 get element2 => element.asElement2;
61+
62+
List<TypeParameterElement2> get typeParameters2 {
63+
return typeParameters.map((e) => e.asElement2).toList();
64+
}
5765
}
5866

5967
class InvocationInferenceHelper {

0 commit comments

Comments
 (0)