Skip to content

Commit 648d65a

Browse files
authored
Rollup merge of #95991 - PoorlyDefinedBehaviour:fix/issue_95898, r=fee1-dead
fix: wrong trait import suggestion for T: The suggestion to bound `T` had an extra `:`. ```rust fn foo<T:>(t: T) { t.clone(); } ``` ``` error[E0599]: no method named `clone` found for type parameter `T` in the current scope --> src/lib.rs:2:7 | 2 | 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: | 1 | fn foo<T: Clone:>(t: T) { | ~~~~~~~~ ``` Fixes: #95898
2 parents 032358b + 9b9f677 commit 648d65a

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1880,9 +1880,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18801880
};
18811881
let sp = hir.span(id);
18821882
let sp = if let Some(first_bound) = has_bounds {
1883-
// `sp` only covers `T`, change it so that it covers
1884-
// `T:` when appropriate
18851883
sp.until(first_bound.span())
1884+
} else if let Some(colon_sp) =
1885+
// If the generic param is declared with a colon but without bounds:
1886+
// fn foo<T:>(t: T) { ... }
1887+
param.colon_span_for_suggestions(
1888+
self.inh.tcx.sess.source_map(),
1889+
)
1890+
{
1891+
sp.to(colon_sp)
18861892
} else {
18871893
sp
18881894
};

src/test/ui/traits/issue-95898.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Test for #95898: The trait suggestion had an extra `:` after the trait.
2+
// edition:2021
3+
4+
fn foo<T:>(t: T) {
5+
t.clone();
6+
//~^ ERROR no method named `clone` found for type parameter `T` in the current scope
7+
}
8+
9+
fn main() {}

src/test/ui/traits/issue-95898.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0599]: no method named `clone` found for type parameter `T` in the current scope
2+
--> $DIR/issue-95898.rs:5:7
3+
|
4+
LL | t.clone();
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 `clone`, perhaps you need to restrict type parameter `T` with it:
9+
|
10+
LL | fn foo<T: Clone>(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)