Skip to content

Commit b7ba61a

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Remove ConstVariableFragment.
Change-Id: Id0605298c4d3c702cd7eba87975eaa2b374c778e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437640 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 4e5b1cf commit b7ba61a

File tree

4 files changed

+54
-72
lines changed

4 files changed

+54
-72
lines changed

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

Lines changed: 48 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ import 'package:pub_semver/pub_semver.dart';
6262

6363
// TODO(fshcheglov): Remove after third_party/pkg/dartdoc stops using it.
6464
// https://github.com/dart-lang/dartdoc/issues/4066
65-
@Deprecated('Use ConstVariableFragment instead')
66-
typedef ConstVariableElement = ConstVariableFragment;
65+
@Deprecated('Use VariableFragmentImpl instead')
66+
typedef ConstVariableElement = VariableFragmentImpl;
6767

6868
abstract class AnnotatableElementImpl implements ElementImpl, Annotatable {
6969
@override
@@ -727,8 +727,7 @@ class ConstantInitializerImpl implements ConstantInitializer {
727727

728728
/// A `LocalVariableElement` for a local 'const' variable that has an
729729
/// initializer.
730-
class ConstLocalVariableFragmentImpl extends LocalVariableFragmentImpl
731-
with ConstVariableFragment {
730+
class ConstLocalVariableFragmentImpl extends LocalVariableFragmentImpl {
732731
/// Initialize a newly created local variable element to have the given [name]
733732
/// and [offset].
734733
ConstLocalVariableFragmentImpl({
@@ -1132,62 +1131,6 @@ class ConstructorFragmentImpl extends ExecutableFragmentImpl
11321131
}
11331132
}
11341133

1135-
/// Mixin used by elements that represent constant variables and have
1136-
/// initializers.
1137-
///
1138-
/// Note that in correct Dart code, all constant variables must have
1139-
/// initializers. However, analyzer also needs to handle incorrect Dart code,
1140-
/// in which case there might be some constant variables that lack initializers.
1141-
/// This interface is only used for constant variables that have initializers.
1142-
///
1143-
/// This class is not intended to be part of the public API for analyzer.
1144-
mixin ConstVariableFragment implements FragmentImpl, ConstantEvaluationTarget {
1145-
/// If this element represents a constant variable, and it has an initializer,
1146-
/// a copy of the initializer for the constant. Otherwise `null`.
1147-
///
1148-
/// Note that in correct Dart code, all constant variables must have
1149-
/// initializers. However, analyzer also needs to handle incorrect Dart code,
1150-
/// in which case there might be some constant variables that lack
1151-
/// initializers.
1152-
ExpressionImpl? constantInitializer;
1153-
1154-
Constant? _evaluationResult;
1155-
1156-
Constant? get evaluationResult => _evaluationResult;
1157-
1158-
set evaluationResult(Constant? evaluationResult) {
1159-
_evaluationResult = evaluationResult;
1160-
}
1161-
1162-
@override
1163-
bool get isConstantEvaluated => _evaluationResult != null;
1164-
1165-
/// Return a representation of the value of this variable, forcing the value
1166-
/// to be computed if it had not previously been computed, or `null` if either
1167-
/// this variable was not declared with the 'const' modifier or if the value
1168-
/// of this variable could not be computed because of errors.
1169-
DartObject? computeConstantValue() {
1170-
if (evaluationResult == null) {
1171-
var library = this.library;
1172-
// TODO(scheglov): https://github.com/dart-lang/sdk/issues/47915
1173-
if (library == null) {
1174-
return null;
1175-
}
1176-
computeConstants(
1177-
declaredVariables: context.declaredVariables,
1178-
constants: [this],
1179-
featureSet: library.featureSet,
1180-
configuration: ConstantEvaluationConfiguration(),
1181-
);
1182-
}
1183-
1184-
if (evaluationResult case DartObjectImpl result) {
1185-
return result;
1186-
}
1187-
return null;
1188-
}
1189-
}
1190-
11911134
/// This mixin is used to set up loading class members from summaries only when
11921135
/// they are requested. The summary reader uses [deferReadMembers], and
11931136
/// getters invoke [ensureReadMembers].
@@ -2897,7 +2840,6 @@ class FieldFormalParameterFragmentImpl extends FormalParameterFragmentImpl
28972840
}
28982841

28992842
class FieldFragmentImpl extends PropertyInducingFragmentImpl
2900-
with ConstVariableFragment
29012843
implements FieldFragment {
29022844
/// True if this field inherits from a covariant parameter. This happens
29032845
/// when it overrides a field in a supertype that is covariant.
@@ -8871,9 +8813,6 @@ abstract class PropertyInducingFragmentImpl
88718813
return !isFinal;
88728814
}
88738815

8874-
@override
8875-
bool get isConstantEvaluated => true;
8876-
88778816
@override
88788817
bool get isLate {
88798818
return hasModifier(Modifier.LATE);
@@ -9491,7 +9430,6 @@ class TopLevelVariableElementImpl extends PropertyInducingElementImpl
94919430
}
94929431

94939432
class TopLevelVariableFragmentImpl extends PropertyInducingFragmentImpl
9494-
with ConstVariableFragment
94959433
implements TopLevelVariableFragment {
94969434
@override
94979435
late TopLevelVariableElementImpl element;
@@ -10329,14 +10267,29 @@ abstract class VariableElementOrMember
1032910267
}
1033010268

1033110269
abstract class VariableFragmentImpl extends FragmentImpl
10332-
with ConstVariableFragment
1033310270
implements
1033410271
VariableElementOrMember,
1033510272
AnnotatableFragmentImpl,
10273+
ConstantEvaluationTarget,
1033610274
VariableFragment {
1033710275
/// The type of this variable.
1033810276
TypeImpl? _type;
1033910277

10278+
/// If this element represents a constant variable, and it has an initializer,
10279+
/// a copy of the initializer for the constant. Otherwise `null`.
10280+
///
10281+
/// Note that in correct Dart code, all constant variables must have
10282+
/// initializers. However, analyzer also needs to handle incorrect Dart code,
10283+
/// in which case there might be some constant variables that lack
10284+
/// initializers.
10285+
ExpressionImpl? constantInitializer;
10286+
10287+
/// The result of evaluating [constantInitializer].
10288+
///
10289+
/// Is `null` if [constantInitializer] is `null`, or if the value could not
10290+
/// be computed because of errors.
10291+
Constant? evaluationResult;
10292+
1034010293
/// Initialize a newly created variable element to have the given [name] and
1034110294
/// [offset].
1034210295
VariableFragmentImpl({required super.nameOffset});
@@ -10380,6 +10333,9 @@ abstract class VariableFragmentImpl extends FragmentImpl
1038010333
setModifier(Modifier.CONST, isConst);
1038110334
}
1038210335

10336+
@override
10337+
bool get isConstantEvaluated => evaluationResult != null;
10338+
1038310339
/// Set whether this variable is external.
1038410340
set isExternal(bool isExternal) {
1038510341
setModifier(Modifier.EXTERNAL, isExternal);
@@ -10421,6 +10377,32 @@ abstract class VariableFragmentImpl extends FragmentImpl
1042110377
void appendTo(ElementDisplayStringBuilder builder) {
1042210378
builder.writeVariableElement(this);
1042310379
}
10380+
10381+
/// Return a representation of the value of this variable, forcing the value
10382+
/// to be computed if it had not previously been computed, or `null` if either
10383+
/// this variable was not declared with the 'const' modifier or if the value
10384+
/// of this variable could not be computed because of errors.
10385+
@override
10386+
DartObject? computeConstantValue() {
10387+
if (evaluationResult == null) {
10388+
var library = this.library;
10389+
// TODO(scheglov): https://github.com/dart-lang/sdk/issues/47915
10390+
if (library == null) {
10391+
return null;
10392+
}
10393+
computeConstants(
10394+
declaredVariables: context.declaredVariables,
10395+
constants: [this],
10396+
featureSet: library.featureSet,
10397+
configuration: ConstantEvaluationConfiguration(),
10398+
);
10399+
}
10400+
10401+
if (evaluationResult case DartObjectImpl result) {
10402+
return result;
10403+
}
10404+
return null;
10405+
}
1042410406
}
1042510407

1042610408
mixin WrappedElementMixin implements ElementImpl {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class LibraryBuilder {
8484
int _nextUnnamedId = 0;
8585

8686
/// The fields that were speculatively created as [FieldFragmentImpl],
87-
/// but we want to clear [ConstVariableFragment.constantInitializer] for it
87+
/// but we want to clear [VariableFragmentImpl.constantInitializer] for it
8888
/// if the class will not end up with a `const` constructor. We don't know
8989
/// at the time when we create them, because of future augmentations.
9090
final Set<FieldFragmentImpl> finalInstanceFields = Set.identity();

pkg/analyzer/test/src/dart/constant/evaluation_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5406,7 +5406,7 @@ class ConstantVisitorTestSupport extends PubPackageResolutionTest {
54065406
return result;
54075407
}
54085408

5409-
DartObjectImpl? _evaluationResult(ConstVariableFragment element) {
5409+
DartObjectImpl? _evaluationResult(VariableFragmentImpl element) {
54105410
var evaluationResult = element.evaluationResult;
54115411
switch (evaluationResult) {
54125412
case null:
@@ -5420,19 +5420,19 @@ class ConstantVisitorTestSupport extends PubPackageResolutionTest {
54205420

54215421
DartObjectImpl? _field(String variableName) {
54225422
var element = findElement2.field(variableName);
5423-
var constFragment = element.firstFragment as ConstVariableFragment;
5423+
var constFragment = element.firstFragment as VariableFragmentImpl;
54245424
return _evaluationResult(constFragment);
54255425
}
54265426

54275427
DartObjectImpl? _localVar(String variableName) {
54285428
var element = findElement2.localVar(variableName);
5429-
var constFragment = element.firstFragment as ConstVariableFragment;
5429+
var constFragment = element.firstFragment as VariableFragmentImpl;
54305430
return _evaluationResult(constFragment);
54315431
}
54325432

54335433
DartObjectImpl? _topLevelVar(String variableName) {
54345434
var element = findElement2.topVar(variableName);
5435-
var constFragment = element.firstFragment as ConstVariableFragment;
5435+
var constFragment = element.firstFragment as VariableFragmentImpl;
54365436
return _evaluationResult(constFragment);
54375437
}
54385438
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class B extends A {
281281
assertErrorsInResolvedUnit(result, []);
282282

283283
var bElement = findElement2.field('b');
284-
var bFragment = bElement.firstFragment as ConstVariableFragment;
284+
var bFragment = bElement.firstFragment as VariableFragmentImpl;
285285
var bValue = bFragment.evaluationResult as DartObjectImpl;
286286
var superFields = bValue.getField(GenericState.SUPERCLASS_FIELD);
287287
expect(superFields!.getField('f1')!.toBoolValue(), false);

0 commit comments

Comments
 (0)