2d330c7985
Change-Id: Ia1ad96cd77d94c096bf8a10aa0377f37c79ed398 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/195560 Commit-Queue: Kallen Tu <kallentu@google.com> Reviewed-by: Dmitry Stefantsov <dmitryas@google.com> Reviewed-by: Jake Macdonald <jakemac@google.com>
45 lines
1.1 KiB
Dart
45 lines
1.1 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 string usage with const functions.
|
|
|
|
// SharedOptions=--enable-experiment=const-functions
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
const String str = "str";
|
|
const var1 = str[2];
|
|
// ^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
|
|
const var2 = fn();
|
|
// ^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
fn() {
|
|
String local = "str";
|
|
return local[0];
|
|
}
|
|
|
|
const var3 = "str"[0];
|
|
// ^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
|
|
const var4 = fn2();
|
|
// ^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
fn2() {
|
|
try {
|
|
var x = str[-1];
|
|
} on RangeError {
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
Expect.equals(var1, 'r');
|
|
Expect.equals(var2, 's');
|
|
Expect.equals(var3, 's');
|
|
Expect.equals(var4, 2);
|
|
}
|