Skip to content

Commit

Permalink
#2350. Add more factory constructors tests (#2427)
Browse files Browse the repository at this point in the history
Add more factory constructors tests. Note that there are several intentional syntax errors.
  • Loading branch information
sgrekhov authored Dec 13, 2023
1 parent 1863987 commit ccffde6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 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.
///
/// It is a compile-time error if T does not denote a class accessible in the
/// current scope. If T does denote such a class D, it is a compile-time error
/// if R does not denote a constructor. Otherwise, it is a compile-time error if
/// R denotes a generative constructor and D is abstract.
///
/// @description Checks that it is a compile-time error if `R` denotes a
/// generative constructor and `D` is abstract. Test a sealed class
/// @author sgrekhov22@gmail.com
class C {
factory C() = D;
// ^
// [analyzer] unspecified
// [cfe] unspecified

factory C.n() = D.n;
// ^^^
// [analyzer] unspecified
// [cfe] unspecified
}

sealed class D implements C {
D() {}
const D.n();
}

main() {
print(C);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ enum E {

main() {
print(C);
print(F);
print(E);
}
6 changes: 3 additions & 3 deletions Language/Classes/Constructors/Factories/syntax_t04.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
class C {
factory C{ return A();}
// ^^
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

class A implements C{}
class A implements C {}

enum E {
e1, e2;
const E();
factory E.f => E.e1;
// ^^
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}
Expand Down
49 changes: 49 additions & 0 deletions Language/Classes/Constructors/Factories/syntax_t06.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 A factory is a constructor prefaced by the built-in identifier
/// factory.
/// ⟨factoryConstructorSignature⟩ ::=
/// const? factory ⟨constructorName⟩ ⟨formalParameterList⟩
///
/// @description Checks that it is a compile-time error if a non-redirecting
/// factory constructor has an initializers list
/// @author msyabro
class A {
int x;
A(this.x);
factory A.f() : x = 1 {
// ^
// [analyzer] unspecified
// [cfe] unspecified
return C();
}
}

class C extends A {
C() : super(0);
}

enum E {
e1(1), e2(2);

final x;

const E(this.x);
// ^
// [analyzer] unspecified

factory E.f() : x = 0 {
// ^
// [analyzer] unspecified
// [cfe] unspecified
return E.e1;
}
}

main() {
print(C);
print(E);
}

0 comments on commit ccffde6

Please sign in to comment.