Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong trait import suggestion for T: #95991

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1880,9 +1880,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let sp = hir.span(id);
let sp = if let Some(first_bound) = has_bounds {
// `sp` only covers `T`, change it so that it covers
// `T:` when appropriate
sp.until(first_bound.span())
} else if let Some(colon_sp) =
// If the generic param is declared with a colon but without bounds:
// fn foo<T:>(t: T) { ... }
param.colon_span_for_suggestions(
self.inh.tcx.sess.source_map(),
)
{
sp.to(colon_sp)
} else {
sp
};
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/traits/issue-95898.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Test for #95898: The trait suggestion had an extra `:` after the trait.
// edition:2021

fn foo<T:>(t: T) {
t.clone();
//~^ ERROR no method named `clone` found for type parameter `T` in the current scope
}

fn main() {}
15 changes: 15 additions & 0 deletions src/test/ui/traits/issue-95898.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0599]: no method named `clone` found for type parameter `T` in the current scope
--> $DIR/issue-95898.rs:5:7
|
LL | t.clone();
| ^^^^^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `clone`, perhaps you need to restrict type parameter `T` with it:
|
LL | fn foo<T: Clone>(t: T) {
| ~~~~~~~~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.