Skip to content

Commit 811bd38

Browse files
authored
Rollup merge of #65289 - varkor:issue-65284, r=estebank
Fix suggested bound addition diagnostic Fixes #65284.
2 parents 976a57a + c97d715 commit 811bd38

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

src/librustc_typeck/check/method/suggest.rs

+15-21
Original file line numberDiff line numberDiff line change
@@ -809,31 +809,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
809809
// Get the `hir::Param` to verify whether it already has any bounds.
810810
// We do this to avoid suggesting code that ends up as `T: FooBar`,
811811
// instead we suggest `T: Foo + Bar` in that case.
812-
let mut has_bounds = false;
812+
let mut has_bounds = None;
813813
let mut impl_trait = false;
814814
if let Node::GenericParam(ref param) = hir.get(id) {
815-
match param.kind {
816-
hir::GenericParamKind::Type { synthetic: Some(_), .. } => {
817-
// We've found `fn foo(x: impl Trait)` instead of
818-
// `fn foo<T>(x: T)`. We want to suggest the correct
819-
// `fn foo(x: impl Trait + TraitBound)` instead of
820-
// `fn foo<T: TraitBound>(x: T)`. (#63706)
821-
impl_trait = true;
822-
has_bounds = param.bounds.len() > 1;
823-
}
824-
_ => {
825-
has_bounds = !param.bounds.is_empty();
826-
}
815+
let kind = &param.kind;
816+
if let hir::GenericParamKind::Type { synthetic: Some(_), .. } = kind {
817+
// We've found `fn foo(x: impl Trait)` instead of
818+
// `fn foo<T>(x: T)`. We want to suggest the correct
819+
// `fn foo(x: impl Trait + TraitBound)` instead of
820+
// `fn foo<T: TraitBound>(x: T)`. (See #63706.)
821+
impl_trait = true;
822+
has_bounds = param.bounds.get(1);
823+
} else {
824+
has_bounds = param.bounds.get(0);
827825
}
828826
}
829827
let sp = hir.span(id);
830-
// `sp` only covers `T`, change it so that it covers
831-
// `T:` when appropriate
832-
let sp = if has_bounds {
833-
sp.to(self.tcx
834-
.sess
835-
.source_map()
836-
.next_point(self.tcx.sess.source_map().next_point(sp)))
828+
// `sp` only covers `T`, change it so that it covers `T:` when appropriate.
829+
let sp = if let Some(first_bound) = has_bounds {
830+
sp.until(first_bound.span())
837831
} else {
838832
sp
839833
};
@@ -849,7 +843,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
849843
param,
850844
if impl_trait { " +" } else { ":" },
851845
self.tcx.def_path_str(t.def_id),
852-
if has_bounds { " +"} else { "" },
846+
if has_bounds.is_some() { " + " } else { "" },
853847
)),
854848
Applicability::MaybeIncorrect,
855849
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trait Foo {
2+
fn foo(&self);
3+
}
4+
5+
trait Bar {}
6+
7+
fn do_stuff<T : Bar>(t : T) {
8+
t.foo() //~ ERROR no method named `foo` found for type `T` in the current scope
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0599]: no method named `foo` found for type `T` in the current scope
2+
--> $DIR/issue-65284-suggest-generic-trait-bound.rs:8:7
3+
|
4+
LL | t.foo()
5+
| ^^^ method not found in `T`
6+
|
7+
= help: items from traits can only be used if the type parameter is bounded by the trait
8+
help: the following trait defines an item `foo`, perhaps you need to restrict type parameter `T` with it:
9+
|
10+
LL | fn do_stuff<T: Foo + Bar>(t : T) {
11+
| ^^^^^^^^
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)