diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-04-13 17:35:34 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-13 17:35:34 +0200 |
| commit | 648d65ac7b9c8309620dc86ee60cebafe2a811e2 (patch) | |
| tree | a746c3bc511f7f9f3e0f1149525094319dc3c0d4 | |
| parent | 032358bd30d2eaa31cdec4a40d8a4703fbec22cc (diff) | |
| parent | 9b9f6771049ecd30f13138c32f23ba6562a613d5 (diff) | |
| download | rust-648d65ac7b9c8309620dc86ee60cebafe2a811e2.tar.gz rust-648d65ac7b9c8309620dc86ee60cebafe2a811e2.zip | |
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
| -rw-r--r-- | compiler/rustc_typeck/src/check/method/suggest.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/traits/issue-95898.rs | 9 | ||||
| -rw-r--r-- | src/test/ui/traits/issue-95898.stderr | 15 |
3 files changed, 32 insertions, 2 deletions
diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs index ecc29965937..e6560ca4d9b 100644 --- a/compiler/rustc_typeck/src/check/method/suggest.rs +++ b/compiler/rustc_typeck/src/check/method/suggest.rs @@ -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 }; diff --git a/src/test/ui/traits/issue-95898.rs b/src/test/ui/traits/issue-95898.rs new file mode 100644 index 00000000000..41a20b89959 --- /dev/null +++ b/src/test/ui/traits/issue-95898.rs @@ -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() {} diff --git a/src/test/ui/traits/issue-95898.stderr b/src/test/ui/traits/issue-95898.stderr new file mode 100644 index 00000000000..d7d47905396 --- /dev/null +++ b/src/test/ui/traits/issue-95898.stderr @@ -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`. |
