Skip to content

Commit db2850c

Browse files
authored
Rollup merge of #104956 - mucinoab:issue-104870, r=compiler-errors
Avoid ICE if the Clone trait is not found while building error suggestions Fixes #104870 r? `@compiler-errors`
2 parents 412f05c + 7a378dd commit db2850c

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -732,13 +732,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
732732
let tcx = self.infcx.tcx;
733733
// Try to find predicates on *generic params* that would allow copying `ty`
734734
let infcx = tcx.infer_ctxt().build();
735-
if infcx
736-
.type_implements_trait(
737-
tcx.lang_items().clone_trait().unwrap(),
738-
[tcx.erase_regions(ty)],
739-
self.param_env,
740-
)
741-
.must_apply_modulo_regions()
735+
736+
if let Some(clone_trait_def) = tcx.lang_items().clone_trait()
737+
&& infcx
738+
.type_implements_trait(
739+
clone_trait_def,
740+
[tcx.erase_regions(ty)],
741+
self.param_env,
742+
)
743+
.must_apply_modulo_regions()
742744
{
743745
err.span_suggestion_verbose(
744746
span.shrink_to_hi(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Avoid panicking if the Clone trait is not found while building error suggestions
2+
// See #104870
3+
4+
#![feature(no_core, lang_items)]
5+
#![no_core]
6+
7+
#[lang = "sized"]
8+
trait Sized {}
9+
10+
#[lang = "copy"]
11+
trait Copy {}
12+
13+
fn g<T>(x: T) {}
14+
15+
fn f(x: *mut u8) {
16+
g(x);
17+
g(x); //~ ERROR use of moved value: `x`
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0382]: use of moved value: `x`
2+
--> $DIR/missing-clone-for-suggestion.rs:17: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/missing-clone-for-suggestion.rs:13: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)