about summary refs log tree commit diff
path: root/compiler/rustc_middle
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-07-17 16:22:32 +0200
committerGitHub <noreply@github.com>2024-07-17 16:22:32 +0200
commit1e1e64f10fe9cd0e8bc24c96f8231700f278221d (patch)
tree1be425448af069bb500c5889779d9212673b0a4d /compiler/rustc_middle
parent9db57bf0c954a4ea571e58c8905bf50ad6c2591e (diff)
parent40e07a3ab13913eaed0e9fa336669f590d07d89a (diff)
downloadrust-1e1e64f10fe9cd0e8bc24c96f8231700f278221d.tar.gz
rust-1e1e64f10fe9cd0e8bc24c96f8231700f278221d.zip
Rollup merge of #127844 - chenyukang:yukang-fix-type-bound-127555, r=jieyouxu
Remove invalid further restricting suggestion for type bound

This PR partially addresses #127555, it will remove the obvious error suggestion:

```console
   |                      ^^^^ required by this bound in `<Baz as Foo>::bar`
help: consider further restricting this bound
   |
12 |         F: FnMut() + Send + std::marker::Send,
   |                           +++++++++++++++++++
```

I may create another PR to get a better diagnostic for `impl has stricter requirements than trait` scenario.
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;