mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 04:27:17 +00:00
237c6eef9e
* FunctionInvocation and LocalFunctionInvocation nodes are now supported. * Handling of List getters is added to InstanceGet. * Removed incorrect constant evaluation of .hashCode and .runtimeType for List constants. Issue: https://github.com/dart-lang/sdk/issues/45340 Change-Id: I73f3e64e805c0753325463b3e777a6c1b7d0b5fb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/198181 Reviewed-by: Johnni Winther <johnniwinther@google.com> Commit-Queue: Alexander Markov <alexmarkov@google.com>
165 lines
4.1 KiB
Dart
165 lines
4.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 lists with const functions.
|
|
|
|
// SharedOptions=--enable-experiment=const-functions
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
const firstVar = firstFn();
|
|
// ^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int firstFn() {
|
|
const List<int> x = [1, 2];
|
|
return x.first;
|
|
}
|
|
|
|
const firstCatchVar = firstCatchFn();
|
|
// ^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int firstCatchFn() {
|
|
try {
|
|
const List<int> x = [];
|
|
var v = x.first;
|
|
} on StateError {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
const isEmptyVar = isEmptyFn();
|
|
// ^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
bool isEmptyFn() {
|
|
const List<int> x = [1, 2];
|
|
return x.isEmpty;
|
|
}
|
|
|
|
const isNotEmptyVar = isNotEmptyFn();
|
|
// ^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
bool isNotEmptyFn() {
|
|
const List<int> x = [1, 2];
|
|
return x.isNotEmpty;
|
|
}
|
|
|
|
const lastVar = lastFn();
|
|
// ^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int lastFn() {
|
|
const List<int> x = [1, 2];
|
|
return x.last;
|
|
}
|
|
|
|
const lastCatchVar = lastCatchFn();
|
|
// ^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int lastCatchFn() {
|
|
try {
|
|
const List<int> x = [];
|
|
var v = x.last;
|
|
} on StateError {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
const lengthVar = lengthFn();
|
|
// ^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int lengthFn() {
|
|
const List<int> x = [1, 2];
|
|
return x.length;
|
|
}
|
|
|
|
const singleVar = singleFn();
|
|
// ^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int singleFn() {
|
|
const List<int> x = [1];
|
|
return x.single;
|
|
}
|
|
|
|
const singleCatchVar = singleCatchFn();
|
|
// ^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int singleCatchFn() {
|
|
try {
|
|
const List<int> x = [];
|
|
var v = x.single;
|
|
} on StateError {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
const singleCatchVar2 = singleCatchFn2();
|
|
// ^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int singleCatchFn2() {
|
|
try {
|
|
const List<int> x = [1, 2];
|
|
var v = x.single;
|
|
} on StateError {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
const getWithIndexVar = getWithIndexFn();
|
|
// ^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int getWithIndexFn() {
|
|
const List<int> x = [1];
|
|
return x[0];
|
|
}
|
|
|
|
const rangeErrorCatchVar = rangeErrorCatchFn();
|
|
// ^^^^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
int rangeErrorCatchFn() {
|
|
try {
|
|
const List<int> x = [1];
|
|
var v = x[1];
|
|
} on RangeError {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
const mutableListVar = mutableList();
|
|
// ^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
List<int> mutableList() {
|
|
List<int> x = [1, 2];
|
|
return x;
|
|
}
|
|
|
|
const mutableListAddVar = mutableListAdd();
|
|
// ^^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
List<int> mutableListAdd() {
|
|
List<int> x = [1, 2];
|
|
x.add(3);
|
|
return x;
|
|
}
|
|
|
|
void main() {
|
|
Expect.equals(firstVar, 1);
|
|
Expect.equals(firstCatchVar, 0);
|
|
Expect.equals(isEmptyVar, false);
|
|
Expect.equals(isNotEmptyVar, true);
|
|
Expect.equals(lastVar, 2);
|
|
Expect.equals(lastCatchVar, 0);
|
|
Expect.equals(lengthVar, 2);
|
|
Expect.equals(singleVar, 1);
|
|
Expect.equals(singleCatchVar, 0);
|
|
Expect.equals(singleCatchVar2, 0);
|
|
Expect.equals(getWithIndexVar, 1);
|
|
Expect.equals(rangeErrorCatchVar, 0);
|
|
Expect.equals(mutableListVar, const [1, 2]);
|
|
Expect.equals(mutableListAddVar, const [1, 2, 3]);
|
|
}
|