5c36a30de2
Change-Id: Ie090cbf47d1e5be82e59a3f782dea8b4c1c75c91 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196380 Reviewed-by: Dmitry Stefantsov <dmitryas@google.com> Reviewed-by: Jake Macdonald <jakemac@google.com> Reviewed-by: Bob Nystrom <rnystrom@google.com> Commit-Queue: Kallen Tu <kallentu@google.com>
62 lines
1.4 KiB
Dart
62 lines
1.4 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_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int fn() => const A(1).y;
|
|
|
|
const var2 = fn2();
|
|
// ^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int fn2() {
|
|
var x = const A(1);
|
|
return x.y;
|
|
}
|
|
|
|
const var3 = const A(1).y;
|
|
// ^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
|
|
class B extends A {
|
|
const B(int x) : super(x);
|
|
}
|
|
|
|
const var4 = fn4();
|
|
// ^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
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_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
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);
|
|
}
|