From 2343353033ab0eeba827e52d05d0b91a6cca9d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20A=2E=20Muci=C3=B1o?= Date: Sat, 26 Nov 2022 16:26:01 -0600 Subject: [PATCH] Avoid ICE if the Clone trait is not found while building error suggestions --- .../src/diagnostics/conflict_errors.rs | 16 +++++++------- src/test/ui/issues/issue-104870.rs | 19 +++++++++++++++++ src/test/ui/issues/issue-104870.stderr | 21 +++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/issues/issue-104870.rs create mode 100644 src/test/ui/issues/issue-104870.stderr diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index a53940070f7ea..12736836c552c 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -739,13 +739,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(), diff --git a/src/test/ui/issues/issue-104870.rs b/src/test/ui/issues/issue-104870.rs new file mode 100644 index 0000000000000..ad4df23c6fbc9 --- /dev/null +++ b/src/test/ui/issues/issue-104870.rs @@ -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(x: T) {} + +fn f(x: *mut u8) { + g(x); + g(x); //~ ERROR use of moved value: `x` +} + +fn main() {} diff --git a/src/test/ui/issues/issue-104870.stderr b/src/test/ui/issues/issue-104870.stderr new file mode 100644 index 0000000000000..e99621a001567 --- /dev/null +++ b/src/test/ui/issues/issue-104870.stderr @@ -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(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`.