Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc_middle::traits::IsConstable;
use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::print::{
PrintPolyTraitPredicateExt as _, PrintPolyTraitRefExt, PrintTraitPredicateExt as _,
with_forced_trimmed_paths, with_no_trimmed_paths, with_types_for_suggestion,
with_crate_prefix, with_forced_trimmed_paths, with_no_trimmed_paths, with_types_for_suggestion,
};
use rustc_middle::ty::{
self, AdtKind, GenericArgs, InferTy, IsSuggestable, Ty, TyCtxt, TypeFoldable, TypeFolder,
Expand Down Expand Up @@ -221,15 +221,24 @@ pub fn suggest_restriction<'tcx, G: EmissionGuarantee>(
(_, None) => predicate_constraint(hir_generics, trait_pred.upcast(tcx)),
(None, Some((ident, []))) => (
ident.span.shrink_to_hi(),
format!(": {}", trait_pred.print_modifiers_and_trait_path()),
with_crate_prefix!(with_no_trimmed_paths!(format!(
": {}",
trait_pred.print_modifiers_and_trait_path()
))),
),
(_, Some((_, [.., bounds]))) => (
bounds.span().shrink_to_hi(),
format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
with_crate_prefix!(with_no_trimmed_paths!(format!(
" + {}",
trait_pred.print_modifiers_and_trait_path()
))),
),
(Some(_), Some((_, []))) => (
hir_generics.span.shrink_to_hi(),
format!(": {}", trait_pred.print_modifiers_and_trait_path()),
with_crate_prefix!(with_no_trimmed_paths!(format!(
": {}",
trait_pred.print_modifiers_and_trait_path()
))),
),
};

Expand Down Expand Up @@ -384,9 +393,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
// Missing generic type parameter bound.
let param_name = self_ty.to_string();
let mut constraint = with_no_trimmed_paths!(
trait_pred.print_modifiers_and_trait_path().to_string()
);
let mut constraint = with_crate_prefix!(with_no_trimmed_paths!(format!(
"{}",
trait_pred.print_modifiers_and_trait_path()
)));

if let Some((name, term)) = associated_ty {
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
help: consider further restricting `Self`
|
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
| +++++++++++++++++++++++++
LL | trait UncheckedCopy: Sized + std::ops::AddAssign<&'static str> {
| +++++++++++++++++++++++++++++++++++

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

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

error: aborting due to 4 previous errors

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/associated-types/defaults-unsound-62211-1.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
help: consider further restricting `Self`
|
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
| +++++++++++++++++++++++++
LL | trait UncheckedCopy: Sized + std::ops::AddAssign<&'static str> {
| +++++++++++++++++++++++++++++++++++

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

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

error: aborting due to 4 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
help: consider further restricting `Self`
|
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
| +++++++++++++++++++++++++
LL | trait UncheckedCopy: Sized + std::ops::AddAssign<&'static str> {
| +++++++++++++++++++++++++++++++++++

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

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

error: aborting due to 4 previous errors

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/associated-types/defaults-unsound-62211-2.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ LL | type Output: Copy + Deref<Target = str> + AddAssign<&'static str> + Fro
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `UncheckedCopy::Output`
help: consider further restricting `Self`
|
LL | trait UncheckedCopy: Sized + AddAssign<&'static str> {
| +++++++++++++++++++++++++
LL | trait UncheckedCopy: Sized + std::ops::AddAssign<&'static str> {
| +++++++++++++++++++++++++++++++++++

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

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

error: aborting due to 4 previous errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/issue-63593.current.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ LL | type This = Self;
| ^^^^^^^^^^^^^^^^^ required by this bound in `MyTrait::This`
help: consider further restricting `Self`
|
LL | trait MyTrait: Sized {
| +++++++
LL | trait MyTrait: std::marker::Sized {
| ++++++++++++++++++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/issue-63593.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ LL | type This = Self;
| ^^^^^^^^^^^^^^^^^ required by this bound in `MyTrait::This`
help: consider further restricting `Self`
|
LL | trait MyTrait: Sized {
| +++++++
LL | trait MyTrait: std::marker::Sized {
| ++++++++++++++++++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ note: required by an implicit `Sized` bound in `Add`
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
help: consider further restricting `Self`
|
LL | trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> + Sized {}
| +++++++
LL | trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> + std::marker::Sized {}
| ++++++++++++++++++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/dyn-compatibility/supertrait-mentions-Self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ LL | trait Bar<T> {
| ^ required by the implicit `Sized` requirement on this type parameter in `Bar`
help: consider further restricting `Self`
|
LL | trait Baz : Bar<Self> + Sized {
| +++++++
LL | trait Baz : Bar<Self> + std::marker::Sized {
| ++++++++++++++++++++
help: consider relaxing the implicit `Sized` restriction
|
LL | trait Bar<T: ?Sized> {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/generic-associated-types/issue-74816.current.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ LL | type Associated: Trait1 = Self;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Trait2::Associated`
help: consider further restricting `Self`
|
LL | trait Trait2: Sized {
| +++++++
LL | trait Trait2: std::marker::Sized {
| ++++++++++++++++++++

error: aborting due to 2 previous errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/generic-associated-types/issue-74816.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ LL | type Associated: Trait1 = Self;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Trait2::Associated`
help: consider further restricting `Self`
|
LL | trait Trait2: Sized {
| +++++++
LL | trait Trait2: std::marker::Sized {
| ++++++++++++++++++++

error: aborting due to 2 previous errors

Expand Down
36 changes: 19 additions & 17 deletions tests/ui/suggestions/bound-suggestions.fixed
Original file line number Diff line number Diff line change
@@ -1,73 +1,75 @@
//@ run-rustfix
//@ edition:2018

#![allow(unused)]

#[allow(unused)]
use std::fmt::Debug;
// Rustfix should add this, or use `std::fmt::Debug` instead.

#[allow(dead_code)]
fn test_impl(t: impl Sized + std::fmt::Debug) {
println!("{:?}", t);
//~^ ERROR doesn't implement
}

#[allow(dead_code)]
fn test_no_bounds<T: std::fmt::Debug>(t: T) {
println!("{:?}", t);
//~^ ERROR doesn't implement
}

#[allow(dead_code)]
fn test_one_bound<T: Sized + std::fmt::Debug>(t: T) {
println!("{:?}", t);
//~^ ERROR doesn't implement
}

#[allow(dead_code)]
fn test_no_bounds_where<X, Y>(x: X, y: Y) where X: std::fmt::Debug, Y: std::fmt::Debug {
println!("{:?} {:?}", x, y);
//~^ ERROR doesn't implement
}

#[allow(dead_code)]
fn test_one_bound_where<X>(x: X) where X: Sized + std::fmt::Debug {
println!("{:?}", x);
//~^ ERROR doesn't implement
}

#[allow(dead_code)]
fn test_many_bounds_where<X>(x: X) where X: Sized + std::fmt::Debug, X: Sized {
println!("{:?}", x);
//~^ ERROR doesn't implement
}

#[allow(dead_code)]
trait Foo<T>: Sized {
trait Foo<T>: std::marker::Sized {
const SIZE: usize = core::mem::size_of::<Self>();
//~^ ERROR the size for values of type `Self` cannot be known at compilation time
}

#[allow(dead_code)]
trait Bar: std::fmt::Display + Sized {
trait Bar: std::fmt::Display + std::marker::Sized {
const SIZE: usize = core::mem::size_of::<Self>();
//~^ ERROR the size for values of type `Self` cannot be known at compilation time
}

#[allow(dead_code)]
trait Baz: Sized where Self: std::fmt::Display {
trait Baz: std::marker::Sized where Self: std::fmt::Display {
const SIZE: usize = core::mem::size_of::<Self>();
//~^ ERROR the size for values of type `Self` cannot be known at compilation time
}

#[allow(dead_code)]
trait Qux<T>: Sized where Self: std::fmt::Display {
trait Qux<T>: std::marker::Sized where Self: std::fmt::Display {
const SIZE: usize = core::mem::size_of::<Self>();
//~^ ERROR the size for values of type `Self` cannot be known at compilation time
}

#[allow(dead_code)]
trait Bat<T>: std::fmt::Display + Sized {
trait Bat<T>: std::fmt::Display + std::marker::Sized {
const SIZE: usize = core::mem::size_of::<Self>();
//~^ ERROR the size for values of type `Self` cannot be known at compilation time
}

trait MyTrait {}

trait Wrapper<T: MyTrait> {}

mod inner {
trait MyTrait {}
// Ensure that we suggest the fully-qualified path of `crate::MyTrait`
// We only do that on editions>=2018.
fn method<T: MyTrait + crate::MyTrait, W: super::Wrapper<T>>() {} //~ ERROR the trait bound `T: MyTrait` is not satisfied
}

fn main() { }
Loading
Loading