Skip to content

Commit 66bca0d

Browse files
committed
When suggesting trait bounds, use fully-qualified path
This side-steps issues of name clashes with imports or traits in the current scope. ``` error[E0277]: the trait bound `T: MyTrait` is not satisfied --> $DIR/bound-suggestions.rs:72:27 | LL | fn method<T: MyTrait, W: super::Wrapper<T>>() {} | ^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `T` | note: required by a bound in `Wrapper` --> $DIR/bound-suggestions.rs:66:18 | LL | trait Wrapper<T: MyTrait> {} | ^^^^^^^ required by this bound in `Wrapper` help: consider further restricting type parameter `T` with trait `MyTrait` | LL | fn method<T: MyTrait + crate::MyTrait, W: super::Wrapper<T>>() {} | ++++++++++++++++ ``` Ideally we'd lean on resolve to have the best name for a `DefId` on a given scope, but then we'd have to ferry the scope around for pretty much every diagnostic. This is the second best thing that does address the problem of "the suggestion is broken".
1 parent 6d6a08c commit 66bca0d

17 files changed

+129
-99
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_middle::traits::IsConstable;
2828
use rustc_middle::ty::error::TypeError;
2929
use rustc_middle::ty::print::{
3030
PrintPolyTraitPredicateExt as _, PrintPolyTraitRefExt, PrintTraitPredicateExt as _,
31-
with_forced_trimmed_paths, with_no_trimmed_paths, with_types_for_suggestion,
31+
with_crate_prefix, with_forced_trimmed_paths, with_no_trimmed_paths, with_types_for_suggestion,
3232
};
3333
use rustc_middle::ty::{
3434
self, AdtKind, GenericArgs, InferTy, IsSuggestable, Ty, TyCtxt, TypeFoldable, TypeFolder,
@@ -221,15 +221,24 @@ pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
221221
(_, None) => predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
222222
(None, Some((ident, []))) => (
223223
ident.span.shrink_to_hi(),
224-
format!(": {}", trait_pred.print_modifiers_and_trait_path()),
224+
with_crate_prefix!(with_no_trimmed_paths!(format!(
225+
": {}",
226+
trait_pred.print_modifiers_and_trait_path()
227+
))),
225228
),
226229
(_, Some((_, [.., bounds]))) => (
227230
bounds.span().shrink_to_hi(),
228-
format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
231+
with_crate_prefix!(with_no_trimmed_paths!(format!(
232+
" + {}",
233+
trait_pred.print_modifiers_and_trait_path()
234+
))),
229235
),
230236
(Some(_), Some((_, []))) => (
231237
hir_generics.span.shrink_to_hi(),
232-
format!(": {}", trait_pred.print_modifiers_and_trait_path()),
238+
with_crate_prefix!(with_no_trimmed_paths!(format!(
239+
": {}",
240+
trait_pred.print_modifiers_and_trait_path()
241+
))),
233242
),
234243
};
235244

@@ -384,9 +393,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
384393
}
385394
// Missing generic type parameter bound.
386395
let param_name = self_ty.to_string();
387-
let mut constraint = with_no_trimmed_paths!(
388-
trait_pred.print_modifiers_and_trait_path().to_string()
389-
);
396+
let mut constraint = with_crate_prefix!(with_no_trimmed_paths!(format!(
397+
"{}",
398+
trait_pred.print_modifiers_and_trait_path()
399+
)));
390400

391401
if let Some((name, term)) = associated_ty {
392402
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.

tests/ui/associated-types/defaults-unsound-62211-1.current.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
2727
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
2828
help: consider further restricting `Self`
2929
|
30-
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
31-
| +++++++++++++++++++++++++
30+
LL | trait UncheckedCopy: Sized + std::ops::AddAssign<&'static str> {
31+
| +++++++++++++++++++++++++++++++++++
3232

3333
error[E0277]: the trait bound `Self: Deref` is not satisfied
3434
--> $DIR/defaults-unsound-62211-1.rs:24:96
@@ -43,8 +43,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
4343
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
4444
help: consider further restricting `Self`
4545
|
46-
LL | trait UncheckedCopy: Sized + Deref {
47-
| +++++++
46+
LL | trait UncheckedCopy: Sized + std::ops::Deref {
47+
| +++++++++++++++++
4848

4949
error[E0277]: the trait bound `Self: Copy` is not satisfied
5050
--> $DIR/defaults-unsound-62211-1.rs:24:96
@@ -59,8 +59,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
5959
| ^^^^ required by this bound in `UncheckedCopy::Output`
6060
help: consider further restricting `Self`
6161
|
62-
LL | trait UncheckedCopy: Sized + Copy {
63-
| ++++++
62+
LL | trait UncheckedCopy: Sized + std::marker::Copy {
63+
| +++++++++++++++++++
6464

6565
error: aborting due to 4 previous errors
6666

tests/ui/associated-types/defaults-unsound-62211-1.next.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
2727
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
2828
help: consider further restricting `Self`
2929
|
30-
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
31-
| +++++++++++++++++++++++++
30+
LL | trait UncheckedCopy: Sized + std::ops::AddAssign<&'static str> {
31+
| +++++++++++++++++++++++++++++++++++
3232

3333
error[E0277]: the trait bound `Self: Deref` is not satisfied
3434
--> $DIR/defaults-unsound-62211-1.rs:24:96
@@ -43,8 +43,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
4343
| ^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
4444
help: consider further restricting `Self`
4545
|
46-
LL | trait UncheckedCopy: Sized + Deref {
47-
| +++++++
46+
LL | trait UncheckedCopy: Sized + std::ops::Deref {
47+
| +++++++++++++++++
4848

4949
error[E0277]: the trait bound `Self: Copy` is not satisfied
5050
--> $DIR/defaults-unsound-62211-1.rs:24:96
@@ -59,8 +59,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
5959
| ^^^^ required by this bound in `UncheckedCopy::Output`
6060
help: consider further restricting `Self`
6161
|
62-
LL | trait UncheckedCopy: Sized + Copy {
63-
| ++++++
62+
LL | trait UncheckedCopy: Sized + std::marker::Copy {
63+
| +++++++++++++++++++
6464

6565
error: aborting due to 4 previous errors
6666

tests/ui/associated-types/defaults-unsound-62211-2.current.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
2727
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
2828
help: consider further restricting `Self`
2929
|
30-
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
31-
| +++++++++++++++++++++++++
30+
LL | trait UncheckedCopy: Sized + std::ops::AddAssign<&'static str> {
31+
| +++++++++++++++++++++++++++++++++++
3232

3333
error[E0277]: the trait bound `Self: Deref` is not satisfied
3434
--> $DIR/defaults-unsound-62211-2.rs:24:96
@@ -43,8 +43,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
4343
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
4444
help: consider further restricting `Self`
4545
|
46-
LL | trait UncheckedCopy: Sized + Deref {
47-
| +++++++
46+
LL | trait UncheckedCopy: Sized + std::ops::Deref {
47+
| +++++++++++++++++
4848

4949
error[E0277]: the trait bound `Self: Copy` is not satisfied
5050
--> $DIR/defaults-unsound-62211-2.rs:24:96
@@ -59,8 +59,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
5959
| ^^^^ required by this bound in `UncheckedCopy::Output`
6060
help: consider further restricting `Self`
6161
|
62-
LL | trait UncheckedCopy: Sized + Copy {
63-
| ++++++
62+
LL | trait UncheckedCopy: Sized + std::marker::Copy {
63+
| +++++++++++++++++++
6464

6565
error: aborting due to 4 previous errors
6666

tests/ui/associated-types/defaults-unsound-62211-2.next.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
2727
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
2828
help: consider further restricting `Self`
2929
|
30-
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
31-
| +++++++++++++++++++++++++
30+
LL | trait UncheckedCopy: Sized + std::ops::AddAssign<&'static str> {
31+
| +++++++++++++++++++++++++++++++++++
3232

3333
error[E0277]: the trait bound `Self: Deref` is not satisfied
3434
--> $DIR/defaults-unsound-62211-2.rs:24:96
@@ -43,8 +43,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
4343
| ^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
4444
help: consider further restricting `Self`
4545
|
46-
LL | trait UncheckedCopy: Sized + Deref {
47-
| +++++++
46+
LL | trait UncheckedCopy: Sized + std::ops::Deref {
47+
| +++++++++++++++++
4848

4949
error[E0277]: the trait bound `Self: Copy` is not satisfied
5050
--> $DIR/defaults-unsound-62211-2.rs:24:96
@@ -59,8 +59,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
5959
| ^^^^ required by this bound in `UncheckedCopy::Output`
6060
help: consider further restricting `Self`
6161
|
62-
LL | trait UncheckedCopy: Sized + Copy {
63-
| ++++++
62+
LL | trait UncheckedCopy: Sized + std::marker::Copy {
63+
| +++++++++++++++++++
6464

6565
error: aborting due to 4 previous errors
6666

tests/ui/associated-types/issue-63593.current.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ LL | type This = Self;
1111
| ^^^^^^^^^^^^^^^^^ required by this bound in `MyTrait::This`
1212
help: consider further restricting `Self`
1313
|
14-
LL | trait MyTrait: Sized {
15-
| +++++++
14+
LL | trait MyTrait: std::marker::Sized {
15+
| ++++++++++++++++++++
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/associated-types/issue-63593.next.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ LL | type This = Self;
1111
| ^^^^^^^^^^^^^^^^^ required by this bound in `MyTrait::This`
1212
help: consider further restricting `Self`
1313
|
14-
LL | trait MyTrait: Sized {
15-
| +++++++
14+
LL | trait MyTrait: std::marker::Sized {
15+
| ++++++++++++++++++++
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/associated-types/trait-with-supertraits-needing-sized-self.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ note: required by an implicit `Sized` bound in `Add`
88
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
99
help: consider further restricting `Self`
1010
|
11-
LL | trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> + Sized {}
12-
| +++++++
11+
LL | trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> + std::marker::Sized {}
12+
| ++++++++++++++++++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/dyn-compatibility/supertrait-mentions-Self.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ LL | trait Bar<T> {
1111
| ^ required by the implicit `Sized` requirement on this type parameter in `Bar`
1212
help: consider further restricting `Self`
1313
|
14-
LL | trait Baz : Bar<Self> + Sized {
15-
| +++++++
14+
LL | trait Baz : Bar<Self> + std::marker::Sized {
15+
| ++++++++++++++++++++
1616
help: consider relaxing the implicit `Sized` restriction
1717
|
1818
LL | trait Bar<T: ?Sized> {

tests/ui/generic-associated-types/issue-74816.current.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ LL | type Associated: Trait1 = Self;
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Trait2::Associated`
2828
help: consider further restricting `Self`
2929
|
30-
LL | trait Trait2: Sized {
31-
| +++++++
30+
LL | trait Trait2: std::marker::Sized {
31+
| ++++++++++++++++++++
3232

3333
error: aborting due to 2 previous errors
3434

0 commit comments

Comments
 (0)