Skip to content

Commit

Permalink
suggest_borrow_generic_arg: erase regions sooner
Browse files Browse the repository at this point in the history
  • Loading branch information
dianne committed Nov 17, 2024
1 parent 5afd5ad commit 2e3372a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
return None;
}

// Erase region variables before performing any substitutions (#133118)
let moved_arg_ty = tcx.erase_regions(moved_arg_ty);

// Try borrowing a shared reference first, then mutably.
if let Some(mutbl) = [ty::Mutability::Not, ty::Mutability::Mut].into_iter().find(|&mutbl| {
let re = self.infcx.tcx.lifetimes.re_erased;
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/moves/region-var-in-moved-ty-issue-133118.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! regression test for #133118
pub trait Alpha {
fn y(self) -> usize;
}

pub trait Beta {
type Gamma;
fn gamma(&self) -> Self::Gamma;
}

pub fn a<T: Alpha>(_x: T) -> usize {
todo!();
}

pub fn x<B>(beta: &B) -> usize
where
for<'a> &'a B: Beta,
for<'a> <&'a B as Beta>::Gamma: Alpha,
{
let g1 = beta.gamma();
a(g1) + a(g1) //~ ERROR use of moved value: `g1` [E0382]
}

pub fn main() {}
21 changes: 21 additions & 0 deletions tests/ui/moves/region-var-in-moved-ty-issue-133118.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0382]: use of moved value: `g1`
--> $DIR/region-var-in-moved-ty-issue-133118.rs:22:15
|
LL | let g1 = beta.gamma();
| -- move occurs because `g1` has type `<&B as Beta>::Gamma`, which does not implement the `Copy` trait
LL | a(g1) + a(g1)
| -- ^^ value used here after move
| |
| value moved here
|
note: consider changing this parameter type in function `a` to borrow instead if owning the value isn't necessary
--> $DIR/region-var-in-moved-ty-issue-133118.rs:12:24
|
LL | pub fn a<T: Alpha>(_x: T) -> usize {
| - ^ this parameter takes ownership of the value
| |
| in this function

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.

0 comments on commit 2e3372a

Please sign in to comment.