Skip to content

Commit b523b2d

Browse files
committed
Avoid ICE when referencing desugared local binding in borrow error
1 parent 03f19f7 commit b523b2d

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

Diff for: src/librustc_mir/borrow_check/conflict_errors.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1140,19 +1140,18 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11401140
bug!("try_report_cannot_return_reference_to_local: not a local")
11411141
};
11421142
match self.body.local_kind(*local) {
1143-
LocalKind::ReturnPointer | LocalKind::Temp => {
1144-
(
1145-
"temporary value".to_string(),
1146-
"temporary value created here".to_string(),
1147-
)
1148-
}
1149-
LocalKind::Arg => {
1150-
(
1151-
"function parameter".to_string(),
1152-
"function parameter borrowed here".to_string(),
1153-
)
1154-
},
1155-
LocalKind::Var => bug!("local variable without a name"),
1143+
LocalKind::ReturnPointer | LocalKind::Temp => (
1144+
"temporary value".to_string(),
1145+
"temporary value created here".to_string(),
1146+
),
1147+
LocalKind::Arg => (
1148+
"function parameter".to_string(),
1149+
"function parameter borrowed here".to_string(),
1150+
),
1151+
LocalKind::Var => (
1152+
"local binding".to_string(),
1153+
"local binding introduced here".to_string(),
1154+
),
11561155
}
11571156
};
11581157

Diff for: src/test/ui/borrowck/issue-63026.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::collections::HashMap;
2+
use std::hash::Hash;
3+
4+
fn group_by<I, F, T>(xs: &mut I, f: F) -> HashMap<T, Vec<&I::Item>>
5+
where
6+
I: Iterator,
7+
F: Fn(&I::Item) -> T,
8+
T: Eq + Hash,
9+
{
10+
let mut result = HashMap::new();
11+
for ref x in xs {
12+
let key = f(x);
13+
result.entry(key).or_insert(Vec::new()).push(x);
14+
}
15+
result //~ ERROR cannot return value referencing local binding
16+
}
17+
18+
fn main() {}

Diff for: src/test/ui/borrowck/issue-63026.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0515]: cannot return value referencing local binding
2+
--> $DIR/issue-63026.rs:15:5
3+
|
4+
LL | for ref x in xs {
5+
| -- local binding introduced here
6+
...
7+
LL | result
8+
| ^^^^^^ returns a value referencing data owned by the current function
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0515`.

0 commit comments

Comments
 (0)