Skip to content

Commit

Permalink
Fixes #3029. Add more tests for constants (#3045)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov authored Jan 14, 2025
1 parent 1398986 commit bc9b1da
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 There are no other constant expressions.
///
/// @description Checks that a null-check operator cannot be used in a constant
/// expression.
/// @author sgrekhov22@gmail.com
/// @issue 59800
const one = 1!;
// ^
// [analyzer] unspecified
// [cfe] unspecified

const int? two = 2;
const int nonNullableTwo = two!;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const int three = 1 + two!;
// ^
// [analyzer] unspecified
// [cfe] unspecified

main() {
print(one);
print(two);
print(nonNullableTwo);
print(three);
}
42 changes: 42 additions & 0 deletions Language/Expressions/Constants/string_length_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 The potentially constant expressions and constant expressions are
/// the following:
/// ...
/// • An expression of the form `e.length` is potentially constant if `e` is a
/// potentially constant expression. It is further constant if `e` is a
/// constant expression that evaluates to an instance of [String], such that
/// `length` denotes an instance getter invocation.
///
/// @description Checks that it is a compile-time error to use a null-aware
/// operator `?.` in a constant `String.length` expression.
/// @author sgrekhov22@gmail.com
/// @issue 59904
const c1 = "x"?.length;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
const String? one = "1";
const int? c2 = one?.length;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
const String? three = null;
const int? c3 = three?.length;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
const int? c4 = three == null ? null : three?.length;
// ^^
// [analyzer] unspecified
// [cfe] unspecified

main() {
print(c1);
print(c2);
print(c3);
print(c4);
}
30 changes: 30 additions & 0 deletions Language/Expressions/Constants/string_length_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 The potentially constant expressions and constant expressions are
/// the following:
/// ...
/// • An expression of the form `e.length` is potentially constant if `e` is a
/// potentially constant expression. It is further constant if `e` is a
/// constant expression that evaluates to an instance of [String], such that
/// `length` denotes an instance getter invocation.
///
/// @description Checks that it is a compile-time error to use a cascade member
/// access in a constant `String.length` expression.
/// @author sgrekhov22@gmail.com
const c1 = "x"..length;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
const String one = "1";
const c2 = one..length;
// ^^
// [analyzer] unspecified
// [cfe] unspecified

main() {
print(c1);
print(c2);
}

0 comments on commit bc9b1da

Please sign in to comment.