-
Notifications
You must be signed in to change notification settings - Fork 1.7k
NNBD: Default type bound of the generic type parameter is incorrect in analyzer: it's dynamic instead of Object? #40368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@leafpetersen My reading of the spec(s) is that analyzer does it correctly. Yes, the bound of a type parameter in NNBD is
|
@scheglov is correct. Also to the point, the error you are seeing has nothing to do with the difference between |
Here's another comment on the approach. The following used to work when we have some declaration that introduces a generic type named 'A' and we want to test that the result of i2b on the raw type // Example declaration of a type which is to be tested.
class A<X extends num> {}
// Standard convenience definitions for this kind of test.
typedef F<X> = void Function<Y extends X>();
F<X> toF<X>(X x) => null;
main() {
A source; // Use the target type as a raw type.
var fsource = toF(source); // Type of `fsource` contains `A` invariantly.
F<A<num>> target = fsource;
} With nnbd, this template needs to be updated. In order to avoid involving more complex types it is probably useful to change But we don't have to execute the code involving // Example declaration of a type which is to be tested.
class A<X extends num?> {}
// Standard convenience definitions for this kind of test.
typedef F<X> = void Function<Y extends X>();
F<X> toF<X>(X x) => throw 0;
void testA(A source) {
var fsource = toF(source);
F<A<num?>> _ = fsource;
}
main() {
// Ensure that `testA` will be compiled, but not executed.
print(testA);
} We do not require that any tool must analyze/compile dead code, so a robust test which does not expect a compile-time error would need to have something like We need to avoid executing |
The following source code produces compile error: typedef F<X> = void Function<Y extends X>();
F<X>? toF<X>(X x) => null;
class A<T> {}
F<A<Object?>>? testA(A? source) {
var fsource = toF(source);
return fsource;
}
main() {} Sample output is:
Dart version is:
|
@iarkh Hm... I think the error is expected here. The type |
Maybe the test should be like this?: typedef F<X> = void Function<Y extends X>();
F<X>? toF<X>(X x) => null;
class A<T> {}
F<A<Object?>?>? testA(A? source) { // Extra `?` in return type.
var fsource = toF(source);
return fsource;
}
main() {} I'll close this issue because we do get the expected results from the analyzer. However, to sum up the considerations about how to test instantiation to bound, here's a version that gets as close as we can now: typedef F<X> = void Function<Y extends X>();
F<X> toF<X>(X x) => throw 0;
class A<T> {}
F<A<Object?>> testA(A source) {
var fsource = toF(source);
return fsource;
}
void main() {
print(testA);
} @iarkh, this template should be usable for testing instantiation to bound under nnbd. Note that we never introduce nullability into the types, which will enable us to test a type like Note that the nnbd subtype rules will use mutual subtyping among type variable bounds, so this means that we cannot distinguish types like |
@eernstg, seems like it works for me, thank you! All the old static tests for instantiate-to-bound calls |
Dart VM version: 2.8.0-edge.40f23c735f04433e4fc334fbd674474bd3de0f8b (Tue Jan 28 01:14:48 2020 +0000) on "linux_x64"
This bug is similar with #40367, but this one against analyzer, and #40367 is against runtime.
NNBD Spec reads:
The default bound of generic type parameters is treated as Object?.
However, currently this is not so and analyzer treats it as
dynamic
.Please run analyzer with the following code:
It should pass, however actually it fails with compile time error:
The text was updated successfully, but these errors were encountered: