-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Pattern errors are too imprecise and should be removed in favor of MIR borrowck #45600
Copy link
Copy link
Closed
Labels
A-NLLArea: Non-lexical lifetimes (NLL)Area: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerArea: The borrow checkerE-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.F-move_ref_pattern`#![feature(move_ref_pattern)]``#![feature(move_ref_pattern)]`NLL-completeWorking towards the "valid code works" goalWorking towards the "valid code works" goalP-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-NLLArea: Non-lexical lifetimes (NLL)Area: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerArea: The borrow checkerE-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.F-move_ref_pattern`#![feature(move_ref_pattern)]``#![feature(move_ref_pattern)]`NLL-completeWorking towards the "valid code works" goalWorking towards the "valid code works" goalP-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Update by @pnkfelix :
NLL (aka MIR-borrowck) addresses the first case given in the issue, but not this one:
Original bug report follows
For this code:
rustc produces:
This error comes from
check_legality_of_move_bindingsinlibrustc_const_eval/check_match.rs.The message is at least arguably incorrect: since
xis not used in the pattern guard, it doesn't make much sense to claim that the code "moves value into pattern guard".I suppose you could argue that all bindings in a pattern are always moved/copied to the pattern guard, and the fact that this one is unused doesn't matter. Under that interpretation, it's a matter of improving the error message, and this issue is a dupe of rust-lang/rfcs#684 which seeks to improve that error message in general.
However, I think the code should be allowed, as it's not unsound. I'm not too familiar with compiler internals, but it seems like once MIR borrow checking is finished, it would catch any actually-unsound moves into pattern guards, and this error could be removed altogether. Is that correct?
The other two errors generated by
check_legality_of_move_bindingshave similar issues:For one thing, this error seems to be a subset of a different one from elsewhere in
check_match.rs, "pattern bindings are not allowed after an@" - at least, I can't think of any code that would trigger this and not that. (Feel free to let me know what I'm missing.) But in any case, this is overly conservative. If we havethen it should be legal to match
x @ Foo(y)(becauseyisCopy). Again, this seems like something MIR borrowck might be able to handle better, although the frontend might have to make sure the copy comes from the right place (pre- or post-move).I don't even know what this error is trying to prevent. The following works fine even with the existing borrowck (
fproperly becomes a partially moved value):So what is wrong with putting b into the pattern?