Skip to content

Commit

Permalink
dart-lang#2485. Add is T constant evaluation tests (dart-lang#2501)
Browse files Browse the repository at this point in the history
Add `is T` constant evaluation tests
  • Loading branch information
sgrekhov committed Feb 15, 2024
1 parent a2e21a4 commit 1c67529
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 7 deletions.
8 changes: 5 additions & 3 deletions Language/Expressions/Constants/as_type_A01_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ typedef IntET1Alias = IntET1;
typedef IntET2Alias = IntET2;

void foo() {}

void as() {}
int bar<T>(T t) => 42;

main() {
Expand All @@ -73,7 +73,8 @@ main() {
const fo7 = IntET2(1) as FutureOr<IntET1Alias>;

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

const d = 2 as dynamic;

Expand All @@ -86,6 +87,7 @@ main() {
Expect.identical(fo4, fo5);
Expect.identical(fo6, fo7);
Expect.identical(foo, f1);
Expect.identical(bar<int>, f2);
Expect.identical(as, f2);
Expect.identical(bar<int>, f3);
Expect.identical(2, d);
}
4 changes: 4 additions & 0 deletions Language/Expressions/Constants/as_type_A02_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,9 @@ main() {
const c19 = c as Object;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c20 = () {} as Function;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
4 changes: 0 additions & 4 deletions Language/Expressions/Constants/as_type_A02_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ main() {
const cIntExtension = IntExtension as Type;
// ^^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const F = () {} as Function;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const R1 = (void,) as Type;
// ^^^^
Expand Down
76 changes: 76 additions & 0 deletions Language/Expressions/Constants/is_type_A01_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 is 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 is 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 is num;
const c2 = (3.14 as num) is double;
const c3 = IntET1(1) is int;
const c4 = IntET2(1) is int;
const c5 = (IntET1(1),) is (int,);
const c6 = (IntET2(1),) is (int,);
const c7 = [IntET1(1)] is List<int>;
const c8 = [IntET2(1)] is List<num>;
const c9 = {IntET1(1)} is Set<num>;
const c10 = {IntET2(1)} is Set<num>;
const c11 = {IntET1(1): IntET1(2)} is Map<num, int>;
const c12 = {IntET1(1): IntET2(2)} is Map<num, int>;
const c13 = {IntET2(1): IntET1(2)} is Map<num, int>;
const c14 = {IntET2(1): IntET2(2)} is Map<num, int>;
const c15 = 1 is IntET1;
const c16 = 1 is IntET2;
const c17 = IntET1(1) is IntET2;
const c18 = IntET2(1) is IntET1;
const c19 = C() is Object;
const c20 = const C() is Object;
const c21 = () is ();
const c22 = (i: 1) is ({num i});

Expect.isTrue(c1);
Expect.isTrue(c2);
Expect.isTrue(c3);
Expect.isTrue(c4);
Expect.isTrue(c5);
Expect.isTrue(c6);
Expect.isTrue(c7);
Expect.isTrue(c8);
Expect.isTrue(c9);
Expect.isTrue(c10);
Expect.isTrue(c11);
Expect.isTrue(c12);
Expect.isTrue(c13);
Expect.isTrue(c14);
Expect.isTrue(c15);
Expect.isTrue(c16);
Expect.isTrue(c17);
Expect.isTrue(c18);
Expect.isTrue(c19);
Expect.isTrue(c20);
Expect.isTrue(c21);
Expect.isTrue(c22);
}
97 changes: 97 additions & 0 deletions Language/Expressions/Constants/is_type_A01_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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 is 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 is 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() {}
void as() {}
int bar<T>(T t) => 42;

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

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

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

const d = 2 is dynamic;

Expect.isTrue(c1);
Expect.isTrue(c2);
Expect.isTrue(c3);
Expect.isTrue(c4);
Expect.isTrue(c5);
Expect.isTrue(c6);
Expect.isTrue(c7);
Expect.isTrue(fo1);
Expect.isTrue(fo2);
Expect.isTrue(fo3);
Expect.isTrue(fo4);
Expect.isTrue(fo5);
Expect.isTrue(fo6);
Expect.isTrue(fo7);
Expect.isTrue(f1);
Expect.isTrue(f2);
Expect.isTrue(d);
}
52 changes: 52 additions & 0 deletions Language/Expressions/Constants/is_type_A01_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 is 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 is T` is a constant
/// expression if `T` is a constant type expression of type `FutureOr<void>` or
/// an extension type with a representation type `void`
/// @author sgrekhov22@gmail.com
/// @issue 54620
// SharedOptions=--enable-experiment=inline-class

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

extension type const VoidET(void _) {}

main() {
const c1 = 1 is VoidET;
const c2 = 2 is FutureOr<void>;
const c3 = (3,) is (VoidET,);
const c4 = (4,) is (FutureOr<void>,);
const c5 = (i: 5) is ({VoidET i});
const c6 = (i: 6) is ({FutureOr<void> i});

Expect.isTrue(c1);
Expect.isTrue(c2);
Expect.isTrue(c3);
Expect.isTrue(c4);
Expect.isTrue(c5);
Expect.isTrue(c6);
}
110 changes: 110 additions & 0 deletions Language/Expressions/Constants/is_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 is 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 is 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 is Type;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

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

const c1 = i is num;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c2 = (d as num) is double;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c3 = IntET1(1) is int;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c4 = IntET2(1) is int;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c5 = (i,) is (int,);
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c6 = (IntET1(1),) is (int,);
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c7 = (IntET2(1),) is (int,);
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c8 = [i] is List<int>;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c9 = [IntET1(1)] is List<int>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c10 = [IntET2(1)] is List<num>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c11 = {d} is Set<num>;
// ^
// [analyzer] unspecified
// [cfe] unspecified
const c12 = {IntET1(1)} is Set<num>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c13 = {IntET2(1)} is Set<num>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c14 = {IntET1(1): IntET1(2)} is Map<num, int>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c15 = {IntET2(1): IntET2(2)} is Map<num, int>;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c16 = IntET1(1) is IntET2;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c17 = IntET2(1) is IntET1;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const c19 = c is Object;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}
Loading

0 comments on commit 1c67529

Please sign in to comment.