Skip to content

Commit

Permalink
#3054. Add covariant parameters tests (#3059)
Browse files Browse the repository at this point in the history
Add covariant parameters tests
  • Loading branch information
sgrekhov authored Jan 29, 2025
1 parent bc52d30 commit 9b05eab
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2025, 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 Let `o` be an object, and let `u` be a fresh final variable bound
/// to o. The closurization of method `f` on object `o` is defined to be
/// equivalent to:
/// ```
/// - <X1 extends B′1, ..., Xs extends B′s>
/// (T1 p1, ..., Tn pn, {Tn+1 pn+1 = d1, ..., Tn+k pn+k = dk}) =>
/// u.m<X1, ..., Xs>(p1, ..., pn, pn+1: pn+1, ..., pn+k: pn+k);
/// ```
/// where `f` is an instance method named `m` which has type parameter
/// declarations `X1 extends B1, ..., Xs extends Bs`, required parameters
/// `p1, ..., pn`, and named parameters `pn+1, ..., pn+k` with defaults
/// `d1, ..., dk`, using `null` for parameters whose default value is not
/// specified.
/// ...
/// The parameter types `Tj,j ∈ 1..n + k`, are determined as follows: Let the
/// method declaration `D` be the implementation of `m` which is invoked by the
/// expression in the body. Let `T` be the class that contains D.
///
/// For each parameter `pj, j ∈ 1..n + k`, if `pj` is covariant then `Tj` is
/// the built-in class `Object`.
///
/// @description Check that if `pj` is covariant then `Tj` is `Object?` at
/// run time. Test covariant-by-declaration parameters.
/// @author [email protected]
import '../../../../Utils/expect.dart';
import '../../../../Utils/static_type_helper.dart';

class C {
num m<Y extends num>(
covariant int r1,
covariant Y r2, {
covariant int p1 = 0
}) {
return 42;
}
}

main() {
C o = C();
final f = o.m;
f.expectStaticType<
Exactly<num Function<Y extends num>(int r1, Y r2, {int p1})>
>();

Expect.isTrue(
f is num Function<Y extends num>(Object? r1, Object? r2, {Object? p1})
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2025, 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 Let `o` be an object, and let `u` be a fresh final variable bound
/// to o. The closurization of method `f` on object `o` is defined to be
/// equivalent to:
/// ```
/// - <X1 extends B′1, ..., Xs extends B′s>
/// (T1 p1, ..., Tn pn, {Tn+1 pn+1 = d1, ..., Tn+k pn+k = dk}) =>
/// u.m<X1, ..., Xs>(p1, ..., pn, pn+1: pn+1, ..., pn+k: pn+k);
/// ```
/// where `f` is an instance method named `m` which has type parameter
/// declarations `X1 extends B1, ..., Xs extends Bs`, required parameters
/// `p1, ..., pn`, and named parameters `pn+1, ..., pn+k` with defaults
/// `d1, ..., dk`, using `null` for parameters whose default value is not
/// specified.
/// ...
/// The parameter types `Tj,j ∈ 1..n + k`, are determined as follows: Let the
/// method declaration `D` be the implementation of `m` which is invoked by the
/// expression in the body. Let `T` be the class that contains D.
///
/// For each parameter `pj, j ∈ 1..n + k`, if `pj` is covariant then `Tj` is
/// the built-in class `Object`.
///
/// @description Check that if `pj` is covariant then `Tj` is `Object?` at
/// run time. Test covariant-by-class parameters.
/// @author [email protected]
import '../../../../Utils/expect.dart';
import '../../../../Utils/static_type_helper.dart';

class C<X> {
X m<Y extends X>(X r1, Y r2, {required X p1}) {
return 42 as X;
}
}

main() {
C<Object?> o = C<Object?>();
final f = o.m;
f.expectStaticType<
Exactly<Object? Function<Y extends Object?>(Object? r1, Y r2, {required Object? p1})>
>();

Expect.isTrue(
f is Object? Function<Y extends Object?>( // ignore: unnecessary_type_check
Object? r1,
Y r2, {
required Object? p1,
}),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2025, 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 Let `o` be an object, and let `u` be a fresh final variable bound
/// to o. The closurization of method `f` on object `o` is defined to be
/// equivalent to:
/// ```
/// - <X1 extends B′1, ..., Xs extends B′s>
/// (T1 p1, ..., Tn pn, {Tn+1 pn+1 = d1, ..., Tn+k pn+k = dk}) =>
/// u.m<X1, ..., Xs>(p1, ..., pn, pn+1: pn+1, ..., pn+k: pn+k);
/// ```
/// where `f` is an instance method named `m` which has type parameter
/// declarations `X1 extends B1, ..., Xs extends Bs`, required parameters
/// `p1, ..., pn`, and named parameters `pn+1, ..., pn+k` with defaults
/// `d1, ..., dk`, using `null` for parameters whose default value is not
/// specified.
/// ...
/// The parameter types `Tj,j ∈ 1..n + k`, are determined as follows: Let the
/// method declaration `D` be the implementation of `m` which is invoked by the
/// expression in the body. Let `T` be the class that contains D.
///
/// For each parameter `pj, j ∈ 1..n + k`, if `pj` is covariant then `Tj` is
/// the built-in class `Object`.
///
/// @description Check that if `pj` is covariant then `Tj` is `Object?` at
/// run time.
/// @author [email protected]
import '../../../../Utils/expect.dart';
import '../../../../Utils/static_type_helper.dart';

class C<X extends num> {
X m<Y extends X>(
covariant X r1,
covariant Y r2, {
required covariant int p1,
}) {
return 42 as X;
}
}

main() {
var o = C<num>();
final f = o.m;
f.expectStaticType<
Exactly<num Function<Y extends num>(num r1, Y r2, {required int p1})>
>();

Expect.isTrue(
f is num Function<Y extends num>(
Object? r1,
Object? r2, {
required Object? p1,
}),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2025, 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 Let `o` be an object, and let `u` be a fresh final variable bound
/// to o. The closurization of method `f` on object `o` is defined to be
/// equivalent to:
/// ...
/// ```
/// - <X1 extends B′1, ..., Xs extends B′s>
/// (T1 p1, ..., Tn pn, [Tn+1 pn+1 = d1, ..., Tn+k pn+k = dk]) =>
/// u.m<X1, ..., Xs>(p1, ..., pn+k);
/// ```
/// where `f` is an instance method named `m` which has type parameter
/// declarations `X1 extends B1, ..., Xs extends Bs`, required parameters
/// `p1, ..., pn`, and optional positional parameters `pn+1, ..., pn+k` with
/// defaults `d1, ..., dk`, using `null` for parameters whose default value is
/// not specified.
/// ...
/// The parameter types `Tj,j ∈ 1..n + k`, are determined as follows: Let the
/// method declaration `D` be the implementation of `m` which is invoked by the
/// expression in the body. Let `T` be the class that contains D.
///
/// For each parameter `pj, j ∈ 1..n + k`, if `pj` is covariant then `Tj` is
/// the built-in class `Object`.
///
/// @description Check that if `pj` is covariant then `Tj` is `Object?` at
/// run time. Test covariant-by-declaration parameters.
/// @author [email protected]
import '../../../../Utils/expect.dart';
import '../../../../Utils/static_type_helper.dart';

class C {
num m<Y extends num>(
covariant int r1,
covariant Y r2, [
covariant int p1 = 0,
]) {
return 42;
}
}

main() {
C o = C();
final f = o.m;
f.expectStaticType<
Exactly<num Function<Y extends num>(int r1, Y r2, [int p1])>
>();

Expect.isTrue(
f is num Function<Y extends num>(Object? r1, Object? r2, [Object? p1]),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2025, 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 Let `o` be an object, and let `u` be a fresh final variable bound
/// to o. The closurization of method `f` on object `o` is defined to be
/// equivalent to:
/// ...
/// ```
/// - <X1 extends B′1, ..., Xs extends B′s>
/// (T1 p1, ..., Tn pn, [Tn+1 pn+1 = d1, ..., Tn+k pn+k = dk]) =>
/// u.m<X1, ..., Xs>(p1, ..., pn+k);
/// ```
/// where `f` is an instance method named `m` which has type parameter
/// declarations `X1 extends B1, ..., Xs extends Bs`, required parameters
/// `p1, ..., pn`, and optional positional parameters `pn+1, ..., pn+k` with
/// defaults `d1, ..., dk`, using `null` for parameters whose default value is
/// not specified.
/// ...
/// The parameter types `Tj,j ∈ 1..n + k`, are determined as follows: Let the
/// method declaration `D` be the implementation of `m` which is invoked by the
/// expression in the body. Let `T` be the class that contains D.
///
/// For each parameter `pj, j ∈ 1..n + k`, if `pj` is covariant then `Tj` is
/// the built-in class `Object`.
///
/// @description Check that if `pj` is covariant then `Tj` is `Object?` at
/// run time. Test covariant-by-class parameters.
/// @author [email protected]
import '../../../../Utils/expect.dart';
import '../../../../Utils/static_type_helper.dart';

class C<X> {
X m<Y extends X>(X r1, Y r2, [X? p1]) {
return 42 as X;
}
}

main() {
C<Object?> o = C<Object?>();
final f = o.m;
f.expectStaticType<
Exactly<
Object? Function<Y extends Object?>(Object? r1, Y r2, [Object? p1])
>
>();

Expect.isTrue(
f is Object? Function<Y extends Object?>(Object? r1, Y r2, [Object? p1]) // ignore: unnecessary_type_check
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2025, 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 Let `o` be an object, and let `u` be a fresh final variable bound
/// to o. The closurization of method `f` on object `o` is defined to be
/// equivalent to:
/// ...
/// ```
/// - <X1 extends B′1, ..., Xs extends B′s>
/// (T1 p1, ..., Tn pn, [Tn+1 pn+1 = d1, ..., Tn+k pn+k = dk]) =>
/// u.m<X1, ..., Xs>(p1, ..., pn+k);
/// ```
/// where `f` is an instance method named `m` which has type parameter
/// declarations `X1 extends B1, ..., Xs extends Bs`, required parameters
/// `p1, ..., pn`, and optional positional parameters `pn+1, ..., pn+k` with
/// defaults `d1, ..., dk`, using `null` for parameters whose default value is
/// not specified.
/// ...
/// The parameter types `Tj,j ∈ 1..n + k`, are determined as follows: Let the
/// method declaration `D` be the implementation of `m` which is invoked by the
/// expression in the body. Let `T` be the class that contains D.
///
/// For each parameter `pj, j ∈ 1..n + k`, if `pj` is covariant then `Tj` is
/// the built-in class `Object`.
///
/// @description Check that if `pj` is covariant then `Tj` is `Object?` at
/// run time.
/// @author [email protected]
import '../../../../Utils/expect.dart';
import '../../../../Utils/static_type_helper.dart';

class C<X extends num> {
X m<Y extends X>(covariant X r1, covariant Y r2, [covariant int? p1]) {
return 42 as X;
}
}

main() {
var o = C<num>();
final f = o.m;
f.expectStaticType<
Exactly<num Function<Y extends num>(num r1, Y r2, [int? p1])>
>();

Expect.isTrue(
f is num Function<Y extends num>(Object? r1, Object? r2, [Object? p1]));
}

0 comments on commit 9b05eab

Please sign in to comment.