Skip to content

Commit 0990e6b

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Require ConstructorElementImpl for ConstructorMember.
Change-Id: I8b9ddca7aeaa6c40a03819d16cea50c189dea426 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/416882 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 51d0bab commit 0990e6b

File tree

14 files changed

+333
-62
lines changed

14 files changed

+333
-62
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15265,7 +15265,7 @@ final class RedirectingConstructorInvocationImpl
1526515265
ArgumentListImpl _argumentList;
1526615266

1526715267
@override
15268-
ConstructorElementMixin? staticElement;
15268+
ConstructorElementImpl? staticElement;
1526915269

1527015270
/// Initializes a newly created redirecting invocation to invoke the
1527115271
/// constructor with the given name with the given arguments.
@@ -15302,9 +15302,9 @@ final class RedirectingConstructorInvocationImpl
1530215302

1530315303
@experimental
1530415304
@override
15305-
ConstructorElementMixin2? get element => staticElement?.asElement2;
15305+
ConstructorElementImpl2? get element => staticElement?.asElement2;
1530615306

15307-
set element(ConstructorElementMixin2? value) {
15307+
set element(ConstructorElementImpl2? value) {
1530815308
staticElement = value?.asElement;
1530915309
}
1531015310

pkg/analyzer/lib/src/dart/constant/evaluation.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,10 +2981,10 @@ class _InstanceCreationEvaluator {
29812981
} else if (initializer is RedirectingConstructorInvocationImpl) {
29822982
// This is a redirecting constructor, so just evaluate the constructor
29832983
// it redirects to.
2984-
var constructor = initializer.staticElement;
2985-
if (constructor != null && constructor.isConst) {
2984+
var baseElement = initializer.staticElement;
2985+
if (baseElement != null && baseElement.isConst) {
29862986
// Instantiate the constructor with the in-scope type arguments.
2987-
constructor = ConstructorMember.from(constructor, definingType);
2987+
var constructor = ConstructorMember.from(baseElement, definingType);
29882988
var result = _evaluationEngine.evaluateConstructorCall(
29892989
_library,
29902990
_errorNode,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6433,7 +6433,7 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
64336433
}
64346434

64356435
@override
6436-
ConstructorElementMixin2? get unnamedConstructor2 {
6436+
ConstructorElementImpl2? get unnamedConstructor2 {
64376437
return getNamedConstructor2('new');
64386438
}
64396439

@@ -6454,7 +6454,7 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
64546454
.getMember4(this, name);
64556455

64566456
@override
6457-
ConstructorElementMixin2? getNamedConstructor2(String name) {
6457+
ConstructorElementImpl2? getNamedConstructor2(String name) {
64586458
return constructors2.firstWhereOrNull((e) => e.name3 == name);
64596459
}
64606460

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

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ConstructorMember extends ExecutableMember
2828
/// Initialize a newly created element to represent a constructor, based on
2929
/// the [declaration], and applied [substitution].
3030
ConstructorMember({
31-
required ConstructorElement super.declaration,
31+
required ConstructorElementImpl super.declaration,
3232
required super.substitution,
3333
}) : super(
3434
typeParameters: const <TypeParameterElementImpl>[],
@@ -90,7 +90,7 @@ class ConstructorMember extends ExecutableMember
9090
@override
9191
ConstructorElementMixin? get redirectedConstructor {
9292
var element = declaration.redirectedConstructor;
93-
return _from2(element);
93+
return _redirect(element);
9494
}
9595

9696
@override
@@ -114,7 +114,7 @@ class ConstructorMember extends ExecutableMember
114114
@override
115115
ConstructorElement? get superConstructor {
116116
var element = declaration.superConstructor;
117-
return _from2(element);
117+
return _redirect(element);
118118
}
119119

120120
@override
@@ -138,71 +138,61 @@ class ConstructorMember extends ExecutableMember
138138
builder.writeConstructorElement(this);
139139
}
140140

141-
ConstructorMember? _from2(ConstructorElement? element) {
142-
if (element == null) {
143-
return null;
144-
}
145-
146-
ConstructorElement declaration;
147-
MapSubstitution substitution;
148-
if (element is ConstructorMember) {
149-
declaration = element._declaration as ConstructorElement;
150-
var map = <TypeParameterElement2, DartType>{};
151-
var elementMap = element.substitution.map;
152-
for (var typeParameter in elementMap.keys) {
153-
var type = elementMap[typeParameter]!;
154-
map[typeParameter] = this.substitution.substituteType(type);
155-
}
156-
substitution = Substitution.fromMap2(map);
157-
} else {
158-
declaration = element;
159-
substitution = this.substitution;
141+
ConstructorElementMixin? _redirect(ConstructorElementMixin? element) {
142+
switch (element) {
143+
case null:
144+
return null;
145+
case ConstructorElementImpl():
146+
return element;
147+
case ConstructorMember():
148+
var memberMap = element.substitution.map;
149+
var map = <TypeParameterElement2, DartType>{
150+
for (var MapEntry(:key, :value) in memberMap.entries)
151+
key: substitution.substituteType(value),
152+
};
153+
return ConstructorMember(
154+
declaration: element.declaration,
155+
substitution: Substitution.fromMap2(map),
156+
);
157+
default:
158+
throw UnimplementedError('(${element.runtimeType}) $element');
160159
}
161-
162-
return ConstructorMember(
163-
declaration: declaration,
164-
substitution: substitution,
165-
);
166160
}
167161

168-
/// If the given [constructor]'s type is different when any type parameters
162+
/// If the given [element]'s type is different when any type parameters
169163
/// from the defining type's declaration are replaced with the actual type
170164
/// arguments from the [definingType], create a constructor member
171165
/// representing the given constructor. Return the member that was created, or
172166
/// the original constructor if no member was created.
173167
static ConstructorElementMixin from(
174-
ConstructorElementMixin constructor, InterfaceType definingType) {
168+
ConstructorElementImpl element,
169+
InterfaceType definingType,
170+
) {
175171
if (definingType.typeArguments.isEmpty) {
176-
return constructor;
177-
}
178-
179-
if (constructor is ConstructorMember) {
180-
constructor = constructor.declaration;
172+
return element;
181173
}
182174

183175
return ConstructorMember(
184-
declaration: constructor,
176+
declaration: element,
185177
substitution: Substitution.fromInterfaceType(definingType),
186178
);
187179
}
188180

189-
/// If the given [constructor]'s type is different when any type parameters
181+
/// If the given [element]'s type is different when any type parameters
190182
/// from the defining type's declaration are replaced with the actual type
191183
/// arguments from the [definingType], create a constructor member
192184
/// representing the given constructor. Return the member that was created, or
193185
/// the original constructor if no member was created.
194186
static ConstructorElementMixin2 from2(
195-
ConstructorElementMixin2 constructor, InterfaceType definingType) {
187+
ConstructorElementImpl2 element,
188+
InterfaceType definingType,
189+
) {
196190
if (definingType.typeArguments.isEmpty) {
197-
return constructor;
198-
}
199-
200-
if (constructor is ConstructorMember) {
201-
constructor = constructor.baseElement;
191+
return element;
202192
}
203193

204194
return ConstructorMember(
205-
declaration: constructor.asElement,
195+
declaration: element.asElement,
206196
substitution: Substitution.fromInterfaceType(definingType),
207197
);
208198
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
957957
ConstructorElementMixin2? lookUpConstructor2(
958958
String? constructorName, LibraryElement2 library) {
959959
// prepare base ConstructorElement
960-
ConstructorElementMixin2? constructorElement;
960+
ConstructorElementImpl2? constructorElement;
961961
if (constructorName == null) {
962962
constructorElement = element3.unnamedConstructor2;
963963
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class ConstructorReferenceResolver {
114114
// Get back to the uninstantiated generic constructor.
115115
// TODO(jmesserly): should we store this earlier in resolution?
116116
// Or look it up, instead of jumping backwards through the Member?
117-
var rawElement = elementToInfer.element2;
117+
var rawElement = elementToInfer.element2.baseElement;
118118
var constructorType = elementToInfer.asType;
119119

120120
var inferred = _resolver.inferenceHelper.inferTearOff(

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ class AnnotationInferrer extends FullInvocationInferrer<AnnotationImpl> {
9090
List<FormalParameterElement>? _storeResult(
9191
List<DartType>? typeArgumentTypes, FunctionType? invokeType) {
9292
if (invokeType != null) {
93+
var elementOrMember = node.element2 as ConstructorElementMixin2;
9394
var constructorElement = ConstructorMember.from2(
94-
node.element2 as ConstructorElementMixin2,
95+
elementOrMember.baseElement,
9596
invokeType.returnType as InterfaceType,
9697
);
9798
constructorName?.element = constructorElement;
@@ -393,7 +394,7 @@ class InstanceCreationInferrer
393394
var constructedType = invokeType.returnType;
394395
node.constructorName.type.type = constructedType;
395396
var constructorElement = ConstructorMember.from2(
396-
node.constructorName.element!,
397+
node.constructorName.element!.baseElement,
397398
constructedType as InterfaceType,
398399
);
399400
node.constructorName.element = constructorElement;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class ElementResolver {
302302
// TODO(brianwilkerson): Report this error.
303303
return;
304304
}
305-
ConstructorElementMixin2? element;
305+
ConstructorElementImpl2? element;
306306
var name = node.constructorName;
307307
if (name == null) {
308308
element = enclosingClass.unnamedConstructor2;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ class AstBinaryReader {
11581158
constructorName: constructorName,
11591159
argumentList: argumentList,
11601160
);
1161-
node.element = _reader.readElement2() as ConstructorElementMixin2?;
1161+
node.element = _reader.readElement2() as ConstructorElementImpl2?;
11621162
_resolveNamedExpressions(node.element, node.argumentList);
11631163
return node;
11641164
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ mixin ResolutionTest implements ResourceProviderMixin {
468468
..withInterfaceTypeElements =
469469
nodeTextConfiguration.withInterfaceTypeElements
470470
..withRedirectedConstructors =
471-
nodeTextConfiguration.withRedirectedConstructors,
471+
nodeTextConfiguration.withRedirectedConstructors
472+
..withSuperConstructors = nodeTextConfiguration.withSuperConstructors,
472473
);
473474
node.accept(
474475
ResolvedAstPrinter(

0 commit comments

Comments
 (0)