-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2349. Add more tests for constant constructors of the extension types (
#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
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
LanguageFeatures/Extension-types/static_analysis_extension_types_A21_t08.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
37 changes: 37 additions & 0 deletions
37
LanguageFeatures/Extension-types/static_analysis_extension_types_A21_t09.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
39 changes: 39 additions & 0 deletions
39
LanguageFeatures/Extension-types/static_analysis_extension_types_A21_t10.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |