Skip to content

Commit

Permalink
#2350. Add more factory constructor tests. Part 3 (#2363)
Browse files Browse the repository at this point in the history
Add more factory constructor tests. Part 3
  • Loading branch information
sgrekhov authored Nov 10, 2023
1 parent d7f7514 commit c4ca3b7
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 15 deletions.
47 changes: 40 additions & 7 deletions Language/Classes/Constructors/Factories/function_type_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,59 @@
// 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 It is a compile error if the function type of k' is not a
/// subtype of the type of k.
/// @description Checks that a compile error is produced if factory constructor
/// redirects to a constructor whose type is not a subtype of factory
/// constructor function type. Checks the case when constructor parameter types
/// @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 error if the function type of k' is not a subtype of the
/// type of k.
///
/// @description Checks that it is a compile-time error is if a factory
/// constructor redirects to a constructor whose type is not a subtype of the
/// factory's function type. Checks the case when constructor parameter types
/// are not assignable.
/// @author ilya

class F {
factory F(x,[String y]) = C;
// ^
// [analyzer] unspecified
// [cfe] unspecified

factory F.foo(x,{Object y}) = C.foo;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

class C implements F {
C(x,[int? y]);
C.foo(x,{int? y});
}

enum E {
e1, e2;
const E();

factory E.f1([int x = 0]) => E.e1;

factory E.fr1([String x]) = E.f1;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
new F(123);
print(F);
print(E);
}
63 changes: 55 additions & 8 deletions Language/Classes/Constructors/Factories/function_type_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@
// 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 It is a compile error if the function type of k' is not a
/// subtype of the type of k.
/// @description Checks that a compile error is produced if factory constructor
/// redirects to a constructor whose type is not a subtype of factory
/// constructor function type. Checks the case when constructor return types
/// @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 error if the function type of k' is not a subtype of the
/// type of k.
///
/// @description Checks that it is a compile-time error is if a factory
/// constructor redirects to a constructor whose type is not a subtype of the
/// factory's function type. Checks the case when constructor return types
/// are not assignable.
/// @author ilya
import "../../../../Utils/dynamic_check.dart";
/// @issue 53934
class F {
factory F(x) = C;
Expand All @@ -23,6 +37,39 @@ class C { // does not implement F, not assignable to F
C(x);
}

enum E1 {
e1, e2;
const E1();

factory E1.f() = C;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

enum E2 {
e1, e2;
const E2();

factory E2.f1() = E3;
// ^^
// [analyzer] unspecified
// [cfe] unspecified

factory E2.f2() = E3.f;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

enum E3 {
e1, e2;
const E3();
factory E3.f() => E3.e1;
}

main() {
checkTypeError(() => new F(123));
print(F);
print(E1);
print(E2);
}
50 changes: 50 additions & 0 deletions Language/Classes/Constructors/Factories/function_type_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 a formal parameter of k′ has a default value
/// whose type is not a subtype of the type annotation on the corresponding
/// formal parameter in k
///
/// @description Checks that it is a compile-time error is if a factory
/// constructor redirects to a constructor whose type is not a subtype of the
/// factory's function type. Checks the case when constructor function type is
/// not assignable.
/// @author [email protected]
class F<T extends num> {
factory F(int x, [T y]) = C<T>;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
factory F.foo(int x, [T y]) = C.foo;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
factory F.bar(int x, {T y}) = C.bar;
// ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

class C<T extends num> implements F<int> {
C(int x, [int y = 0]) {}
C.foo(int x, [int y = 0]) {}
C.bar(int x, {int y = 0}) {}
}

main() {
print(F);
print(C);
}
47 changes: 47 additions & 0 deletions Language/Classes/Constructors/Factories/function_type_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.
/// ...
/// The redirectee constructor for this declaration is the constructor k′
/// denoted by R.
/// ...
/// It is a compile error if the function type of k' is not a subtype of the
/// type of k.
///
/// @description Checks that it is a compile-time error is if a factory
/// constructor redirects to a constructor whose type is not a subtype of the
/// factory's function type. Checks extension type
/// @author [email protected]
/// @issue 53934
// SharedOptions=--enable-experiment=inline-class

enum Bool {
_true._(true),
_false._(false);
final bool isTrue;
const Bool._(this.isTrue);
const factory Bool(bool value) = _BoolHelper;
// ^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

extension type const _BoolHelper._(Bool _) { // missed implements Bool
const _BoolHelper(bool isTrue) : this._(isTrue ? Bool._true : Bool._false);
}

main() {
print(Bool);
}
45 changes: 45 additions & 0 deletions Language/Classes/Constructors/Factories/function_type_t05.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.
/// ...
/// The redirectee constructor for this declaration is the constructor k′
/// denoted by R.
/// ...
/// It is a compile error if the function type of k' is not a subtype of the
/// type of k.
///
/// @description Checks that it is a compile-time error is if a factory
/// constructor redirects to a constructor whose type is not a subtype of the
/// factory's function type. Checks extension type
/// @author [email protected]
// SharedOptions=--enable-experiment=inline-class

class Bool {
final bool _value;
Bool(this._value);

factory Bool.f(bool value) = _BoolHelper;
// ^^^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

extension type const _BoolHelper(Bool _) { // missed implements Bool
_BoolHelper.g(bool val) : this(Bool(val));
}

main() {
print(Bool);
}

0 comments on commit c4ca3b7

Please sign in to comment.