Skip to content

Commit 824fd62

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

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,15 @@ 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::OPERATORS
865+
.iter()
866+
.filter(|op| !matches!(op, LangItem::Neg | LangItem::Not))
867+
.filter_map(|&op| self.tcx.lang_items().get(op))
868+
.any(|op| {
869+
op == trait_pred.skip_binder().trait_ref.def_id
870+
})
862871
{
863872
// Suggest dereferencing the LHS, RHS, or both terms of a binop if possible
864873

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
@@ -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)