Skip to content

Commit

Permalink
#2976. Add constant expressions tests.Part 3. (#3026)
Browse files Browse the repository at this point in the history
Add constant expressions tests.Part 3.
  • Loading branch information
sgrekhov authored Jan 8, 2025
1 parent 5d6454b commit 1387aed
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 right operand of a constant `e1 == e2` expression.
/// @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() {
const b1 = C(42) == .answer;
Expect.isTrue(b1);
const b2 = (MC(42) as M) == .answer;
Expect.isTrue(b2);
const b3 = E.e0 == .answer;
Expect.isTrue(b3);
const b4 = ET(42) == .answer;
Expect.isTrue(b4);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 compile-time error if a non-constant static
/// member shorthand expression is used in a right operand of a constant
/// `e1 == e2` expression.
/// @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 const 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() {
const b1 = C(42) == .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const b2 = (MC(42) as M) == .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const b3 = E.e0 == .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const b4 = ET(42) == .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 right operand of a constant `e1 != e2` expression.
/// @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() {
const b1 = C(42) != .answer;
Expect.isFalse(b1);
const b2 = (MC(42) as M) != .answer;
Expect.isFalse(b2);
const b3 = E.e0 != .answer;
Expect.isFalse(b3);
const b4 = ET(42) != .answer;
Expect.isFalse(b4);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 compile-time error if a non-constant static
/// member shorthand expression is used in a right operand of a constant
/// `e1 != e2` expression.
/// @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 const 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() {
const b1 = C(42) != .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const b2 = (MC(42) as M) != .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const b3 = E.e0 != .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const b4 = ET(42) != .answer;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 `e1 ? e2 : e3` expression.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

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

class C {
static const C one = const C(1);
static const C two = const C(2);
final int v;
const C(this.v);
}

mixin M on C {
static const M one = const MC(1);
static const M two = const MC(2);
}

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

enum E {
e1, e2;
static const E one = E.e1;
static const E two = E.e2;
}

extension type const ET(int v) {
static const ET one = const ET(1);
static const ET two = const ET(2);
}

main() {
const C c = 2 > 1 ? .one : .two;
Expect.identical(const C(1), c);
const M m = 2 < 1 ? .one : .two;
Expect.identical(const MC(2), m);
const E e = 2 > 1 ? .one : .two;
Expect.identical(E.e1, e);
const ET et = 2 < 1 ? .one : .two;
Expect.identical(const ET(2), et);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 a constant `e1 ? e2 : e3` expression.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

class C {
static C one = const C(1);
static const C two = const C(2);
final int v;
const C(this.v);
}

mixin M on C {
static M one = const MC(1);
static const M two = const MC(2);
}

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

enum E {
e1, e2;
static const E one = E.e1;
static E two = E.e2;
}

extension type const ET(int v) {
static const ET one = const ET(1);
static ET two = const ET(2);
}

main() {
const C c = 2 > 1 ? .one : .two;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const M m = 2 < 1 ? .one : .two;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const E e = 2 > 1 ? .one : .two;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const ET et = 2 < 1 ? .one : .two;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 `e1 ?? e2` expression.
/// @author [email protected]
// SharedOptions=--enable-experiment=enum-shorthands

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

class C {
static const C one = const C(1);
static const C? two = null;
final int v;
const C(this.v);
}

mixin M on C {
static const M one = const MC(1);
static const M? two = null;
}

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

enum E {
e1, e2;
static const E one = E.e1;
static const E? two = null;
}

extension type const ET(int v) {
static const ET one = const ET(1);
static const ET? two = null;
}

main() {
const C c = .two ?? .one;
Expect.identical(const C(1), c);
const M m = .two ?? .one;
Expect.identical(const MC(1), m);
const E e = .two ?? .one;
Expect.identical(E.e1, e);
const ET et = .two ?? .one;
Expect.identical(const ET(1), et);

const c2 = (C(1) as C?) ?? .one;
Expect.identical(const C(1), c2);
const m2 = (MC(1) as M?) ?? .one;
Expect.identical(const MC(1), m2);
const e2 = (E.e1 as E?) ?? .one;
Expect.identical(E.e1, e2);
const et2 = (ET(1) as ET?) ?? .one;
Expect.identical(const ET(1), et2);
}
Loading

0 comments on commit 1387aed

Please sign in to comment.