forked from dart-lang/co19
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dart-lang#2350. Add more factory constructor tests (dart-lang#2543)
Add more factory constructor tests
- Loading branch information
Showing
6 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
71 changes: 71 additions & 0 deletions
71
Language/Classes/Constructors/Factories/const_modifier_t03.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,71 @@ | ||
// 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 Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name | ||
/// and formal type parameters of the enclosing class, const? is const or empty, | ||
/// N is C or C.id0 for some identifier id0, and id is an identifier, then | ||
/// consider a declaration of a redirecting factory constructor k of one of the | ||
/// forms | ||
/// const? factory | ||
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R; | ||
/// const? factory | ||
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R; | ||
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id. | ||
/// ... | ||
/// The redirectee constructor for this declaration is the constructor k′ | ||
/// denoted by R. | ||
/// ... | ||
/// It is a compile-time error if k is prefixed with the const modifier but k' | ||
/// is not a constant constructor. | ||
/// | ||
/// @description Checks that it is a compile-error if a const redirecting | ||
/// factory constructor redirects to a non-const constructor. Test extension | ||
/// types | ||
/// @author sgrekhov22@gmail.com | ||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
class F { | ||
const factory F(F _) = FET1; | ||
// ^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
const factory F.f2(F _) = FET2.nonConst; | ||
// ^^^^^^^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
|
||
extension type FET1(F _) implements F {} | ||
|
||
extension type const FET2(F v) implements F { | ||
FET2.nonConst(this.v) {} | ||
} | ||
|
||
enum E { | ||
e1, e2; | ||
|
||
const E(); | ||
const factory E.f1(E _) = EET1; | ||
// ^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
const factory E.f2(E _) = EET2.nonConst; | ||
// ^^^^^^^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
|
||
extension type EET1(E _) implements E {} | ||
|
||
extension type const EET2(E v) implements E { | ||
EET2.nonConst(this.v) {} | ||
} | ||
|
||
main() { | ||
print(F); | ||
print(E); | ||
} |
55 changes: 55 additions & 0 deletions
55
Language/Classes/Constructors/Factories/const_modifier_t04.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,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 Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name | ||
/// and formal type parameters of the enclosing class, const? is const or empty, | ||
/// N is C or C.id0 for some identifier id0, and id is an identifier, then | ||
/// consider a declaration of a redirecting factory constructor k of one of the | ||
/// forms | ||
/// const? factory | ||
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R; | ||
/// const? factory | ||
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R; | ||
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id. | ||
/// ... | ||
/// The redirectee constructor for this declaration is the constructor k′ | ||
/// denoted by R. | ||
/// ... | ||
/// It is a compile-time error if k is prefixed with the const modifier but k' | ||
/// is not a constant constructor. | ||
/// | ||
/// @description Checks that it is not an error if a const redirecting | ||
/// factory constructor redirects to a const factory constructor. Test extension | ||
/// types | ||
/// @author sgrekhov22@gmail.com | ||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import '../../../../Utils/expect.dart'; | ||
|
||
class F { | ||
const F(); | ||
const factory F.test(F _) = FET; | ||
} | ||
|
||
extension type const FET(F _) implements F {} | ||
|
||
enum E { | ||
e1, e2; | ||
|
||
const E(); | ||
const factory E.test(E _) = EET; | ||
} | ||
|
||
extension type const EET(E _) implements E {} | ||
|
||
main() { | ||
const f1 = FET(F()); | ||
const f = F.test(F()); | ||
Expect.identical(f, f1); | ||
|
||
const e1 = EET(E.e1); | ||
const e = E.test(E.e1); | ||
Expect.identical(e, e1); | ||
} |
43 changes: 43 additions & 0 deletions
43
Language/Classes/Constructors/Factories/redirecting_constructor_call_t04.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,43 @@ | ||
// 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 For the dynamic semantics, assume that k is a redirecting factory | ||
/// constructor and k′ is the redirectee of k | ||
/// ... | ||
/// When the redirectee k′ is a generative constructor, let o be a | ||
/// fresh instance of the class that contains k′. Execution of k then amounts to | ||
/// execution of k′ to initialize o, governed by the same rules as an instance | ||
/// creation expression. If k′ completed normally then the execution of k | ||
/// completes normally returning o, otherwise k completes by throwing the | ||
/// exception and stack trace thrown by k′. | ||
/// | ||
/// @description Checks that if a call of the redirectee completes by throwing | ||
/// an exception, the redirecting constructor call completes with the same | ||
/// exception | ||
/// @author sgrekhov22@gmail.com | ||
import "../../../../Utils/expect.dart"; | ||
|
||
class F { | ||
factory F() = C; | ||
} | ||
|
||
class C implements F { | ||
C() { | ||
dynamic x = "42"; | ||
int y = x; | ||
} | ||
} | ||
|
||
main() { | ||
try { | ||
C(); | ||
} catch (e1) { | ||
try { | ||
F(); | ||
} catch (e2) { | ||
Expect.equals(e1.toString(), e2.toString()); | ||
} | ||
} | ||
} |