-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Bogus error in beta and nightly: recursive type has infinite size #31299
Comments
Same error if I change the pointer to a Box. |
cc @nikomatsakis since I think he touched that code last |
Mini (no trait Front { type Back; }
impl<T> Front for T { type Back = T; }
struct PtrBack<T: Front>(*mut T::Back);
struct M(PtrBack<M>);
fn main() {
println!("{}", std::mem::size_of::<M>());
} |
This is fallout from inductiveness In the OC, the problem is that:
This could potentially be fixed by lazy normalization. A harder case is: trait Front { type Back; }
impl<T> Front for T { type Back = *mut T; /* note *mut is here */ }
struct PtrBack<T: Front>(T::Back);
struct M(PtrBack<M>);
fn main() {
println!("{}", std::mem::size_of::<M>());
} The cycle is similar:
However, not even lazy normalization can save us here. I admit the infinite type detection here is just plain wrong. |
@arielb1 those sequences are kinda hard to understand. However, I'm not sure why |
@Aatch the message is basically a special-cased variant targeting the case where computing whether This is pretty related to how we can formalize auto traits. I'd been debating about a formalization that basically inserted one (automatically generated) impl for each auto trait, but those impls worked the same way as all other impls (this is not how it works today, but how it works today is unsound). However, I realized that this could fail, and my example was exactly the one that @jorendorff gives here -- one where the recursion arises through a projection. In the Obligation Forest PR, I considered whether to make So it may be that the correct fix is to have auto traits have a more complex, co-inductive proving semantics and -- since |
@nikomatsakis so to be clear, the error message is wrong here, the problem is that we have " |
@Aatch I agree the message is wrong, I didn't consider this case when phrasing it -- but I think ideally we wouldn't be reporting an error here at all. I guess the question is if there is a way to achieve that. |
auto trait = OIBIT right?
What's the difference between that and what we have today? That auto traits are coinductive? If we forbid coinductive traits from having supertraits and associated types/constants we should be fine, as they can only be inputs of trait selection and not outputs. |
triage: P-high |
It's not clear what the best fix here is. One option is to change how we process the |
I think that for now the best thing here is to fix the error message, leaving ourselves the freedom to make this legal again in the future if we decide it won't undermine the type system. :) |
@areilb1 Would you mind explaining your I'm trying to understand what's causing the loop. It seemed to me like it might be
But that can't be right, because @nikomatsakis While we're here - I have feels about the wording "has infinite size" in error messages. It feels like it's attributing a deeply ridiculous intent to me, the user. Instead of saying "you did a typo" rustc is saying "oh, you want an infinitely large struct, I'm sorry, I can't do that". It must think I'm some kind of genius-level doofus. The message would be a little insulting, if the compiler were constructed by actual people who are actually smarter than me... waaaait a minute... |
@jorendorff so the message you get about "infinite size" there is actually wrong, it's actually the issue that we have the requirement As for the general problem of "infinite size" in errors, I see your point, but the error has to explain why it's incorrect somehow. I mean, what would an alternative error message look like? "You appear to have made a mistake, trying adding indirection in this type"? What mistake? What's the issue? Why would adding indirection help? |
The expectation was that you encounter this error when you try to make a recursive type (say a tree or something) without using a I think that's a general problem with error messages. When code with a typo attempts to do something silly, you will get an error message about the silly thing. There's a bug here that we report this error message with no infinite-sized type in sight. @nikomatsakis is working on fixing this (I think). Its just a recursive trait evaluation error. |
Sorry I mentioned it. Filed #31683 for the pet peeve. |
This avoids normalizing for structural types, which kind of gives us a poor man's lazy normalization. It's enough to fix issue rust-lang#31299, anyway, though you can still get false failures if you try hard enough. Fixes rust-lang#31299.
This is now fixed (just verified) by #33138. Closing. |
This program prints
8
in Stable (1.5.0), but in Beta and Nightly compilation fails with:From 8 bytes to infinity would seem to be a serious memory usage regression.
The text was updated successfully, but these errors were encountered: