Skip to content

Commit 5b3ce22

Browse files
committed
Fix ICE when suggesting dereferencing binop operands
1 parent 15755f3 commit 5b3ce22

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
860860
&& let hir::Node::Expr(lhs) = self.tcx.hir_node(*lhs_hir_id)
861861
&& let hir::Node::Expr(rhs) = self.tcx.hir_node(*rhs_hir_id)
862862
&& let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs)
863+
&& let trait_pred = predicate.unwrap_or(trait_pred)
864+
&& hir::lang_items::OPERATORS.iter().any(|&op| {
865+
// Ensure we only run this code on operators
866+
self.tcx.require_lang_item(op, None) == trait_pred.skip_binder().trait_ref.def_id
867+
})
863868
{
864869
// Suggest dereferencing the LHS, RHS, or both terms of a binop if possible
865870

@@ -891,9 +896,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
891896
trait_ref: ty::TraitRef::new(
892897
self.tcx,
893898
inner.trait_ref.def_id,
894-
self.tcx.mk_args(
895-
&[&[l_ty.into(), r_ty.into()], &inner.trait_ref.args[2..]]
896-
.concat(),
899+
self.tcx.mk_args_trait(
900+
l_ty,
901+
iter::once(r_ty.into())
902+
.chain(inner.trait_ref.args.iter().skip(2)),
897903
),
898904
),
899905
..inner

tests/ui/binop/binary-op-suggest-deref.rs

+8
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ fn baz() {
7272
//~^ERROR can't compare `str` with `&String` [E0277]
7373
}
7474

75+
fn qux() {
76+
// Issue #119352
77+
const FOO: i32 = 42;
78+
let _ = FOO & (*"Sized".to_string().into_boxed_str());
79+
//~^ ERROR the size for values of type `str` cannot be known at compilation time
80+
//~| ERROR no implementation for `i32 & str` [E0277]
81+
}
82+
7583
fn main() {}

tests/ui/binop/binary-op-suggest-deref.stderr

+22-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,28 @@ help: consider dereferencing here
300300
LL | _ = partial[..3] == *string_ref;
301301
| +
302302

303-
error: aborting due to 22 previous errors
303+
error[E0277]: no implementation for `i32 & str`
304+
--> $DIR/binary-op-suggest-deref.rs:78:17
305+
|
306+
LL | let _ = FOO & (*"Sized".to_string().into_boxed_str());
307+
| ^ no implementation for `i32 & str`
308+
|
309+
= help: the trait `BitAnd<str>` is not implemented for `i32`
310+
= help: the following other types implement trait `BitAnd<Rhs>`:
311+
<i32 as BitAnd>
312+
<i32 as BitAnd<&i32>>
313+
<&'a i32 as BitAnd<i32>>
314+
<&i32 as BitAnd<&i32>>
315+
316+
error[E0277]: the size for values of type `str` cannot be known at compilation time
317+
--> $DIR/binary-op-suggest-deref.rs:78:17
318+
|
319+
LL | let _ = FOO & (*"Sized".to_string().into_boxed_str());
320+
| ^ doesn't have a size known at compile-time
321+
|
322+
= help: the trait `Sized` is not implemented for `str`
323+
324+
error: aborting due to 24 previous errors
304325

305326
Some errors have detailed explanations: E0277, E0308, E0369.
306327
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)