Skip to content

Commit

Permalink
#2976. Add constant expressions tests.Part 2. (#3025)
Browse files Browse the repository at this point in the history
Add constant expressions tests.Part 2.
  • Loading branch information
sgrekhov authored Jan 8, 2025
1 parent 43b9788 commit 5d6454b
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2024, 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 A static member shorthand expression should be a potentially
/// constant expression if the corresponding explicit static member plus
/// selectors expression would be, which currently means that it's a potentially
/// constant expression if and only if it's a constant expression.
///
/// @description Checks that a qualified reference to a static constant variable
/// that is not qualified by a deferred prefix, is a potentially constant and
/// constant expression
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';
import 'shorthand_lib.dart' as p;

main() {
const p.C c = .answer;
Expect.identical(42, c.t);

const p.M m = .answer;
Expect.identical(42, m.t);

const p.E e = .answer;
Expect.identical(42, e.t);

const p.ET et = .answer;
Expect.identical(42, et.t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2024, 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 A static member shorthand expression should be a potentially
/// constant expression if the corresponding explicit static member plus
/// selectors expression would be, which currently means that it's a potentially
/// constant expression if and only if it's a constant expression.
///
/// @description Checks that a static member shorthand expression can be used in
/// the initializer list of a constant constructor.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class A {
final int id;
const A(this.id);
static const A answer = const A(42);
}

class C {
final A a;
const C(this.a);
const C.fromAnswer() : a = .answer;
}

main() {
Expect.identical(42, C.fromAnswer().a.id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2024, 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 A static member shorthand expression should be a potentially
/// constant expression if the corresponding explicit static member plus
/// selectors expression would be, which currently means that it's a potentially
/// constant expression if and only if it's a constant expression.
///
/// @description Checks that it is a compile-time error if a non-constant static
/// member shorthand expression is used in the initializer list of the constant
/// constructor.
/// @author [email protected]
/// @issue 59804
// SharedOptions=--enable-experiment=enum-shorthands

class A {
int id;
A(this.id);
static A answer = A(42);
}

class C {
final A a;
const C(this.a);
const C.fromAnswer() : a = .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
print(C);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2024, 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 A static member shorthand expression should be a potentially
/// constant expression if the corresponding explicit static member plus
/// selectors expression would be, which currently means that it's a potentially
/// constant expression if and only if it's a constant expression.
///
/// @description Checks that a static member shorthand expression can be used in
/// a constant list literal.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class C {
static const C answer = const C(42);
final int v;
const C(this.v);
}

mixin M on C {
static const M answer = const MC(42);
}

class MC extends C with M {
const MC(super.v);
}

enum E {
e0;
static const E answer = E.e0;
}

extension type const ET(int v) {
static const ET answer = const ET(42);
}

main() {
Expect.identical(const [C(42)], const <C>[.answer]);
Expect.identical(const <M>[MC(42)], const <M>[.answer]);
Expect.identical(const [E.e0], const <E>[.answer]);
Expect.identical(const [ET(42)], const <ET>[.answer]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024, 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 A static member shorthand expression should be a potentially
/// constant expression if the corresponding explicit static member plus
/// selectors expression would be, which currently means that it's a potentially
/// constant expression if and only if it's a constant expression.
///
/// @description Checks that it is a compile-time error to use a non-constant
/// static member shorthand expression in a constant list literal.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

class C {
static C answer = const C(42);
final int v;
const C(this.v);
}

mixin M on C {
static M answer = const MC(42);
}

class MC extends C with M {
const MC(super.v);
}

enum E {
e0;
static E answer = E.e0;
}

extension type const ET(int v) {
static ET answer = const ET(42);
}

main() {
print(const <C>[.answer]);
// ^
// [analyzer] unspecified
// [cfe] unspecified
print(const <M>[.answer]);
// ^
// [analyzer] unspecified
// [cfe] unspecified
print(const <E>[.answer]);
// ^
// [analyzer] unspecified
// [cfe] unspecified
print(const <ET>[.answer]);
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2024, 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 A static member shorthand expression should be a potentially
/// constant expression if the corresponding explicit static member plus
/// selectors expression would be, which currently means that it's a potentially
/// constant expression if and only if it's a constant expression.
///
/// @description Checks that a static member shorthand expression can be used in
/// a constant set literal.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class C {
static const C answer = const C(42);
final int v;
const C(this.v);
}

mixin M on C {
static const M answer = const MC(42);
}

class MC extends C with M {
const MC(super.v);
}

enum E {
e0;
static const E answer = E.e0;
}

extension type const ET(int v) {
static const ET answer = const ET(42);
}

main() {
Expect.identical(const {C(42)}, const <C>{.answer});
Expect.identical(const <M>{MC(42)}, const <M>{.answer});
Expect.identical(const {E.e0}, const <E>{.answer});
Expect.identical(const {ET(42)}, const <ET>{.answer});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024, 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 A static member shorthand expression should be a potentially
/// constant expression if the corresponding explicit static member plus
/// selectors expression would be, which currently means that it's a potentially
/// constant expression if and only if it's a constant expression.
///
/// @description Checks that it is a compile-time error to use a non-constant
/// static member shorthand expression in a constant set literal.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

class C {
static C answer = const C(42);
final int v;
const C(this.v);
}

mixin M on C {
static M answer = const MC(42);
}

class MC extends C with M {
const MC(super.v);
}

enum E {
e0;
static E answer = E.e0;
}

extension type const ET(int v) {
static ET answer = const ET(42);
}

main() {
print(const <C>{.answer});
// ^
// [analyzer] unspecified
// [cfe] unspecified
print(const <M>{.answer});
// ^
// [analyzer] unspecified
// [cfe] unspecified
print(const <E>{.answer});
// ^
// [analyzer] unspecified
// [cfe] unspecified
print(const <ET>{.answer});
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024, 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 A static member shorthand expression should be a potentially
/// constant expression if the corresponding explicit static member plus
/// selectors expression would be, which currently means that it's a potentially
/// constant expression if and only if it's a constant expression.
///
/// @description Checks that a static member shorthand expression can be used in
/// a constant map literal.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

import '../../Utils/expect.dart';

class C {
static const C answer = const C(42);
final int v;
const C(this.v);
}

mixin M on C {
static const M answer = const MC(42);
}

class MC extends C with M {
const MC(super.v);
}

enum E {
e0;

static const E answer = E.e0;
}

extension type const ET(int v) {
static const ET answer = const ET(42);
}

main() {
Expect.identical(const {"key": C(42)}, const <String, C>{"key": .answer});
Expect.identical(
const <M, String>{MC(42): "value"},
const <M, String>{.answer: "value"},
);
Expect.identical(const {"key": E.e0}, const <String, E>{"key": .answer});
Expect.identical(
const {ET(42): "value"},
const <ET, String>{.answer: "value"},
);
}
Loading

0 comments on commit 5d6454b

Please sign in to comment.