Skip to content

Commit 9f0968a

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate FromEnvironmentEvaluator and FromEnvironmentEvaluatorTest.
Change-Id: Ic0d0026647a907dc50811ae331add07f042cb78d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396423 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent bd44f1f commit 9f0968a

File tree

11 files changed

+40
-25
lines changed

11 files changed

+40
-25
lines changed

pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ class DeclarationHelper {
16231623

16241624
var suggestion = ConstructorSuggestion(
16251625
importData: importData,
1626-
element: element.asElement2 as ConstructorElement2,
1626+
element: element.asElement2,
16271627
hasClassName: hasClassName,
16281628
isTearOff: isTearOff,
16291629
isRedirect: isConstructorRedirect,

pkg/analyzer/analyzer_use_new_elements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ lib/src/dart/ast/extensions.dart
2424
lib/src/dart/ast/utilities.dart
2525
lib/src/dart/constant/constant_verifier.dart
2626
lib/src/dart/constant/evaluation.dart
27-
lib/src/dart/constant/from_environment_evaluator.dart
2827
lib/src/dart/constant/potentially_constant.dart
2928
lib/src/dart/constant/utilities.dart
3029
lib/src/dart/constant/value.dart
@@ -170,7 +169,6 @@ lib/src/utilities/extensions/library_element.dart
170169
lib/src/wolf/ir/ast_to_ir.dart
171170
lib/src/wolf/ir/call_descriptor.dart
172171
lib/src/wolf/ir/coded_ir.dart
173-
test/dart/analysis/from_environment_evaluator_test.dart
174172
test/dart/sdk/build_sdk_summary_test.dart
175173
test/error/error_reporter_test.dart
176174
test/generated/element_resolver_test.dart

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,9 @@ abstract class InterfaceElement2 implements InstanceElement2 {
12701270
/// constructor will be returned.
12711271
ConstructorElement2? get unnamedConstructor2;
12721272

1273+
/// Returns the constructor from [constructors2] that has the given [name].
1274+
ConstructorElement2? getNamedConstructor2(String name);
1275+
12731276
/// Create the [InterfaceType] for this element with the given
12741277
/// [typeArguments] and [nullabilitySuffix].
12751278
InterfaceType instantiate({

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,8 +4459,7 @@ final class ConstructorNameImpl extends AstNodeImpl implements ConstructorName {
44594459

44604460
@experimental
44614461
@override
4462-
ConstructorElement2? get element =>
4463-
staticElement?.asElement2 as ConstructorElement2?;
4462+
ConstructorElement2? get element => staticElement?.asElement2;
44644463

44654464
@override
44664465
Token get endToken {
@@ -5621,7 +5620,7 @@ final class EnumConstantDeclarationImpl extends DeclarationImpl
56215620
@experimental
56225621
@override
56235622
ConstructorElement2? get constructorElement2 =>
5624-
constructorElement?.asElement2 as ConstructorElement2?;
5623+
constructorElement?.asElement2;
56255624

56265625
@experimental
56275626
@override
@@ -15077,8 +15076,7 @@ final class RedirectingConstructorInvocationImpl
1507715076

1507815077
@experimental
1507915078
@override
15080-
ConstructorElement2? get element =>
15081-
staticElement?.asElement2 as ConstructorElement2?;
15079+
ConstructorElement2? get element => staticElement?.asElement2;
1508215080

1508315081
@override
1508415082
Token get endToken => _argumentList.endToken;
@@ -16629,8 +16627,7 @@ final class SuperConstructorInvocationImpl extends ConstructorInitializerImpl
1662916627

1663016628
@experimental
1663116629
@override
16632-
ConstructorElement2? get element =>
16633-
staticElement?.asElement2 as ConstructorElement2?;
16630+
ConstructorElement2? get element => staticElement?.asElement2;
1663416631

1663516632
@override
1663616633
Token get endToken => _argumentList.endToken;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import 'package:analyzer/src/generated/engine.dart';
3333
import 'package:analyzer/src/generated/java_core.dart';
3434
import 'package:analyzer/src/task/api/model.dart';
3535
import 'package:analyzer/src/utilities/extensions/collection.dart';
36+
import 'package:analyzer/src/utilities/extensions/element.dart';
3637
import 'package:analyzer/src/utilities/extensions/object.dart';
3738

3839
class ConstantEvaluationConfiguration {
@@ -2503,13 +2504,13 @@ class _InstanceCreationEvaluator {
25032504
);
25042505
}
25052506
return FromEnvironmentEvaluator(typeSystem, _declaredVariables)
2506-
.getBool2(variableName, _namedValues, _constructor);
2507+
.getBool2(variableName, _namedValues, _constructor.asElement2);
25072508
} else if (definingClass == typeProvider.intElement) {
25082509
return FromEnvironmentEvaluator(typeSystem, _declaredVariables)
2509-
.getInt2(variableName, _namedValues, _constructor);
2510+
.getInt2(variableName, _namedValues, _constructor.asElement2);
25102511
} else if (definingClass == typeProvider.stringElement) {
25112512
return FromEnvironmentEvaluator(typeSystem, _declaredVariables)
2512-
.getString2(variableName, _namedValues, _constructor);
2513+
.getString2(variableName, _namedValues, _constructor.asElement2);
25132514
}
25142515
} else if (_constructor.name == 'hasEnvironment' &&
25152516
definingClass == typeProvider.boolElement) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:analyzer/dart/analysis/declared_variables.dart';
66
import 'package:analyzer/dart/constant/value.dart';
7-
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
88
import 'package:analyzer/src/dart/constant/value.dart';
99
import 'package:analyzer/src/dart/element/type_system.dart';
1010

@@ -25,7 +25,7 @@ class FromEnvironmentEvaluator {
2525
DartObjectImpl getBool2(
2626
String? name,
2727
Map<String, DartObjectImpl> namedValues,
28-
ConstructorElement constructor,
28+
ConstructorElement2 constructor,
2929
) {
3030
var str = name != null ? _declaredVariables.get(name) : null;
3131
if (str == 'true') {
@@ -55,7 +55,7 @@ class FromEnvironmentEvaluator {
5555
DartObjectImpl getInt2(
5656
String? name,
5757
Map<String, DartObjectImpl> namedValues,
58-
ConstructorElement constructor,
58+
ConstructorElement2 constructor,
5959
) {
6060
var str = name != null ? _declaredVariables.get(name) : null;
6161
if (str != null) {
@@ -98,7 +98,7 @@ class FromEnvironmentEvaluator {
9898
DartObjectImpl getString2(
9999
String? name,
100100
Map<String, DartObjectImpl> namedValues,
101-
ConstructorElement constructor,
101+
ConstructorElement2 constructor,
102102
) {
103103
var str = name != null ? _declaredVariables.get(name) : null;
104104
if (str != null) {
@@ -138,9 +138,9 @@ class FromEnvironmentEvaluator {
138138
}
139139

140140
static DartObjectImpl _defaultValueDefaultValue(
141-
ConstructorElement constructor) {
142-
return constructor.parameters
143-
.singleWhere((parameter) => parameter.name == _defaultValue)
141+
ConstructorElement2 constructor) {
142+
return constructor.formalParameters
143+
.singleWhere((parameter) => parameter.name3 == _defaultValue)
144144
.computeConstantValue() as DartObjectImpl;
145145
}
146146
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6245,6 +6245,11 @@ abstract class InterfaceElementImpl2 extends InstanceElementImpl2
62456245
return constructors.firstWhereOrNull((element) => element.name == name);
62466246
}
62476247

6248+
@override
6249+
ConstructorElement2? getNamedConstructor2(String name) {
6250+
return constructors2.firstWhereOrNull((e) => e.name3 == name);
6251+
}
6252+
62486253
@override
62496254
InterfaceType instantiate({
62506255
required List<DartType> typeArguments,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ class ConstructorMember extends ExecutableMember
115115
superConstructor.asElement2 as ConstructorElement2;
116116

117117
@override
118-
ConstructorElement2 get _element2 =>
119-
declaration.asElement2 as ConstructorElement2;
118+
ConstructorElement2 get _element2 => declaration.asElement2;
120119

121120
@override
122121
T? accept<T>(ElementVisitor<T> visitor) =>

pkg/analyzer/lib/src/test_utilities/mock_sdk_elements.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:analyzer/src/generated/engine.dart' as engine;
1414
import 'package:analyzer/src/generated/testing/element_factory.dart';
1515
import 'package:analyzer/src/generated/utilities_dart.dart';
1616
import 'package:analyzer/src/summary2/reference.dart';
17+
import 'package:analyzer/src/utilities/extensions/string.dart';
1718

1819
class MockSdkElements {
1920
final LibraryElementImpl coreLibrary;
@@ -992,6 +993,7 @@ class _MockSdkElementsBuilder {
992993
List<ParameterElement> parameters = const [],
993994
}) {
994995
var element = ConstructorElementImpl(name, 0);
996+
element.name2 = name.ifNotEmptyOrElse('new');
995997
element.isFactory = isFactory;
996998
element.isConst = isConst;
997999
element.parameters = parameters;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ extension CompilationUnitElementExtension on CompilationUnitElement {
2828
}
2929
}
3030

31+
extension ConstructorElementExtension on ConstructorElement {
32+
ConstructorElement2 get asElement2 {
33+
return switch (this) {
34+
ConstructorFragment(:var element) => element,
35+
ConstructorMember member => member,
36+
_ => throw UnsupportedError('Unsupported type: $runtimeType'),
37+
};
38+
}
39+
}
40+
3141
extension Element2OrNullExtension on Element2? {
3242
Element? get asElement {
3343
var self = this;

0 commit comments

Comments
 (0)