-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[Typing] Constaint generics from as
checks do not work on immediate values in unreacheable code
#8660
Comments
@Wilfred This might help with the case in #8647 since you think it has something to do with generics allowing their constraints to be violated in unreachable code. This somehow feels like it is the same problem, but in reverse.
|
Interestingly, the following code produces no type errors:
Presumably this is because we always refine Dead code is really tricky. Users like to have some type checking there, but it's legitimate for the type checker to ignore it entirely. Dead code checks are a best effort. Is this just a weird behaviour you noticed, or is this blocking legitimate code patterns? |
During development I often insert a You could say this is a legitimate use case, but this is not something you'd want to do in production. The workaround is quite easy. if(always_true()){
return 'something I want to test quickly';
}
//...
function always_true(): bool {
return true;
} The code below is not dead code to the typechecker, so it won't have this bug. This would also have another benefit. There is no need to fix this issue, but let's keep it open anyway. |
as KeyedContainer<_, _>
fails in unreachable code.as
checks do not work on immediate values in unreacheable code
Superceded by #9013. This issue is a generalization of the concept described in this issue. |
Describe the bug
A clear and concise description of what the bug is.
Type assertions in unreachable code give false positives for type constraints on expressions like
as KeyedContainer<_, _>
. The keytype is limited to a subtype of arraykey, but the typechecker forgets that in unreachable code.Standalone code, or other way to reproduce the problem
Expected behavior
A clear and concise description of what you expected to happen.
This code should not have a typeerror, since
dict(mixed_function() as KeyedContainer<_, _>)
is typesafe.Screenshots
Desktop (please complete the following information):
Additional context
Removing the
return;
fixes the typeerror.The text was updated successfully, but these errors were encountered: