Fix for constant dependencies when opt-out depends on opt-in.

R=brianwilkerson@google.com

Change-Id: I31bb8073fa1059e2edf8903b5fa604afb0f19f50
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134571
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2020-02-06 00:55:20 +00:00 committed by commit-bot@chromium.org
parent 8439158668
commit fe0735f959
3 changed files with 66 additions and 36 deletions

View file

@ -312,8 +312,9 @@ class ConstantEvaluationEngine {
if (constant is ConstructorElement) {
constant = (constant as ConstructorElement).declaration;
}
if (constant is VariableElementImpl) {
Expression initializer = constant.constantInitializer;
if (constant is VariableElement) {
VariableElementImpl declaration = constant.declaration;
Expression initializer = declaration.constantInitializer;
if (initializer != null) {
initializer.accept(referenceFinder);
}

View file

@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/analysis/experiments.dart';
import 'package:analyzer/src/dart/element/element.dart';
@ -232,4 +233,66 @@ class A<T, U> {
'List<Never Function(Object?)>',
);
}
test_field_optIn_fromOptOut() async {
newFile('/test/lib/a.dart', content: r'''
class A {
static const foo = 42;
}
''');
await assertNoErrorsInCode(r'''
// @dart = 2.5
import 'a.dart';
const bar = A.foo;
''');
var bar = findElement.topVar('bar');
_assertIntValue(bar, 42);
}
test_topLevelVariable_optIn_fromOptOut() async {
newFile('/test/lib/a.dart', content: r'''
const foo = 42;
''');
await assertNoErrorsInCode(r'''
// @dart = 2.5
import 'a.dart';
const bar = foo;
''');
var bar = findElement.topVar('bar');
assertType(bar.type, 'int*');
_assertIntValue(bar, 42);
}
test_topLevelVariable_optOut2() async {
newFile('/test/lib/a.dart', content: r'''
const a = 42;
''');
newFile('/test/lib/b.dart', content: r'''
import 'a.dart';
const b = a;
''');
await assertNoErrorsInCode(r'''
// @dart = 2.5
import 'b.dart';
const c = b;
''');
var c = findElement.topVar('c');
assertType(c.type, 'int*');
_assertIntValue(c, 42);
}
void _assertIntValue(VariableElement element, int value) {
expect(element.constantValue.toIntValue(), value);
}
}

View file

@ -334,40 +334,6 @@ main(A a) {
_assertLegacyMember(element, _import_a.method('+'));
}
test_const_field() async {
newFile('/test/lib/a.dart', content: r'''
class A {
static const foo = 42;
}
''');
await assertNoErrorsInCode(r'''
// @dart = 2.5
import 'a.dart';
const bar = A.foo;
''');
var bar = findElement.topVar('bar');
assertType(bar.type, 'int*');
expect(bar.constantValue.toIntValue(), 42);
}
test_const_topLevelVariable() async {
newFile('/test/lib/a.dart', content: r'''
const foo = 42;
''');
await assertNoErrorsInCode(r'''
// @dart = 2.5
import 'a.dart';
const bar = foo;
''');
var bar = findElement.topVar('bar');
assertType(bar.type, 'int*');
expect(bar.constantValue.toIntValue(), 42);
}
test_functionExpressionInvocation() async {
newFile('/test/lib/a.dart', content: r'''
int Function(int, int?)? foo;