-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Generic type defaults should not be allowed if they don’t satisfy type constraints #28024
Comments
triage: I-nominated |
triage: P-high Seems like a straightforward oversight in the WF checker. |
Hmm, thinking more about this, it's perhaps not as straightforward as I originally thought. It gets into problems much like higher-ranked regions etc. For example: fn foo<T,U = T>()
where U: Clone
{
} Here, we can't fully check the value of the default ahead of time, because it references another type parameter ( cc @rust-lang/lang |
Nominating for discussion. |
I'm also going to lower the triage. It seems unlikely that anyone would intentionally abuse this, and I think we'll be able to fix this without undue anger or breakage out there in the wild. triage: P-medium |
Triage: still a problem. @nikomatsakis Long time since any update. Do you know how to fix this and can mentor? |
So it's worth noting that this will compile (modulo some minor fixes), but, if you try to actually use
gives
|
In general, type defaults have been kind of stalled; we've been wanting to rethink the design overall, which has led to a lack of motivation to fixing details of the implementation. I wouldn't in any case tackle this bug first -- the overall algorithm isn't really following the RFC, so I'd fix that first. Not sure if there's an issue about that though. |
Triage: the given example properly fails to compile, giving:
...however, it fails to compile not because the compiler is detecting the invalid default, but because type defaults on functions are being phased out (#36887). Therefore we must ask whether or not it's possible to exhibit an unenforced type default using some other means. My simple attempt to exhibit this bug without using functions has been properly prevented: use std::fmt::Debug;
struct Foo;
trait Bar<T: Debug = Foo> {}
In light of there being no obvious way to reproduce the bug given our modern constraints, and since the bug is quite innocuous (AFAICT it can't ever lead to any actual runtime error, only to an unanticipated compiler error), I feel comfortable closing this. |
As spotted in this internals thread:
compiles. Lack of early error/warning/lint here might be hiding bugs in API design if the default never actually kicks in inside the library.
The text was updated successfully, but these errors were encountered: