-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...ge/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t05.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2025, 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. | ||
|
||
/// @assertion Any expression that appears within the initializer list of a | ||
/// constant constructor must be a potentially constant expression, or a | ||
/// compile-time error occurs. | ||
/// | ||
/// A potentially constant expression is an expression E that would be a valid | ||
/// constant expression if all formal parameters of E's immediately enclosing | ||
/// constant constructor were treated as compile-time constants that were | ||
/// guaranteed to evaluate to an integer, boolean or string value as required | ||
/// by their immediately enclosing superexpression. | ||
/// | ||
/// @description Checks that it is a compile-time error when a constant | ||
/// constructor's initializer list contains a use of a non-constant. | ||
/// @author sgrekhov22@gmail.com | ||
final String zero = "0"; | ||
|
||
class C { | ||
static final name = "name"; | ||
final int v; | ||
|
||
const C.fromObject(Object o) : v = o.toString().length; | ||
// ^^^^^^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
const C.fromGlobal() : v = zero.length; | ||
// ^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
const C.fromName() : v = name.length; | ||
// ^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
|
||
main() { | ||
print(C); | ||
} |
38 changes: 38 additions & 0 deletions
38
...ge/Classes/Constructors/Constant_Constructors/not_a_constant_in_initializer_list_t06.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2025, 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. | ||
|
||
/// @assertion Any expression that appears within the initializer list of a | ||
/// constant constructor must be a potentially constant expression, or a | ||
/// compile-time error occurs. | ||
/// | ||
/// A potentially constant expression is an expression E that would be a valid | ||
/// constant expression if all formal parameters of E's immediately enclosing | ||
/// constant constructor were treated as compile-time constants that were | ||
/// guaranteed to evaluate to an integer, boolean or string value as required | ||
/// by their immediately enclosing superexpression. | ||
/// | ||
/// @description Checks that it is not an error to use `String.length` in a | ||
/// constant constructor's initializer list if the string is a constant. | ||
/// @author sgrekhov22@gmail.com | ||
import '../../../../Utils/expect.dart'; | ||
|
||
const String zero = "0"; | ||
|
||
class C { | ||
static const name = "name"; | ||
final int v; | ||
|
||
const C() : v = "".length; | ||
const C.fromString(String s) : v = s.length; | ||
const C.fromGlobal() : v = zero.length; | ||
const C.fromName() : v = name.length; | ||
} | ||
|
||
main() { | ||
Expect.equals(0, const C().v); | ||
Expect.equals(3, const C.fromString("123").v); | ||
Expect.equals(1, const C.fromGlobal().v); | ||
Expect.equals(4, const C.fromName().v); | ||
} |