Skip to content

Commit

Permalink
#2349. Add more tests for constant constructors of the extension types (
Browse files Browse the repository at this point in the history
#2352)

Add more tests for constant constructors of the extension types.

Co-authored-by: Erik Ernst <[email protected]>, fixing a couple of typos.
  • Loading branch information
sgrekhov authored Nov 9, 2023
1 parent 27ae4f9 commit d7f7514
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2023, 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 An extension type declaration DV named Name may declare one or
/// more constructors. A constructor which is declared in an extension type
/// declaration is also known as an extension type constructor.
///
/// The <representationDeclaration> works as a constructor. The optional
/// ('.' <identifierOrNew>) in the grammar is used to declare this constructor
/// with a name of the form <identifier> '.' <identifier> (at times described as
/// a "named constructor"), or <identifier> '.' 'new'. It is a constant
/// constructor if and only if the reserved word const occurs just after
/// extension type in the header of the declaration. Other constructors may be
/// declared const or not, following the normal rules for constant constructors.
///
/// @description Checks that if an extension type declaration declares a
/// constant constructor then it can be used in a constant expression
/// @author [email protected]
/// @issue 53935
// SharedOptions=--enable-experiment=inline-class

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

extension type const Num(num id) {
const Num.add(Num v1, Num v2) : this((v1 as num) + (v2 as num));
const Num.sub(Num v1, Num v2) : this((v1 as num) - (v2 as num));
const Num.mul(Num v1, Num v2) : this((v1 as num) * (v2 as num));
}

void main() {
const n = Num.sub(Num.mul(Num(7), Num(7)), Num(7));
Expect.equals(42, n.id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023, 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 An extension type declaration DV named Name may declare one or
/// more constructors. A constructor which is declared in an extension type
/// declaration is also known as an extension type constructor.
///
/// The <representationDeclaration> works as a constructor. The optional
/// ('.' <identifierOrNew>) in the grammar is used to declare this constructor
/// with a name of the form <identifier> '.' <identifier> (at times described as
/// a "named constructor"), or <identifier> '.' 'new'. It is a constant
/// constructor if and only if the reserved word const occurs just after
/// extension type in the header of the declaration. Other constructors may be
/// declared const or not, following the normal rules for constant constructors.
///
/// @description Checks that it is a compile-time error if not a constant is
/// used in an evaluation of a constant expression in an extension type constant
/// constructor
/// @author [email protected]
// SharedOptions=--enable-experiment=inline-class

extension type const Num(num id) {
const Num.add(Num v1, Num v2) : this(v1.id + (v2 as num));
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
const Num.sub(Num v1, Num v2) : this((v1 as num) - v2.id);
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

void main() {
print(Num);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2023, 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 An extension type declaration DV named Name may declare one or
/// more constructors. A constructor which is declared in an extension type
/// declaration is also known as an extension type constructor.
///
/// The <representationDeclaration> works as a constructor. The optional
/// ('.' <identifierOrNew>) in the grammar is used to declare this constructor
/// with a name of the form <identifier> '.' <identifier> (at times described as
/// a "named constructor"), or <identifier> '.' 'new'. It is a constant
/// constructor if and only if the reserved word const occurs just after
/// extension type in the header of the declaration. Other constructors may be
/// declared const or not, following the normal rules for constant constructors.
///
/// @description Checks that if an extension type declaration declares a
/// constant constructor then it can be used in a constant expression
/// @author [email protected]
/// @issue 53936,53395
// SharedOptions=--enable-experiment=inline-class

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

extension type const E<T>(Object? id) {
const E.cast(Object? v) : this(v as T);
}

typedef TypeAlias<T> = T;
extension type const TypeOf<T>(T _) {}

void main() {
Expect.equals("a", (const E<String>.cast("a")).id);
Expect.equals("b", (const E<TypeAlias<String>>.cast("b")).id);
Expect.equals("c", (const E<TypeOf<String>>.cast("c")).id);
Expect.equals("d", (const E<String>.cast(TypeOf<String>("d"))).id);
Expect.equals("e", (const E<TypeOf<String>>.cast(TypeOf<String>("e"))).id);
}

0 comments on commit d7f7514

Please sign in to comment.