Skip to content

Commit 2343353

Browse files
committed
Avoid ICE if the Clone trait is not found while building error suggestions
1 parent 579c993 commit 2343353

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -739,13 +739,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
739739
let tcx = self.infcx.tcx;
740740
// Try to find predicates on *generic params* that would allow copying `ty`
741741
let infcx = tcx.infer_ctxt().build();
742-
if infcx
743-
.type_implements_trait(
744-
tcx.lang_items().clone_trait().unwrap(),
745-
[tcx.erase_regions(ty)],
746-
self.param_env,
747-
)
748-
.must_apply_modulo_regions()
742+
743+
if let Some(clone_trait_def) = tcx.lang_items().clone_trait()
744+
&& infcx
745+
.type_implements_trait(
746+
clone_trait_def,
747+
[tcx.erase_regions(ty)],
748+
self.param_env,
749+
)
750+
.must_apply_modulo_regions()
749751
{
750752
err.span_suggestion_verbose(
751753
span.shrink_to_hi(),

src/test/ui/issues/issue-104870.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Avoid panicking if the Clone trait is not found while building error suggestions
2+
3+
#![feature(no_core, lang_items)]
4+
#![no_core]
5+
6+
#[lang = "sized"]
7+
trait Sized {}
8+
9+
#[lang = "copy"]
10+
trait Copy {}
11+
12+
fn g<T>(x: T) {}
13+
14+
fn f(x: *mut u8) {
15+
g(x);
16+
g(x); //~ ERROR use of moved value: `x`
17+
}
18+
19+
fn main() {}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0382]: use of moved value: `x`
2+
--> $DIR/issue-104870.rs:16:7
3+
|
4+
LL | fn f(x: *mut u8) {
5+
| - move occurs because `x` has type `*mut u8`, which does not implement the `Copy` trait
6+
LL | g(x);
7+
| - value moved here
8+
LL | g(x);
9+
| ^ value used here after move
10+
|
11+
note: consider changing this parameter type in function `g` to borrow instead if owning the value isn't necessary
12+
--> $DIR/issue-104870.rs:12:12
13+
|
14+
LL | fn g<T>(x: T) {}
15+
| - ^ this parameter takes ownership of the value
16+
| |
17+
| in this function
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)