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
Expands help for error message E0161 #117536
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @oli-obk (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
This comment has been minimized.
This comment has been minimized.
r? compiler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your PR does not pass CI because of too long lines.
This error message gets triggered when someone wants to move a value in a trait object, such as a `Box<dyn Trait>`. The message gives some advice, such as using a reference. However, this does not address the situation where someone actually intends to move the value. Therefore, this commit adds a suggestion for a possibility of using the `Box<Self>` pattern where one intends to move a self value in the case of using a boxed trait object.
cb8d093
to
2c15266
Compare
I have updated this PR:
I'm not quite sure if the text phrasing is good, would be nice if someone looked it over :) Cheers! |
@rustbot review |
r? compiler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the delay in getting to this, left a suggestion.
@@ -40,3 +40,27 @@ fn main() { | |||
// ok! | |||
} | |||
``` | |||
|
|||
If you absolutely must use a value by moving in a trait, and you know that you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is an improvement, but we should probably be a bit more general in our wording - as I expect there are more arbitrary self types that this is applicable to than just Box
(using Box
as an example is fine and good though).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try to write it more generically!
I have not used arbitrary self types extensively before, usually only stuff like Box<Self>
and Arc<Self>
. So perhaps I was not quite sure on what is available and what makes sense, which is why I limited myself to only mention Box<Self>
.
@xfbs any updates on this? thanks |
This error message gets triggered when someone wants to move a value in a trait object, for example:
The existing message gives the advice of using a reference to
self
in thefinalize()
method. In general, this is sound advice for people who are new to Rust, where they may really intend to take a reference but are used to Python for example.However, this does not address the situation where someone actually needs to move the value. One example is something where you have some kind of a builder pattern (or hash digester, or transaction reference) which have a
finalize(self)
method that you need to consume the boxed trait object as finalization can only be performed once.In the case where:
You can achieve this in another way. This commit adds a suggestion for a possible workaround of using the
Box<Self>
pattern where one intends to move a self value in the case of using a boxed trait object.As this suggestion was very helpful to me, I would like to add it to the error code documentation for others to be able to find it easily as well. I do not replace the existing suggestion of using references, because in most cases that should be preferred. It is only when you know that your trait will always be used via boxed trait objects that this suggestion is the one you are looking for.
What do you think about this? I'm happy to hear some feedback if this is a good thing to add to the error message (or if there is an even better way that I had not thought about).
Cheers!