Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2349. Add more tests for constant constructors of the extension types #2352

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 sgrekhov22@gmail.com
/// @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 sgrekhov22@gmail.com

// 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 sgrekhov22@gmail.com
/// @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);
eernstg marked this conversation as resolved.
Show resolved Hide resolved
}
Loading