dart-sdk/tests/language/mixin/type_parameter_inference_test.dart
Robert Nystrom 7ca5ad46ce Set tests that have mixin errors as 2.19.
Mark tests that contain errors about using a class as a mixin to use
language version 2.19 where that's not an error.

This may not fix all of the tests because it's the language version of
the library where the class is declared that matters, not where the
class is used as a mixin. But most tests have all of their declarations
in the same library, so this should fix most.

Change-Id: I910439ebd2f10f731418dc588b7e4619a0841c16
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/285923
Reviewed-by: Jake Macdonald <jakemac@google.com>
Commit-Queue: Jake Macdonald <jakemac@google.com>
2023-03-01 15:03:39 +00:00

78 lines
2.2 KiB
Dart

// Copyright (c) 2018, 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.
// TODO(51557): Decide if the mixins being applied in this test should be
// "mixin", "mixin class" or the test should be left at 2.19.
// @dart=2.19
import "package:expect/expect.dart";
abstract class A<T> {
// This is ok because type inference will ensure that in C, A and M are
// instantiated with the same T.
T f(T x) => x; //# 01: ok
}
class B {}
mixin M1<T> on A<T> {
T f(T x) => x;
T g(T x) => x;
Type h() => T;
}
class M2<T> {
T g(T x) => x;
Type h() => T;
}
// Inferred as `class C extends A<B> with M1<B>`
class C extends A<B> with M1 {}
// Inferred as `class D = A<B> with M1<B>`
class D = A<B> with M1;
// Inferred as `class E extends Object with M2<dynamic>`
class E extends Object with M2 {}
// Ok because a type parameter is supplied
class F extends Object with M2<B> {}
main() {
C c = new C();
D d = new D();
E e = new E();
F f = new F();
// M1 is instantiated with B, so C.g has type (B) -> B.
B Function(B) x = c.g; //# 02: ok
B Function(B) x = d.g; //# 03: ok
Null Function(Null) x = c.g; //# 04: compile-time error
Null Function(Null) x = d.g; //# 05: compile-time error
Object Function(Object) x = c.g; //# 06: compile-time error
Object Function(Object) x = d.g; //# 07: compile-time error
// And verify that the runtime system has the right type for the type
// parameter
Expect.equals(c.h(), B); //# 08: ok
Expect.equals(c.h(), B); //# 09: ok
// M2 is instantiated with dynamic, so E.g has type (dynamic) -> dynamic.
dynamic Function(dynamic) x = e.g; //# 10: ok
B Function(B) x = e.g; //# 11: compile-time error
// And verify that the runtime system has the right type for the type
// parameter
Expect.equals(e.h(), dynamic); //# 12: ok
// M2 is instantiated with B, so F.g has type (B) -> B.
B Function(B) x = f.g; //# 13: ok
Null Function(Null) x = f.g; //# 14: compile-time error
Object Function(Object) x = f.g; //# 15: compile-time error
// And verify that the runtime system has the right type for the type
// parameter
Expect.equals(f.h(), B); //# 16: ok
}