From 4ac90e286bd49663f6f29130df33fa17576b2765 Mon Sep 17 00:00:00 2001 From: OdenShirataki Date: Sun, 11 Feb 2024 15:03:21 +0900 Subject: [PATCH] Fix suggestion span for ?Sized when param type has default and type in trait is generic. --- .../src/traits/error_reporting/suggestions.rs | 4 +- .../error_reporting/type_err_ctxt_ext.rs | 2 +- .../trait-bounds/suggest-maybe-sized-bound.rs | 20 ++++++++++ .../suggest-maybe-sized-bound.stderr | 37 +++++++++++++++++++ tests/ui/traits/issue-28576.stderr | 8 ++-- 5 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 tests/ui/trait-bounds/suggest-maybe-sized-bound.rs create mode 100644 tests/ui/trait-bounds/suggest-maybe-sized-bound.stderr diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index f68200b6f4d5e..a25bb98215e6f 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3042,7 +3042,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { this = "the implicit `Sized` requirement on this type parameter"; } if let Some(hir::Node::TraitItem(hir::TraitItem { - ident, + generics, kind: hir::TraitItemKind::Type(bounds, None), .. })) = tcx.hir().get_if_local(item_def_id) @@ -3054,7 +3054,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { let (span, separator) = if let [.., last] = bounds { (last.span().shrink_to_hi(), " +") } else { - (ident.span.shrink_to_hi(), ":") + (generics.span.shrink_to_hi(), ":") }; err.span_suggestion_verbose( span, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index 07e4fef9dd4f2..fa02c0ab99a6c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -2993,7 +2993,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { { (s, " +") } else { - (span.shrink_to_hi(), ":") + (param.name.ident().span.shrink_to_hi(), ":") }; err.span_suggestion_verbose( span, diff --git a/tests/ui/trait-bounds/suggest-maybe-sized-bound.rs b/tests/ui/trait-bounds/suggest-maybe-sized-bound.rs new file mode 100644 index 0000000000000..15aa27349aa91 --- /dev/null +++ b/tests/ui/trait-bounds/suggest-maybe-sized-bound.rs @@ -0,0 +1,20 @@ +// issue: 120878 +fn main() { + struct StructA { + _marker: std::marker::PhantomData (A, B)>, + } + + struct StructB { + a: StructA, + //~^ ERROR: the size for values of type `[u8]` cannot be known at compilation time [E0277] + } + + trait Trait { + type P; + } + + impl Trait for () { + type P = [u8]; + //~^ ERROR: the size for values of type `[u8]` cannot be known at compilation time [E0277] + } +} diff --git a/tests/ui/trait-bounds/suggest-maybe-sized-bound.stderr b/tests/ui/trait-bounds/suggest-maybe-sized-bound.stderr new file mode 100644 index 0000000000000..4ce936582f43d --- /dev/null +++ b/tests/ui/trait-bounds/suggest-maybe-sized-bound.stderr @@ -0,0 +1,37 @@ +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> $DIR/suggest-maybe-sized-bound.rs:8:12 + | +LL | a: StructA, + | ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by an implicit `Sized` bound in `StructA` + --> $DIR/suggest-maybe-sized-bound.rs:3:23 + | +LL | struct StructA { + | ^^^^^ required by the implicit `Sized` requirement on this type parameter in `StructA` +help: consider relaxing the implicit `Sized` restriction + | +LL | struct StructA { + | ++++++++ + +error[E0277]: the size for values of type `[u8]` cannot be known at compilation time + --> $DIR/suggest-maybe-sized-bound.rs:17:21 + | +LL | type P = [u8]; + | ^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[u8]` +note: required by a bound in `Trait::P` + --> $DIR/suggest-maybe-sized-bound.rs:13:9 + | +LL | type P; + | ^^^^^^^^^^ required by this bound in `Trait::P` +help: consider relaxing the implicit `Sized` restriction + | +LL | type P: ?Sized; + | ++++++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/traits/issue-28576.stderr b/tests/ui/traits/issue-28576.stderr index adba5830b10e1..653ce05d28574 100644 --- a/tests/ui/traits/issue-28576.stderr +++ b/tests/ui/traits/issue-28576.stderr @@ -36,8 +36,8 @@ LL | pub trait Bar: Foo + Sized { | +++++++ help: consider relaxing the implicit `Sized` restriction | -LL | pub trait Foo { - | ++++++++ +LL | pub trait Foo { + | ++++++++ error[E0277]: the size for values of type `Self` cannot be known at compilation time --> $DIR/issue-28576.rs:5:16 @@ -56,8 +56,8 @@ LL | ) where Self: Sized; | +++++++++++++++++ help: consider relaxing the implicit `Sized` restriction | -LL | pub trait Foo { - | ++++++++ +LL | pub trait Foo { + | ++++++++ error: aborting due to 3 previous errors