Skip to content

Commit 2e3372a

Browse files
committed
suggest_borrow_generic_arg: erase regions sooner
1 parent 5afd5ad commit 2e3372a

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
666666
return None;
667667
}
668668

669+
// Erase region variables before performing any substitutions (#133118)
670+
let moved_arg_ty = tcx.erase_regions(moved_arg_ty);
671+
669672
// Try borrowing a shared reference first, then mutably.
670673
if let Some(mutbl) = [ty::Mutability::Not, ty::Mutability::Mut].into_iter().find(|&mutbl| {
671674
let re = self.infcx.tcx.lifetimes.re_erased;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! regression test for #133118
2+
3+
pub trait Alpha {
4+
fn y(self) -> usize;
5+
}
6+
7+
pub trait Beta {
8+
type Gamma;
9+
fn gamma(&self) -> Self::Gamma;
10+
}
11+
12+
pub fn a<T: Alpha>(_x: T) -> usize {
13+
todo!();
14+
}
15+
16+
pub fn x<B>(beta: &B) -> usize
17+
where
18+
for<'a> &'a B: Beta,
19+
for<'a> <&'a B as Beta>::Gamma: Alpha,
20+
{
21+
let g1 = beta.gamma();
22+
a(g1) + a(g1) //~ ERROR use of moved value: `g1` [E0382]
23+
}
24+
25+
pub fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0382]: use of moved value: `g1`
2+
--> $DIR/region-var-in-moved-ty-issue-133118.rs:22:15
3+
|
4+
LL | let g1 = beta.gamma();
5+
| -- move occurs because `g1` has type `<&B as Beta>::Gamma`, which does not implement the `Copy` trait
6+
LL | a(g1) + a(g1)
7+
| -- ^^ value used here after move
8+
| |
9+
| value moved here
10+
|
11+
note: consider changing this parameter type in function `a` to borrow instead if owning the value isn't necessary
12+
--> $DIR/region-var-in-moved-ty-issue-133118.rs:12:24
13+
|
14+
LL | pub fn a<T: Alpha>(_x: T) -> usize {
15+
| - ^ this parameter takes ownership of the value
16+
| |
17+
| in this function
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)