Files
sdk/tests/language/const_functions/const_functions_instance_fields_test.dart
T
Kallen Tu 742ba5c20d [analyzer] Change the const evaluation result of variables to be Constant.
We rely on the result of the `ConstantVisitor` to indicate whether we have an error or a valid constant value.

This CL changes `evaluationResult` to be a `Constant` and changes error reporting to occur at a POE for evaluating a constant.

Last few chunks of cleaning up the constant evaluator, woo!

Change-Id: Icd41a4fcbab0626df36c6a83cd60ecbb59c2dcf0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324573
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
2023-09-22 16:13:10 +00:00

62 lines
1.3 KiB
Dart

// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// 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.
// Tests instance field usage with const functions.
// SharedOptions=--enable-experiment=const-functions
import "package:expect/expect.dart";
class A {
final int y;
const A(this.y);
}
const var1 = fn();
// ^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_METHOD_INVOCATION
int fn() => const A(1).y;
const var2 = fn2();
// ^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_METHOD_INVOCATION
int fn2() {
var x = const A(1);
return x.y;
}
const var3 = const A(1).y;
// ^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_PROPERTY_ACCESS
class B extends A {
const B(int x) : super(x);
}
const var4 = fn4();
// ^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_METHOD_INVOCATION
int fn4() => const B(1).y;
class C extends A {
@override
final int y = 2;
const C() : super(100);
}
const var5 = fn5();
// ^^^^^
// [analyzer] COMPILE_TIME_ERROR.CONST_EVAL_METHOD_INVOCATION
int fn5() => const C().y;
void main() {
Expect.equals(var1, 1);
Expect.equals(var2, 1);
Expect.equals(var3, 1);
Expect.equals(var4, 1);
Expect.equals(var5, 2);
}