about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-01-14 09:39:03 +0000
committerbors <bors@rust-lang.org>2024-01-14 09:39:03 +0000
commit8847bda592d940ae1f34d87d8cacdc09a9f787fa (patch)
tree442c109337fbed839905bd748ca6d1d733aae396
parent0dab65b8a112dff83147f95c9ae1cbe75327b5e4 (diff)
parentc36b5d535363917503c04943a8254a1bd3ed4296 (diff)
downloadrust-8847bda592d940ae1f34d87d8cacdc09a9f787fa.tar.gz
rust-8847bda592d940ae1f34d87d8cacdc09a9f787fa.zip
Auto merge of #119361 - sjwang05:issue-119352, r=WaffleLapkin
Fix ICE when suggesting dereferencing binop operands

Fixes #119352
-rw-r--r--compiler/rustc_hir/src/lang_items.rs27
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs8
-rw-r--r--tests/ui/binop/binary-op-suggest-deref.rs8
-rw-r--r--tests/ui/binop/binary-op-suggest-deref.stderr23
4 files changed, 65 insertions, 1 deletions
diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs
index 3f3b57ba94f..1cc1f11b3c8 100644
--- a/compiler/rustc_hir/src/lang_items.rs
+++ b/compiler/rustc_hir/src/lang_items.rs
@@ -374,3 +374,30 @@ pub static OPERATORS: &'static [LangItem] = &[
     LangItem::PartialEq,
     LangItem::PartialOrd,
 ];
+
+pub static BINARY_OPERATORS: &'static [LangItem] = &[
+    LangItem::Add,
+    LangItem::Sub,
+    LangItem::Mul,
+    LangItem::Div,
+    LangItem::Rem,
+    LangItem::BitXor,
+    LangItem::BitAnd,
+    LangItem::BitOr,
+    LangItem::Shl,
+    LangItem::Shr,
+    LangItem::AddAssign,
+    LangItem::SubAssign,
+    LangItem::MulAssign,
+    LangItem::DivAssign,
+    LangItem::RemAssign,
+    LangItem::BitXorAssign,
+    LangItem::BitAndAssign,
+    LangItem::BitOrAssign,
+    LangItem::ShlAssign,
+    LangItem::ShrAssign,
+    LangItem::Index,
+    LangItem::IndexMut,
+    LangItem::PartialEq,
+    LangItem::PartialOrd,
+];
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
index 6c0909d2853..0e33e9cd790 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
@@ -859,6 +859,14 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
             && let hir::Node::Expr(lhs) = self.tcx.hir_node(*lhs_hir_id)
             && let hir::Node::Expr(rhs) = self.tcx.hir_node(*rhs_hir_id)
             && let Some(rhs_ty) = typeck_results.expr_ty_opt(rhs)
+            && let trait_pred = predicate.unwrap_or(trait_pred)
+            // Only run this code on binary operators
+            && hir::lang_items::BINARY_OPERATORS
+                .iter()
+                .filter_map(|&op| self.tcx.lang_items().get(op))
+                .any(|op| {
+                    op == trait_pred.skip_binder().trait_ref.def_id
+                })
         {
             // Suggest dereferencing the LHS, RHS, or both terms of a binop if possible
 
diff --git a/tests/ui/binop/binary-op-suggest-deref.rs b/tests/ui/binop/binary-op-suggest-deref.rs
index 57f24a4c28e..ae442a0d0b4 100644
--- a/tests/ui/binop/binary-op-suggest-deref.rs
+++ b/tests/ui/binop/binary-op-suggest-deref.rs
@@ -72,4 +72,12 @@ fn baz() {
     //~^ERROR can't compare `str` with `&String` [E0277]
 }
 
+fn qux() {
+    // Issue #119352
+    const FOO: i32 = 42;
+    let _ = FOO & (*"Sized".to_string().into_boxed_str());
+    //~^ ERROR the size for values of type `str` cannot be known at compilation time
+    //~| ERROR no implementation for `i32 & str` [E0277]
+}
+
 fn main() {}
diff --git a/tests/ui/binop/binary-op-suggest-deref.stderr b/tests/ui/binop/binary-op-suggest-deref.stderr
index 68b5a24bf97..a98a2ab0706 100644
--- a/tests/ui/binop/binary-op-suggest-deref.stderr
+++ b/tests/ui/binop/binary-op-suggest-deref.stderr
@@ -295,7 +295,28 @@ help: consider dereferencing here
 LL |     _ = partial[..3] == *string_ref;
    |                         +
 
-error: aborting due to 22 previous errors
+error[E0277]: no implementation for `i32 & str`
+  --> $DIR/binary-op-suggest-deref.rs:78:17
+   |
+LL |     let _ = FOO & (*"Sized".to_string().into_boxed_str());
+   |                 ^ no implementation for `i32 & str`
+   |
+   = help: the trait `BitAnd<str>` is not implemented for `i32`
+   = help: the following other types implement trait `BitAnd<Rhs>`:
+             <i32 as BitAnd>
+             <i32 as BitAnd<&i32>>
+             <&'a i32 as BitAnd<i32>>
+             <&i32 as BitAnd<&i32>>
+
+error[E0277]: the size for values of type `str` cannot be known at compilation time
+  --> $DIR/binary-op-suggest-deref.rs:78:17
+   |
+LL |     let _ = FOO & (*"Sized".to_string().into_boxed_str());
+   |                 ^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `str`
+
+error: aborting due to 24 previous errors
 
 Some errors have detailed explanations: E0277, E0308, E0369.
 For more information about an error, try `rustc --explain E0277`.