about summary refs log tree commit diff
path: root/compiler/rustc_middle
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2024-07-17 14:18:41 +0800
committeryukang <moorekang@gmail.com>2024-07-17 19:08:37 +0800
commit40e07a3ab13913eaed0e9fa336669f590d07d89a (patch)
tree130c4ecc965cb6c8188281eb0aaf08201b68e225 /compiler/rustc_middle
parent1a6e777c3c9e5dd57e1e62d95e13eeebaaebac04 (diff)
downloadrust-40e07a3ab13913eaed0e9fa336669f590d07d89a.tar.gz
rust-40e07a3ab13913eaed0e9fa336669f590d07d89a.zip
Remove invalid further restricting for type bound
Diffstat (limited to 'compiler/rustc_middle')
-rw-r--r--compiler/rustc_middle/src/ty/diagnostics.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs
index 4bf22337991..f479b18c7c4 100644
--- a/compiler/rustc_middle/src/ty/diagnostics.rs
+++ b/compiler/rustc_middle/src/ty/diagnostics.rs
@@ -271,6 +271,19 @@ pub fn suggest_constraining_type_params<'a>(
             }
         }
 
+        // in the scenario like impl has stricter requirements than trait,
+        // we should not suggest restrict bound on the impl, here we double check
+        // the whether the param already has the constraint by checking `def_id`
+        let bound_trait_defs: Vec<DefId> = generics
+            .bounds_for_param(param.def_id)
+            .flat_map(|bound| {
+                bound.bounds.iter().flat_map(|b| b.trait_ref().and_then(|t| t.trait_def_id()))
+            })
+            .collect();
+
+        constraints
+            .retain(|(_, def_id)| def_id.map_or(true, |def| !bound_trait_defs.contains(&def)));
+
         if constraints.is_empty() {
             continue;
         }
@@ -332,6 +345,7 @@ pub fn suggest_constraining_type_params<'a>(
         //          --
         //          |
         //          replace with: `T: Bar +`
+
         if let Some((span, open_paren_sp)) = generics.bounds_span_for_suggestions(param.def_id) {
             suggest_restrict(span, true, open_paren_sp);
             continue;