Skip to content

Commit

Permalink
dart-lang#2485. Add constants as T tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Jan 17, 2024
1 parent c790e10 commit 38fb57a
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Language/Expressions/Constants/as_type_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 constant expression is an expression whose value can never
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// ...
/// • An expression of the form e as T is potentially constant if e is a
/// potentially constant expression and T is a constant type expression, and
/// it is further constant if e is constant.
///
/// @description Checks that an expression of the form `e as T` is a constant
/// expression if `e` is a constant expression and `T` is a constant type
/// expression
/// @author sgrekhov22@gmail.com
/// @issue 54620
// SharedOptions=--enable-experiment=inline-class

import "../../../Utils/expect.dart";

class C {
const C();
}

extension type const IntET1(int _) {}
extension type const IntET2(int _) implements int {}

main() {
const c1 = 1 as num;
const c2 = (3.14 as num) as double;
const c3 = IntET1(1) as int;
const c4 = IntET2(1) as int;
const c5 = (IntET1(1),) as (int,);
const c6 = (IntET2(1),) as (int,);
const c7 = [IntET1(1)] as List<int>;
const c8 = [IntET2(1)] as List<num>;
const c9 = {IntET1(1)} as Set<num>;
const c10 = {IntET2(1)} as Set<num>;
const c11 = {IntET1(1): IntET1(2)} as Map<num, int>;
const c12 = {IntET1(1): IntET2(2)} as Map<num, int>;
const c13 = {IntET2(1): IntET1(2)} as Map<num, int>;
const c14 = {IntET2(1): IntET2(2)} as Map<num, int>;
const c15 = 1 as IntET1;
const c16 = 1 as IntET2;
const c17 = IntET1(1) as IntET2;
const c18 = IntET2(1) as IntET1;
const c19 = C() as Object;
const c20 = const C() as Object;

Expect.identical(1, c1);
Expect.identical(3.14, c2);
Expect.identical(1, c3);
Expect.identical(1, c4);
Expect.identical(const (1,), c5);
Expect.identical(const (1,), c6);
Expect.identical(const [1], c7);
Expect.identical(const [1], c8);
Expect.identical(const {1}, c9);
Expect.identical(const {1}, c10);
Expect.identical(const {1: 2}, c11);
Expect.identical(const {1: 2}, c12);
Expect.identical(const {1: 2}, c13);
Expect.identical(const {1: 2}, c14);
Expect.identical(1, c15);
Expect.identical(1, c16);
Expect.identical(1, c17);
Expect.identical(1, c18);
Expect.identical(const C(), c19);
Expect.identical(const C(), c20);
}
91 changes: 91 additions & 0 deletions Language/Expressions/Constants/as_type_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 constant expression is an expression whose value can never
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// ...
/// • An expression of the form e as T is potentially constant if e is a
/// potentially constant expression and T is a constant type expression, and
/// it is further constant if e is constant.
/// ...
/// A constant type expression is one of:
/// • An simple or qualified identifier denoting a type declaration (a type
/// alias, class or mixin declaration) that is not qualified by a deferred
/// prefix, optionally followed by type arguments of the form <T1, ..., Tn>
/// where T1, ..., Tn are constant type expressions.
/// • A type of the form FutureOr<T> where T is a constant type expression.
/// • A function type R Function<typeParameters>(argumentTypes) (where
/// R and <typeParameters> may be omitted) and where R, typeParameters
/// and argumentTypes (if present) contain only constant type expressions.
/// • The type void.
/// • The type dynamic
///
/// @description Checks that an expression of the form `e as T` is a constant
/// expression if `e` is a constant expression and `T` is a constant type
/// expression. Test different constant type expressions
/// @author sgrekhov22@gmail.com
/// @issue 54636
// SharedOptions=--enable-experiment=inline-class

import "dart:async";
import "../../../Utils/expect.dart";

mixin M {}

class C<T> with M {
const C();
}

typedef CNumAlias = C<num>;

enum E {
e1, e2;
}

extension type const IntET1(int _) {}
extension type const IntET2(int _) implements int {}

typedef IntET1Alias = IntET1;
typedef IntET2Alias = IntET2;

void foo() {}

int bar<T>(T t) => 42;

main() {
const c1 = C() as C;
const c2 = C() as M;
const c3 = E.e1 as E;
const c4 = C<int>() as C<num>;
const c5 = C<int>() as CNumAlias;
const c6 = IntET1(1) as IntET2Alias;
const c7 = IntET2(1) as IntET1Alias;

const fo1 = C() as FutureOr<C>;
const fo2 = C() as FutureOr<M>;
const fo3 = E.e1 as FutureOr<E>;
const fo4 = C<int>() as FutureOr<C<num>>;
const fo5 = C<int>() as FutureOr<CNumAlias>;
const fo6 = IntET1(1) as FutureOr<IntET2Alias>;
const fo7 = IntET2(1) as FutureOr<IntET1Alias>;

const f1 = foo as void Function();
const f2 = bar<int> as int Function<int>(int);

const d = 2 as dynamic;

Expect.identical(c1, c2);
Expect.identical(E.e1, c3);
Expect.identical(c4, c5);
Expect.identical(c6, c7);
Expect.identical(fo1, fo2);
Expect.identical(E.e1, fo3);
Expect.identical(fo4, fo5);
Expect.identical(fo6, fo7);
Expect.identical(foo, f1);
Expect.identical(bar<int>, f2);
Expect.identical(2, d);
}
110 changes: 110 additions & 0 deletions Language/Expressions/Constants/as_type_A02_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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 constant expression is an expression whose value can never
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// ...
/// • An expression of the form e as T is potentially constant if e is a
/// potentially constant expression and T is a constant type expression, and
/// it is further constant if e is constant.
///
/// @description Checks that an expression of the form `e as T` is not a
/// constant expression if `e` is not a constant expression
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=inline-class

class C {
C();
}

extension type IntET1(int _) {}
extension type IntET2(int _) implements int {}

test<T>() {
const c = T as Type;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
int i = 1;
var d = 3.14;
var c = C();

const c1 = i as num;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c2 = (d as num) as double;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c3 = IntET1(1) as int;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c4 = IntET2(1) as int;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c5 = (i,) as (int,);
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c6 = (IntET1(1),) as (int,);
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c7 = (IntET2(1),) as (int,);
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c8 = [i] as List<int>;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c9 = [IntET1(1)] as List<int>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c10 = [IntET2(1)] as List<num>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c11 = {d} as Set<num>;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c12 = {IntET1(1)} as Set<num>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c13 = {IntET2(1)} as Set<num>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c14 = {IntET1(1): IntET1(2)} as Map<num, int>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c15 = {IntET2(1): IntET2(2)} as Map<num, int>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c16 = IntET1(1) as IntET2;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c17 = IntET2(1) as IntET1;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c19 = c as Object;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
36 changes: 36 additions & 0 deletions Language/Expressions/Constants/as_type_A02_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 constant expression is an expression whose value can never
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// ...
/// • An expression of the form e as T is potentially constant if e is a
/// potentially constant expression and T is a constant type expression, and
/// it is further constant if e is constant.
///
/// @description Checks that an expression of the form `e as T` is not a
/// constant expression if `e` is not an expression
/// @author sgrekhov22@gmail.com
extension IntExtension on int {}

main() {
const Foo = int Function() as Function;
// ^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const Void = void as Type;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
const cIntExtension = IntExtension as Type;
// ^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const F = () {} as Function;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
55 changes: 55 additions & 0 deletions Language/Expressions/Constants/as_type_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 constant expression is an expression whose value can never
/// change, and that can be evaluated entirely at compile time.
/// A constant expression is one of the following:
/// ...
/// • An expression of the form e as T is potentially constant if e is a
/// potentially constant expression and T is a constant type expression, and
/// it is further constant if e is constant.
/// ...
/// A constant type expression is one of:
/// • An simple or qualified identifier denoting a type declaration (a type
/// alias, class or mixin declaration) that is not qualified by a deferred
/// prefix, optionally followed by type arguments of the form <T1, ..., Tn>
/// where T1, ..., Tn are constant type expressions.
/// • A type of the form FutureOr<T> where T is a constant type expression.
/// • A function type R Function<typeParameters>(argumentTypes) (where
/// R and <typeParameters> may be omitted) and where R, typeParameters
/// and argumentTypes (if present) contain only constant type expressions.
/// • The type void.
/// • The type dynamic
///
/// @description Checks that an expression of the form `e as T` is not a
/// constant expression if `T` is not a constant type expression
/// @author sgrekhov22@gmail.com
import "constants_lib.dart" deferred as p;

extension IntExtension on int {}

main() {
const Num = num;

const c1 = 1 as Num;
// ^^^
// [analyzer] unspecified
// [cfe] unspecified

const c2 = 1 as p.A;
// ^^^
// [analyzer] unspecified
// [cfe] unspecified

const c3 = 1 as IntExtension;
// ^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

const c4 = 1 as 42;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}
Loading

0 comments on commit 38fb57a

Please sign in to comment.