mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 04:27:17 +00:00
c906133967
Change-Id: Id03250e5eeb38d9c69b629c239e573a1d838820f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/190980 Reviewed-by: Jake Macdonald <jakemac@google.com> Reviewed-by: Dmitry Stefantsov <dmitryas@google.com> Commit-Queue: Kallen Tu <kallentu@google.com>
94 lines
2.5 KiB
Dart
94 lines
2.5 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 local function usage, some having references to other constant values.
|
|
|
|
// SharedOptions=--enable-experiment=const-functions
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
int function1() {
|
|
int add(int a, int b) => a + b;
|
|
const value = add(10, 2);
|
|
// ^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
return value;
|
|
}
|
|
|
|
const constTwo = 2;
|
|
int function2() {
|
|
int addTwo(int a) {
|
|
int b = a + constTwo;
|
|
return b;
|
|
}
|
|
|
|
const value = addTwo(2);
|
|
// ^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
return value;
|
|
}
|
|
|
|
int function3() {
|
|
int addTwoReturn(int a) => a + constTwo;
|
|
const value = addTwoReturn(3);
|
|
// ^^^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
return value;
|
|
}
|
|
|
|
int function4() {
|
|
const localTwo = 2;
|
|
int addTwo(int a) => a + localTwo;
|
|
const value = addTwo(20);
|
|
// ^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
return value;
|
|
}
|
|
|
|
int function5() {
|
|
T typeFn<T>(T a) => a;
|
|
const value = typeFn(3);
|
|
// ^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
return value;
|
|
}
|
|
|
|
int function6() {
|
|
int optionalFn([int a = 0]) => a;
|
|
const value = optionalFn(1);
|
|
// ^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
return value;
|
|
}
|
|
|
|
int function7() {
|
|
int namedFn({int a = 0}) => a;
|
|
const value = namedFn(a: 2);
|
|
// ^^^^^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
return value;
|
|
}
|
|
|
|
int function8() {
|
|
int add(int a, int b) => a + b;
|
|
const value = add(1, 1);
|
|
// ^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
const value1 = add(2, 3);
|
|
// ^^^^^^^^^
|
|
// [analyzer] COMPILE_TIME_ERROR.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
|
|
return value + value1;
|
|
}
|
|
|
|
void main() {
|
|
Expect.equals(function1(), 12);
|
|
Expect.equals(function2(), 4);
|
|
Expect.equals(function3(), 5);
|
|
Expect.equals(function4(), 22);
|
|
Expect.equals(function5(), 3);
|
|
Expect.equals(function6(), 1);
|
|
Expect.equals(function7(), 2);
|
|
Expect.equals(function8(), 7);
|
|
}
|