mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 04:27:17 +00:00
68d133d949
Within constant evaluation, mutable lists can be created and altered with `add`. Change-Id: I4ad86e3bea768844835dd732bff3ae135d6df8fb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/197280 Commit-Queue: Kallen Tu <kallentu@google.com> Reviewed-by: Jake Macdonald <jakemac@google.com> Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
96 lines
3.2 KiB
Dart
96 lines
3.2 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 erroneous list usage with const functions.
|
|
|
|
// SharedOptions=--enable-experiment=const-functions
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
const firstException = firstExceptionFn();
|
|
// ^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
// [cfe] Constant evaluation error:
|
|
int firstExceptionFn() {
|
|
const List<int> x = [];
|
|
return x.first;
|
|
}
|
|
|
|
const lastException = lastExceptionFn();
|
|
// ^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
// [cfe] Constant evaluation error:
|
|
int lastExceptionFn() {
|
|
const List<int> x = [];
|
|
return x.last;
|
|
}
|
|
|
|
const singleException = singleExceptionFn();
|
|
// ^^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
// [cfe] Constant evaluation error:
|
|
int singleExceptionFn() {
|
|
const List<int> x = [];
|
|
return x.single;
|
|
}
|
|
|
|
const singleExceptionMulti = singleExceptionMultiFn();
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
// [cfe] Constant evaluation error:
|
|
int singleExceptionMultiFn() {
|
|
const List<int> x = [1, 2];
|
|
return x.single;
|
|
}
|
|
|
|
const invalidProperty = invalidPropertyFn();
|
|
// ^^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int invalidPropertyFn() {
|
|
const List<int> x = [1, 2];
|
|
return x.invalidProperty;
|
|
// ^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_GETTER
|
|
// [cfe] The getter 'invalidProperty' isn't defined for the class 'List<int>'.
|
|
}
|
|
|
|
const getWithIndexException = getWithIndexExceptionFn();
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
// [cfe] Constant evaluation error:
|
|
int getWithIndexExceptionFn() {
|
|
const List<int> x = [1];
|
|
return x[1];
|
|
}
|
|
|
|
const getWithIndexException2 = getWithIndexExceptionFn2();
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
// [cfe] Constant evaluation error:
|
|
int getWithIndexExceptionFn2() {
|
|
const List<int> x = [1];
|
|
return x[-1];
|
|
}
|
|
|
|
const getWithIndexException3 = getWithIndexExceptionFn3();
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int getWithIndexExceptionFn3() {
|
|
const List<int> x = [1];
|
|
return x[0.1];
|
|
// ^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.ARGUMENT_TYPE_NOT_ASSIGNABLE
|
|
// [cfe] A value of type 'double' can't be assigned to a variable of type 'int'.
|
|
}
|
|
|
|
const constListAddException = constListAddExceptionFn();
|
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
// [cfe] Constant evaluation error:
|
|
List<int> constListAddExceptionFn() {
|
|
const List<int> x = [1, 2];
|
|
x.add(3);
|
|
return x;
|
|
}
|