You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Suggest borrowing when doing if let Some(_) from non-Copy ADT #63988
error[E0382]: borrow of moved value: `x`
--> src/librustc_mir/hair/pattern/_match.rs:1614:53
|
1611 | if let Some(range) = x {
| ----- value moved here
...
1614 | debug!("intersection {:?}", x);
| ^ value borrowed here after partial move
|
= note: move occurs because value has type `hair::pattern::_match::IntRange<'_>`, which does not implement the `Copy` trait
error: aborting due to previous error
@estebank can you elaborate on the message you expect to see here? Something like
error[E0382]: borrow of moved value: `x`
--> src/librustc_mir/hair/pattern/_match.rs:1614:53
|
1611 | if let Some(range) = x {
| ----- value moved here
...
1614 | debug!("intersection {:?}", x);
| ^ value borrowed here after partial move
|
= note: move occurs because value has type `hair::pattern::_match::IntRange<'_>`, which does not implement the `Copy` trait
Consider borrowing `x` rather than moving it.
error[E0382]: borrow of moved value: `x`
--> src/librustc_mir/hair/pattern/_match.rs:1614:53
|
1611 | if let Some(range) = x {
| ----- - help: consider borrowing here: `&x`
| |
| value moved here
...
1614 | debug!("intersection {:?}", x);
| ^ value borrowed here after partial move
|
= note: move occurs because value has type `hair::pattern::_match::IntRange<'_>`, which does not implement the `Copy` trait
I took a look at this today. I am a first-time contributor to this project and would like to contribute this enhancement, but I'm not sure how much work this will end up requiring. Here are some notes/leads I've noticed; I'm hoping someone may be able to provide suggestions, and at the very least I hope this is helpful for anyone else wishing to tackle this.
Consider the example provided in the first comment of this issue (abbreviated for example):
error[E0382]: borrow of moved value: `x`
--> src/librustc_mir/hair/pattern/_match.rs:1614:53
|
1611 | if let Some(range) = x {
| ----- value moved here
...
1614 | debug!("intersection {:?}", x);
| ^ value borrowed here after partial move
This error message is generated by MirBorrowckCtxt#report_use_of_moved_or_uninitialized:
This function knows about the erroring span (where the value is borrowed after a move; x in debug!("intersection {:?}", x) in the example above) and the location of where the value was moved (range in the example above).
It's not immediately clear to me that it's easy to recover the span of the RHS of Some(range) = x from here, which is the span we want to give a suggestion for. However, MirBorrowckCtxt seems to know about move sites, and about where values are moved out of:
/// - Generated by moves and declaration of uninitialized variables.
/// - Killed by assignments to the memory.
#[derive(Copy,Clone)]
pubstructMoveOut{
/// path being moved
pubpath:MovePathIndex,
/// location of move
pubsource:Location,
}
I have a feeling that a MoveOut.source is what we're looking for (I assume it would describe the moving out of x in Some(range) = x), but am not entirely certain of that. It's also not yet clear to me how to convert a MoveOut source into a Span that can be used to give a suggestion.
Looking from the top-down is interesting but doesn't seem too useful. One of the lower-stack callers that generates this error is MirBorrowckCtxt#consume_rvalue:
Which does exactly what it sounds like, consuming an rvalue (seemingly x in debug!("intersection {:?}", x) in the example above) and checking for any errors. This doesn't seem too helpful for this problem because the actual checking of move sites isn't done until higher up the call stack, particularly in MirBorrowckCtxt#report_use_of_moved_or_uninitialized.
Right now, I think looking at MirBorrowckCtxt#report_use_of_moved_or_uninitialized and the MoveOut struct are the most likely paths to get closer to solving this. I definitely have many knowledge gaps, and am likely misunderstanding a lot, so any suggestions would be appreciated. Otherwise, I hope it can provide some baseline information for anyone else interested in this.
Activity
ayazhafiz commentedon Sep 3, 2019
@estebank can you elaborate on the message you expect to see here? Something like
perhaps?
estebank commentedon Sep 3, 2019
@ayazhafiz I believe we should aim at:
ayazhafiz commentedon Sep 9, 2019
I took a look at this today. I am a first-time contributor to this project and would like to contribute this enhancement, but I'm not sure how much work this will end up requiring. Here are some notes/leads I've noticed; I'm hoping someone may be able to provide suggestions, and at the very least I hope this is helpful for anyone else wishing to tackle this.
Consider the example provided in the first comment of this issue (abbreviated for example):
This error message is generated by
MirBorrowckCtxt#report_use_of_moved_or_uninitialized:rust/src/librustc_mir/borrow_check/conflict_errors.rs
Lines 47 to 53 in a6624ed
This function knows about the erroring span (where the value is borrowed after a move;
xindebug!("intersection {:?}", x)in the example above) and the location of where the value was moved (rangein the example above).It's not immediately clear to me that it's easy to recover the span of the RHS of
Some(range) = xfrom here, which is the span we want to give a suggestion for. However,MirBorrowckCtxtseems to know about move sites, and about where values are moved out of:rust/src/librustc_mir/dataflow/move_paths/mod.rs
Lines 100 to 102 in 7eb65df
rust/src/librustc_mir/dataflow/move_paths/mod.rs
Lines 156 to 168 in 7eb65df
I have a feeling that a
MoveOut.sourceis what we're looking for (I assume it would describe the moving out ofxinSome(range) = x), but am not entirely certain of that. It's also not yet clear to me how to convert aMoveOutsource into aSpanthat can be used to give a suggestion.Looking from the top-down is interesting but doesn't seem too useful. One of the lower-stack callers that generates this error is
MirBorrowckCtxt#consume_rvalue:rust/src/librustc_mir/borrow_check/mod.rs
Lines 1218 to 1261 in 7eb65df
Which does exactly what it sounds like, consuming an rvalue (seemingly
xindebug!("intersection {:?}", x)in the example above) and checking for any errors. This doesn't seem too helpful for this problem because the actual checking of move sites isn't done until higher up the call stack, particularly inMirBorrowckCtxt#report_use_of_moved_or_uninitialized.Right now, I think looking at
MirBorrowckCtxt#report_use_of_moved_or_uninitializedand theMoveOutstruct are the most likely paths to get closer to solving this. I definitely have many knowledge gaps, and am likely misunderstanding a lot, so any suggestions would be appreciated. Otherwise, I hope it can provide some baseline information for anyone else interested in this.Provide suggestions for some moved value errors
Rollup merge of rust-lang#73534 - estebank:borrowck-suggestions, r=ma…
Rollup merge of rust-lang#73534 - estebank:borrowck-suggestions, r=ma…
Provide suggestions for some moved value errors
Rollup merge of rust-lang#73534 - estebank:borrowck-suggestions, r=ma…
Rollup merge of rust-lang#73534 - estebank:borrowck-suggestions, r=ma…
Rollup merge of rust-lang#73534 - estebank:borrowck-suggestions, r=ma…