Skip to content

Commit 0897b95

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Analyzer: Support const Symbol('>>>')
Bug: #44908 Change-Id: I7af5587317da43f79f043dd7e92976e1011935ca Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/185242 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent d763cd5 commit 0897b95

File tree

7 files changed

+34
-11
lines changed

7 files changed

+34
-11
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class _ConstantWalker extends graph.DependencyWalker<_ConstantNode> {
7777
}
7878

7979
ConstantEvaluationEngine _getEvaluationEngine(_ConstantNode node) {
80-
return ConstantEvaluationEngine(declaredVariables);
80+
return ConstantEvaluationEngine(
81+
declaredVariables, experimentStatus.triple_shift);
8182
}
8283

8384
_ConstantNode _getNode(ConstantEvaluationTarget constant) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/declared_variables.dart';
6+
import 'package:analyzer/dart/analysis/features.dart';
67
import 'package:analyzer/dart/ast/ast.dart';
78
import 'package:analyzer/dart/ast/token.dart';
89
import 'package:analyzer/dart/ast/visitor.dart';
@@ -65,7 +66,8 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
6566
this._typeSystem,
6667
this._typeProvider,
6768
this.declaredVariables,
68-
) : _evaluationEngine = ConstantEvaluationEngine(declaredVariables);
69+
) : _evaluationEngine = ConstantEvaluationEngine(declaredVariables,
70+
_currentLibrary.featureSet.isEnabled(Feature.triple_shift));
6971

7072
bool get _isNonNullableByDefault => _currentLibrary.isNonNullableByDefault;
7173

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ class ConstantEvaluationEngine {
5858
/// The set of variables declared on the command line using '-D'.
5959
final DeclaredVariables _declaredVariables;
6060

61+
/// Whether the `triple_shift` experiment is enabled.
62+
final bool _isTripleShiftExperimentEnabled;
63+
6164
/// Initialize a newly created [ConstantEvaluationEngine].
6265
///
6366
/// [declaredVariables] is the set of variables declared on the command
6467
/// line using '-D'.
65-
ConstantEvaluationEngine(DeclaredVariables declaredVariables)
68+
ConstantEvaluationEngine(
69+
DeclaredVariables declaredVariables, this._isTripleShiftExperimentEnabled)
6670
: _declaredVariables = declaredVariables;
6771

6872
/// Check that the arguments to a call to fromEnvironment() are correct. The
@@ -127,7 +131,14 @@ class ConstantEvaluationEngine {
127131
return false;
128132
}
129133
var name = argumentValues[0]?.toStringValue();
130-
return name != null && isValidPublicSymbol(name);
134+
if (name == null) {
135+
return false;
136+
}
137+
// TODO(srawlins): If the argument is '>>>' but triple-shift is not enabled,
138+
// report a different error indicating that the triple-shift experiment
139+
// should be enabled, or a minimum SDK version set, when one is declared.
140+
return isValidPublicSymbol(name) ||
141+
(_isTripleShiftExperimentEnabled && name == '>>>');
131142
}
132143

133144
/// Compute the constant value associated with the given [constant].
@@ -414,11 +425,11 @@ class ConstantEvaluationEngine {
414425

415426
constructor = followConstantRedirectionChain(constructor);
416427
InterfaceType definingType = constructor.returnType;
417-
ClassElement definingClass = constructor.enclosingElement;
418428
if (constructor.isFactory) {
419429
// We couldn't find a non-factory constructor.
420430
// See if it's because we reached an external const factory constructor
421431
// that we can emulate.
432+
ClassElement definingClass = constructor.enclosingElement;
422433
if (constructor.name == "fromEnvironment") {
423434
if (!checkFromEnvironmentArguments(
424435
library, arguments, argumentValues, namedValues, definingType)) {
@@ -479,8 +490,6 @@ class ConstantEvaluationEngine {
479490
definingType,
480491
);
481492
}
482-
var constructorBase = constructor.declaration as ConstructorElementImpl;
483-
var initializers = constructorBase.constantInitializers;
484493

485494
var fieldMap = HashMap<String, DartObjectImpl>();
486495

@@ -597,6 +606,8 @@ class ConstantEvaluationEngine {
597606
lexicalEnvironment: parameterMap,
598607
substitution: Substitution.fromInterfaceType(definingType),
599608
);
609+
var constructorBase = constructor.declaration as ConstructorElementImpl;
610+
var initializers = constructorBase.constantInitializers;
600611
String? superName;
601612
NodeList<Expression>? superArguments;
602613
for (var i = 0; i < initializers.length; i++) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,8 +1676,8 @@ abstract class InstanceState {
16761676
return BoolState.from(leftValue | rightValue);
16771677
}
16781678

1679-
/// Return the result of invoking the '&gt;&gt;' operator on this object with
1680-
/// the [rightOperand].
1679+
/// Return the result of invoking the '&gt;&gt;&gt;' operator on this object
1680+
/// with the [rightOperand].
16811681
///
16821682
/// Throws an [EvaluationException] if the operator is not appropriate for an
16831683
/// object of this kind.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/analysis/declared_variables.dart';
6+
import 'package:analyzer/dart/analysis/features.dart';
67
import 'package:analyzer/dart/ast/ast.dart';
78
import 'package:analyzer/error/error.dart';
89
import 'package:analyzer/error/listener.dart';
@@ -113,6 +114,7 @@ class ConstantEvaluator {
113114
var result = expression.accept(ConstantVisitor(
114115
ConstantEvaluationEngine(
115116
DeclaredVariables(),
117+
_library.featureSet.isEnabled(Feature.triple_shift),
116118
),
117119
_library,
118120
errorReporter));

pkg/analyzer/lib/src/lint/linter.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ class LinterContextImpl implements LinterContext {
369369
);
370370

371371
var visitor = ConstantVisitor(
372-
ConstantEvaluationEngine(declaredVariables),
372+
ConstantEvaluationEngine(
373+
declaredVariables, isEnabled(Feature.triple_shift)),
373374
libraryElement,
374375
errorReporter,
375376
);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +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+
import 'package:analyzer/dart/analysis/features.dart';
56
import 'package:analyzer/error/error.dart';
67
import 'package:analyzer/error/listener.dart';
78
import 'package:analyzer/src/dart/analysis/experiments.dart';
@@ -1042,7 +1043,11 @@ class ConstantVisitorTestSupport extends PubPackageResolutionTest
10421043
}) {
10431044
var expression = findNode.topVariableDeclarationByName(name).initializer!;
10441045

1045-
var source = this.result.unit!.declaredElement!.source;
1046+
var unit = this.result.unit;
1047+
if (unit == null) {
1048+
throw StateError('analysis result unit is null');
1049+
}
1050+
var source = unit.declaredElement!.source;
10461051
var errorListener = GatheringErrorListener();
10471052
var errorReporter = ErrorReporter(
10481053
errorListener,
@@ -1054,6 +1059,7 @@ class ConstantVisitorTestSupport extends PubPackageResolutionTest
10541059
ConstantVisitor(
10551060
ConstantEvaluationEngine(
10561061
DeclaredVariables.fromMap(declaredVariables),
1062+
unit.featureSet.isEnabled(Feature.triple_shift),
10571063
),
10581064
this.result.libraryElement as LibraryElementImpl,
10591065
errorReporter,

0 commit comments

Comments
 (0)