Inconsistency when borrow-checking 'ref mut' patterns #48495
Labels
A-borrow-checker
Area: The borrow checker
A-NLL
Area: Non-lexical lifetimes (NLL)
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
While investigating #48357, I discovered a strange inconsistency in how 'ref mut' patterns are checked.
This snippet is correctly rejected both with and without NLL (playground):
It's trying to take a mutable reference to a field of an immutable variable, which clearly should not compile.
However, this snippet compiles both with and without NLL (playground):
The only difference is that
foo
is now part of a struct - but it now borrow checks. Sincefoo
is consumed by the match, I don't think that this can lead to any soundness issues - but it's still both surprising and inconsistent.The same issue comes into play in the
image
crate. This line displays the same inconsistency demonstrated in my example.buffer
is an immutable variable, but a mutable reference is taken to the field of one it's variants (which is itself a mutable reference). TheColorType
enum takes the place of an integer in the tuple, but the result is the same.The text was updated successfully, but these errors were encountered: