Skip to content

Commit

Permalink
dart-lang#2350. Add more factory constructor tests (dart-lang#2543)
Browse files Browse the repository at this point in the history
Add more factory constructor tests
  • Loading branch information
sgrekhov committed Feb 15, 2024
1 parent 77be124 commit b6c6b27
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class F<T extends String> {
}

class C<T extends num> implements F<T> {
// ^
// ^
// [analyzer] unspecified
// [cfe] unspecified

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
/// 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 const redirecting
/// factory constructor redirects to non-const generative constructor
/// @description Checks that it is a compile-error if a const redirecting
/// factory constructor redirects to a non-const generative constructor
/// @author ilya
class F {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
/// 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 const redirecting
/// factory constructor redirects to non-const factory constructor
/// @description Checks that it is a compile-error if a const redirecting
/// factory constructor redirects to a non-const factory constructor
/// @author ilya
class F1 {
Expand Down
71 changes: 71 additions & 0 deletions Language/Classes/Constructors/Factories/const_modifier_t03.dart
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 Language/Classes/Constructors/Factories/const_modifier_t04.dart
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);
}
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());
}
}
}

0 comments on commit b6c6b27

Please sign in to comment.