-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Dart does not throw compile error when declare a variable of the type which is not well bounded #36959
Comments
This looks correct to me. Dart allows "super-bounded types", which are invalid arguments to parameterized types, in some situations. The requirement is that the type would be valid if only some Super-bounded types are useful precisely because recursive type-restrictions like class A<X extends A<X>> {} would not otherwise have any expressible super-types. There is no valid type parameter which can be applied to Example: List<A> x = [];
List<A<A>> y = [];
List<A<dynamic>> z = []; These three declarations are valid, but because List<A<A<dynamic>>> x = <A<A<dynamic>>>[];
List<A<A<A<dynamic>>>> y = <A<A<A<dynamic>>>>[];
List<A<dynamic>> z = <A<dynamic>>[]; which you can check by adding: print(x.runtimeType);
print(y.runtimeType);
print(z.runtimeType); All of those Or something close to his, I'm actually not an expert. @eernstg? |
If dart behavior is correct, there is a bug in analyzer here - it should not throw compile error with the example above, but it does actually... Please note that the problem is in the line 9 here ( |
CFE issue? |
I believe it's an analyzer issue. I just read the specification section "Super-Bounded Types", and I am a little concerned about the declaration typedef AAlias<T extends A<T>> = T Function<T>(); There is no use of typedef AAlias<T extends A<T>> = void Function(); and the fact that it isn't suggests that the test might not be testing what it intends to test. Also, it is a little confusing that we talk about types and not type expressions, because |
I agree: The subtype rules talk about types and they include rules for type aliases, so in that sense we do have precedents for calling terms like So my conclusion is also that this is an analyzer issue. |
This test case does not have errors when the library is legacy (with With null safety |
This issue is still reproducible with the recent Dart, it's: It does not matter if I add To test it with nnbd test source should be changed a liittle (and it also fails): class A<X extends A<X>> {}
typedef AAlias<T extends A<T>> = T Function<T>();
main() {
AAlias a1;
AAlias<A> a2;
AAlias<A<Never>> a3;
AAlias<A<dynamic>> a4;
AAlias<A<Object?>> a5; //# 01: compile-time error
print("OK");
} |
Reopen for investigation. |
Your SDK build did not include the fix. The fix is in c9ee369, and right before it I see extra compilation errors in legacy and null safety. No errors on the bleeding edge. |
Dart SDK Version: 2.3.1-dev.0.0
OS: Windows 10 64 bit
Analyzer throws compile error for the following source code and seems like this is an expected behavior:
Dart does not throw any errors here and just prints "OK". Seems like expected behavior is to throw a compile-time error for
AAlias<A<Object>> a5;
like analyzer does here.Sample output is:
The text was updated successfully, but these errors were encountered: