-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Confusing compiler error message when attempting to pass a trait object to a function using immutable reference #37914
Comments
Iterator::min consumes self, which requires the size of self to be known comile time. If you have a non mutable reference rustc will deref the reference to get something it can call .min on. However if you have a mutable reference rustc knows that there is a implementation of Iterator for it without having to dereference it, so you dont have a unsized value. |
Thanks for the clarification. Could the compiler's diagnostics check, "if only this reference to trait were mutable, it would no longer be unsized", and provide a hint to the user? In many other cases rustc does an excellent job in guiding the user toward correct code. |
The message has changed in the meantime, but it still doesn't show how to fix the problem:
It would be nice if the message at least hinted at mutability as the source of the problem. |
This problem surfaced again on Stack Overflow, for the same underlying reason: An interesting point is that calling fn test(t: &Iterator<Item = &u64>) {
t.next();
}
|
|
…matsakis Note when a mutable trait object is needed Fix rust-lang#63619, fix rust-lang#37914. CC rust-lang#64068.
Current output:
|
In the case of noisy iterator types, I didn't see the difference between "&mut Iter..." and "&Iter..." until I found this issue page! Would it be possible to really emphasize the mutability difference in the help message?
|
…r=Nilstrieb Structured suggestion for `&mut dyn Iterator` when possible Fix rust-lang#37914.
…r=Nilstrieb Structured suggestion for `&mut dyn Iterator` when possible Fix rust-lang#37914.
Thanks to everyone working to improve this over the years. @bors The primary message "the I can file a separate ticket that concentrates on this issue, as the part where the compiler identifies that a mutable ref is needed is clearly covered now. |
We can reopen this one, of course, but opening a new one with a more direct description (and a link to this one) will be easier to keep track of and understand when it is "done". Thank you for all your reports! They are incredibly important in improving Rust! |
The following code fails to compile because the reference to the iterator trait object must be mutable, to enable advancing the iterator:
When the problem is fixed by declaring the trait object as mutable, the code compiles and runs just fine:
Understanding the problem, I would expect the error message for the first snippet to be something along the lines of "error: Iterator<Item=&u64>::next requires a mutable reference". Instead, the compilation failed with the following error:
Being a beginner in Rust, this error message threw me off. First, I couldn't understand why Rust was insisting that the object is unsized, when the object was declared to accept a reference (fat pointer) to a trait, or that was at least my intention. (I still don't understand this part.)
More importantly, there was no hint that the problem could be resolved simply by changing
&
to&mut
in declaration and invocation. Once I realized that, the change was obvious and easy, but the compiler's error message did not help me understand the problem.The text was updated successfully, but these errors were encountered: