Skip to content

Commit c36b5d5

Browse files
committed
Fix ICE when suggesting dereferencing binop operands
1 parent 6029085 commit c36b5d5

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

Diff for: compiler/rustc_hir/src/lang_items.rs

+27
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,30 @@ pub static OPERATORS: &'static [LangItem] = &[
374374
LangItem::PartialEq,
375375
LangItem::PartialOrd,
376376
];
377+
378+
pub static BINARY_OPERATORS: &'static [LangItem] = &[
379+
LangItem::Add,
380+
LangItem::Sub,
381+
LangItem::Mul,
382+
LangItem::Div,
383+
LangItem::Rem,
384+
LangItem::BitXor,
385+
LangItem::BitAnd,
386+
LangItem::BitOr,
387+
LangItem::Shl,
388+
LangItem::Shr,
389+
LangItem::AddAssign,
390+
LangItem::SubAssign,
391+
LangItem::MulAssign,
392+
LangItem::DivAssign,
393+
LangItem::RemAssign,
394+
LangItem::BitXorAssign,
395+
LangItem::BitAndAssign,
396+
LangItem::BitOrAssign,
397+
LangItem::ShlAssign,
398+
LangItem::ShrAssign,
399+
LangItem::Index,
400+
LangItem::IndexMut,
401+
LangItem::PartialEq,
402+
LangItem::PartialOrd,
403+
];

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+8
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,14 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
859859
&& let hir::Node::Expr(lhs) = self.tcx.hir_node(*lhs_hir_id)
860860
&& let hir::Node::Expr(rhs) = self.tcx.hir_node(*rhs_hir_id)
861861
&& let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs)
862+
&& let trait_pred = predicate.unwrap_or(trait_pred)
863+
// Only run this code on binary operators
864+
&& hir::lang_items::BINARY_OPERATORS
865+
.iter()
866+
.filter_map(|&op| self.tcx.lang_items().get(op))
867+
.any(|op| {
868+
op == trait_pred.skip_binder().trait_ref.def_id
869+
})
862870
{
863871
// Suggest dereferencing the LHS, RHS, or both terms of a binop if possible
864872

Diff for: 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() {}

Diff for: tests/ui/binop/binary-op-suggest-deref.stderr

+22-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,28 @@ help: consider dereferencing here
295295
LL | _ = partial[..3] == *string_ref;
296296
| +
297297

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

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

0 commit comments

Comments
 (0)