Skip to content

Commit

Permalink
Rollup merge of rust-lang#104956 - mucinoab:issue-104870, r=compiler-…
Browse files Browse the repository at this point in the history
…errors

Avoid ICE if the Clone trait is not found while building error suggestions

Fixes rust-lang#104870

r? ```@compiler-errors```
  • Loading branch information
matthiaskrgr committed Nov 27, 2022
2 parents 34d73ea + 2343353 commit 4990ab8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
16 changes: 9 additions & 7 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let tcx = self.infcx.tcx;
// Try to find predicates on *generic params* that would allow copying `ty`
let infcx = tcx.infer_ctxt().build();
if infcx
.type_implements_trait(
tcx.lang_items().clone_trait().unwrap(),
[tcx.erase_regions(ty)],
self.param_env,
)
.must_apply_modulo_regions()

if let Some(clone_trait_def) = tcx.lang_items().clone_trait()
&& infcx
.type_implements_trait(
clone_trait_def,
[tcx.erase_regions(ty)],
self.param_env,
)
.must_apply_modulo_regions()
{
err.span_suggestion_verbose(
span.shrink_to_hi(),
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/issues/issue-104870.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Avoid panicking if the Clone trait is not found while building error suggestions

#![feature(no_core, lang_items)]
#![no_core]

#[lang = "sized"]
trait Sized {}

#[lang = "copy"]
trait Copy {}

fn g<T>(x: T) {}

fn f(x: *mut u8) {
g(x);
g(x); //~ ERROR use of moved value: `x`
}

fn main() {}
21 changes: 21 additions & 0 deletions src/test/ui/issues/issue-104870.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0382]: use of moved value: `x`
--> $DIR/issue-104870.rs:16:7
|
LL | fn f(x: *mut u8) {
| - move occurs because `x` has type `*mut u8`, which does not implement the `Copy` trait
LL | g(x);
| - value moved here
LL | g(x);
| ^ value used here after move
|
note: consider changing this parameter type in function `g` to borrow instead if owning the value isn't necessary
--> $DIR/issue-104870.rs:12:12
|
LL | fn g<T>(x: T) {}
| - ^ this parameter takes ownership of the value
| |
| in this function

error: aborting due to previous error

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

0 comments on commit 4990ab8

Please sign in to comment.