-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Disallow taking non zero-sized constants by mutable reference #885
Comments
Out of curiosity, is there a compelling reason to allow it for zero-sized constants as a special case, as in the title? What's the use case? |
@glaebhoerl |
Closures make sense, but what is |
I'd like to bring someone's attention to this issue, because the proposed change is backward incompatible. |
@glaebhoerl sometimes you need to conjure an empty slice out of thin air to fulfill all kinds of types and APIs. |
@eddyb, @petrochenkov: So mutably referencing zero size literals is useful (at least, used). But mutably referencing In case the distinction I drew between literals and |
My views didn't change since Feb 18, 2015. |
I'm dubious that I certainly agree your line marked "Surprise!" should be an error though. Could the resulting rvalue simply be marked as |
@petrochenkov hmm I missed these pings first time around. I agree there is some potential for confusion. I'm curious, would you be up for preparing a branch to make this change? I'd like to measure the potential impact. It'd be good to know how much code is affected. |
I suspect a lint is more likely than banning such a construct altogether. I'm not sure what would be the best way to target the lint. Either way, seeing cases of code that breaks might help to figure that out. |
Closing in favor of a rustc warning (rust-lang/rust#40543) or clippy warning (rust-lang/rust-clippy#829). |
As @eddyb explained here, constants (
const
items) no longer behave as rvalues when taken by immutable reference (or by mutable reference in case of zero-sized constants).Yet they still behave as rvalues and create a temporary on the stack when taken by mutable reference:
One more example where the mutable borrowing is implicit and the result is very confusing:
I suggest to prohibit taking constants by mutable reference.
If necessary, the mutable temporary can be created explicitly.
It is rarely needed and the prohibition would not affect ergonomics.
The achieved explicitness would be beneficial for both a reader (it's clearly visible that the reference doesn't bind directly and temporary is created) and a writer (no potential mistakes with surprising consequences) of the code.
A more radical solution would be to disallow taking all rvalues by mutable reference, like C++ does.
If you know the history, early C++ allowed it for a long time, but in the end it was prohibited anyway during the standardization. The consequences are still visible today - Visual C++ still has to allow binding mutable references to temporaries to preserve backward compatibility with older code.
The text was updated successfully, but these errors were encountered: