1
Fork 0

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.
This commit is contained in:
Matthias Krüger 2024-07-17 16:22:32 +02:00 committed by GitHub
commit 1e1e64f10f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 70 additions and 33 deletions

View file

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