-
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
Should there be a warning at compile time when implementing Drop trait with creation of same type inside? #55388
Comments
cc @wesleywiser Do you think your new MIR based unconditional recursion lint could be extended to also know about |
I'm guessing we should handle this in in rust/src/librustc_mir/lints.rs Line 128 in 694cf75
The type of the dropped item can probably be extracted from the |
Hi @oli-obk , I'd like to work on this to dive deep into rustc. |
Great! So there's already some advice up above, but I'll repeat what I think are the steps needed:
|
It looks like this method in LanguageItems returns an option with the DefId inside of it, looks like it procedurally generates methods for all the kinds of items. rust/src/librustc/middle/lang_items.rs Lines 91 to 94 in 05812fa
including the drop_trait method: I think this only gives you the DefId of the Drop Trait though, not the method implemented on the type. Not sure if this is helpful but I'm quite interested in how it all works so thought I'd do some digging. Look forward to seeing how it's all worked out though. |
Hi @oli-obk @tom7980 ,
But I haven't figured out how to use rust/src/librustc_mir/lints.rs Line 101 in 694cf75
|
Hi @agnxy For your issue with the mk_substs, I found this code in the monomorphize.rs source. It looks like you can just call it with an array of items cast to an iter with the .iter().cloned() methods.
I imagine it could look something like this
As for the Instance, I'm unsure where to go from there I'd need to spend some time digging around. Let me know if this is useful at all hopefully oli can chime in to give a better explanation |
can you elaborate on the ICE? I'm guessing it's due to your
Kind of. I think you can factor out rust/src/librustc_mir/lints.rs Lines 100 to 120 in 0195812
into its own function in order to "just call it" from the original location and from the new drop checks |
Thanks @tom7980 @oli-obk . I'll continue investigating and studying trait solving in rustc.
Removing |
This failure is happening because we are in a generic context. There are three ways to continue
|
So I've been looking at this in my spare time and tried implementing it with My real question is however, once we have the |
PR with fix: #113902 |
I'm fairly new to Rust and have been working through the reference book recently, I've just covered the Drop trait implementation and thought it would be fun to see what happens if I create a new instance of the type I just dropped inside of the function.
Obviously this is completely nonsensical as nobody is going to do this but as you can probably tell this caused my stack to overflow and crash the program, would an error message at compile time be appropriate for something like this?
I can't see why anyone would want to initialise new variables inside of the drop trait but it's possible.
Example code of my implementation:
The text was updated successfully, but these errors were encountered: