From e20f6ff1dcfaa90aad841f8d36734d1e70d2e065 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 11 Feb 2023 23:17:38 +0000 Subject: [PATCH] Tighter spans for bad inherent impl types --- .../src/coherence/inherent_impls.rs | 33 ++++---- tests/ui/coherence/issue-85026.stderr | 8 +- .../const-generics/wrong-normalization.stderr | 4 +- tests/ui/error-codes/E0116.stderr | 2 +- tests/ui/error-codes/E0118.stderr | 4 +- tests/ui/error-codes/E0390.stderr | 8 +- tests/ui/impl-trait/where-allowed.stderr | 4 +- .../needs-has-incoherent-impls.stderr | 78 ++++++------------- .../no-attr-empty-impl.stderr | 12 +-- tests/ui/kinds-of-primitive-impl.stderr | 16 ++-- .../private-in-public-ill-formed.stderr | 8 +- .../traits/trait-or-new-type-instead.stderr | 7 +- 12 files changed, 74 insertions(+), 110 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs index c1b0237b2d1..940a450101c 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs @@ -57,7 +57,7 @@ const ADD_ATTR: &str = "alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items"; impl<'tcx> InherentCollect<'tcx> { - fn check_def_id(&mut self, item: &hir::Item<'_>, self_ty: Ty<'tcx>, def_id: DefId) { + fn check_def_id(&mut self, item: &hir::Item<'_>, self_ty: Ty<'tcx>, def_id: DefId, span: Span) { let impl_def_id = item.owner_id; if let Some(def_id) = def_id.as_local() { // Add the implementation to the mapping from implementation to base @@ -76,12 +76,12 @@ impl<'tcx> InherentCollect<'tcx> { if !self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) { struct_span_err!( self.tcx.sess, - item.span, + span, E0390, "cannot define inherent `impl` for a type outside of the crate where the type is defined", ) .help(INTO_DEFINING_CRATE) - .span_help(item.span, ADD_ATTR_TO_TY) + .span_help(span, ADD_ATTR_TO_TY) .emit(); return; } @@ -93,12 +93,12 @@ impl<'tcx> InherentCollect<'tcx> { { struct_span_err!( self.tcx.sess, - item.span, + span, E0390, "cannot define inherent `impl` for a type outside of the crate where the type is defined", ) .help(INTO_DEFINING_CRATE) - .span_help(impl_item.span, ADD_ATTR) + .span_help(self.tcx.hir().span(impl_item.id.hir_id()), ADD_ATTR) .emit(); return; } @@ -112,12 +112,12 @@ impl<'tcx> InherentCollect<'tcx> { } else { struct_span_err!( self.tcx.sess, - item.span, + span, E0116, "cannot define inherent `impl` for a type outside of the crate \ where the type is defined" ) - .span_label(item.span, "impl for type defined outside of crate.") + .span_label(span, "impl for type defined outside of crate.") .note("define and implement a trait or new type instead") .emit(); } @@ -182,29 +182,30 @@ impl<'tcx> InherentCollect<'tcx> { } let item = self.tcx.hir().item(id); - let hir::ItemKind::Impl(hir::Impl { of_trait: None, self_ty: ty, items, .. }) = item.kind else { + let impl_span = self.tcx.hir().span(id.hir_id()); + let hir::ItemKind::Impl(hir::Impl { of_trait: None, items, .. }) = item.kind else { return; }; let self_ty = self.tcx.type_of(item.owner_id); match *self_ty.kind() { ty::Adt(def, _) => { - self.check_def_id(item, self_ty, def.did()); + self.check_def_id(item, self_ty, def.did(), impl_span); } ty::Foreign(did) => { - self.check_def_id(item, self_ty, did); + self.check_def_id(item, self_ty, did, impl_span); } ty::Dynamic(data, ..) if data.principal_def_id().is_some() => { - self.check_def_id(item, self_ty, data.principal_def_id().unwrap()); + self.check_def_id(item, self_ty, data.principal_def_id().unwrap(), impl_span); } ty::Dynamic(..) => { struct_span_err!( self.tcx.sess, - ty.span, + impl_span, E0785, "cannot define inherent `impl` for a dyn auto trait" ) - .span_label(ty.span, "impl requires at least one non-auto trait") + .span_label(impl_span, "impl requires at least one non-auto trait") .note("define and implement a new trait or type instead") .emit(); } @@ -221,17 +222,17 @@ impl<'tcx> InherentCollect<'tcx> { | ty::Never | ty::FnPtr(_) | ty::Tuple(..) => { - self.check_primitive_impl(item.owner_id.def_id, self_ty, items, ty.span) + self.check_primitive_impl(item.owner_id.def_id, self_ty, items, impl_span) } ty::Alias(..) | ty::Param(_) => { let mut err = struct_span_err!( self.tcx.sess, - ty.span, + impl_span, E0118, "no nominal type found for inherent implementation" ); - err.span_label(ty.span, "impl requires a nominal type") + err.span_label(impl_span, "impl requires a nominal type") .note("either implement a trait on it or create a newtype to wrap it instead"); err.emit(); diff --git a/tests/ui/coherence/issue-85026.stderr b/tests/ui/coherence/issue-85026.stderr index a5da19bbfaa..fb6e9976583 100644 --- a/tests/ui/coherence/issue-85026.stderr +++ b/tests/ui/coherence/issue-85026.stderr @@ -1,16 +1,16 @@ error[E0785]: cannot define inherent `impl` for a dyn auto trait - --> $DIR/issue-85026.rs:5:6 + --> $DIR/issue-85026.rs:5:1 | LL | impl dyn AutoTrait {} - | ^^^^^^^^^^^^^ impl requires at least one non-auto trait + | ^^^^^^^^^^^^^^^^^^ impl requires at least one non-auto trait | = note: define and implement a new trait or type instead error[E0785]: cannot define inherent `impl` for a dyn auto trait - --> $DIR/issue-85026.rs:8:6 + --> $DIR/issue-85026.rs:8:1 | LL | impl dyn Unpin {} - | ^^^^^^^^^ impl requires at least one non-auto trait + | ^^^^^^^^^^^^^^ impl requires at least one non-auto trait | = note: define and implement a new trait or type instead diff --git a/tests/ui/const-generics/wrong-normalization.stderr b/tests/ui/const-generics/wrong-normalization.stderr index fb806bdb1e7..658a8406608 100644 --- a/tests/ui/const-generics/wrong-normalization.stderr +++ b/tests/ui/const-generics/wrong-normalization.stderr @@ -1,8 +1,8 @@ error[E0118]: no nominal type found for inherent implementation - --> $DIR/wrong-normalization.rs:16:6 + --> $DIR/wrong-normalization.rs:16:1 | LL | impl as Identity>::Identity { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type | = note: either implement a trait on it or create a newtype to wrap it instead diff --git a/tests/ui/error-codes/E0116.stderr b/tests/ui/error-codes/E0116.stderr index a5ceeb4a55d..8a027686760 100644 --- a/tests/ui/error-codes/E0116.stderr +++ b/tests/ui/error-codes/E0116.stderr @@ -2,7 +2,7 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher --> $DIR/E0116.rs:1:1 | LL | impl Vec {} - | ^^^^^^^^^^^^^^^ impl for type defined outside of crate. + | ^^^^^^^^^^^^ impl for type defined outside of crate. | = note: define and implement a trait or new type instead diff --git a/tests/ui/error-codes/E0118.stderr b/tests/ui/error-codes/E0118.stderr index 8c6fa7947a8..442f8a4f870 100644 --- a/tests/ui/error-codes/E0118.stderr +++ b/tests/ui/error-codes/E0118.stderr @@ -1,8 +1,8 @@ error[E0118]: no nominal type found for inherent implementation - --> $DIR/E0118.rs:1:9 + --> $DIR/E0118.rs:1:1 | LL | impl T { - | ^ impl requires a nominal type + | ^^^^^^^^^ impl requires a nominal type | = note: either implement a trait on it or create a newtype to wrap it instead diff --git a/tests/ui/error-codes/E0390.stderr b/tests/ui/error-codes/E0390.stderr index 0e5a9ca762b..ec4b5758c5b 100644 --- a/tests/ui/error-codes/E0390.stderr +++ b/tests/ui/error-codes/E0390.stderr @@ -1,16 +1,16 @@ error[E0390]: cannot define inherent `impl` for primitive types - --> $DIR/E0390.rs:5:6 + --> $DIR/E0390.rs:5:1 | LL | impl *mut Foo {} - | ^^^^^^^^ + | ^^^^^^^^^^^^^ | = help: consider using an extension trait instead error[E0390]: cannot define inherent `impl` for primitive types - --> $DIR/E0390.rs:7:6 + --> $DIR/E0390.rs:7:1 | LL | impl fn(Foo) {} - | ^^^^^^^ + | ^^^^^^^^^^^^ | = help: consider using an extension trait instead diff --git a/tests/ui/impl-trait/where-allowed.stderr b/tests/ui/impl-trait/where-allowed.stderr index 3ad0a9f9d5c..e3a9caa6460 100644 --- a/tests/ui/impl-trait/where-allowed.stderr +++ b/tests/ui/impl-trait/where-allowed.stderr @@ -303,10 +303,10 @@ LL | fn in_method_generic_param_default(_: T) {} = note: for more information, see issue #36887 error[E0118]: no nominal type found for inherent implementation - --> $DIR/where-allowed.rs:233:23 + --> $DIR/where-allowed.rs:233:1 | LL | impl T {} - | ^ impl requires a nominal type + | ^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type | = note: either implement a trait on it or create a newtype to wrap it instead diff --git a/tests/ui/incoherent-inherent-impls/needs-has-incoherent-impls.stderr b/tests/ui/incoherent-inherent-impls/needs-has-incoherent-impls.stderr index 8f70825115d..f5900afe2dc 100644 --- a/tests/ui/incoherent-inherent-impls/needs-has-incoherent-impls.stderr +++ b/tests/ui/incoherent-inherent-impls/needs-has-incoherent-impls.stderr @@ -1,114 +1,80 @@ error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined --> $DIR/needs-has-incoherent-impls.rs:5:1 | -LL | / impl extern_crate::StructWithAttr { -LL | | -LL | | fn foo() {} -LL | | } - | |_^ +LL | impl extern_crate::StructWithAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider moving this inherent impl into the crate defining the type if possible help: alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items --> $DIR/needs-has-incoherent-impls.rs:7:5 | LL | fn foo() {} - | ^^^^^^^^^^^ + | ^^^^^^^^ error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined --> $DIR/needs-has-incoherent-impls.rs:13:1 | -LL | / impl extern_crate::StructNoAttr { -LL | | -LL | | fn foo() {} -LL | | } - | |_^ +LL | impl extern_crate::StructNoAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider moving this inherent impl into the crate defining the type if possible help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items --> $DIR/needs-has-incoherent-impls.rs:13:1 | -LL | / impl extern_crate::StructNoAttr { -LL | | -LL | | fn foo() {} -LL | | } - | |_^ +LL | impl extern_crate::StructNoAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined --> $DIR/needs-has-incoherent-impls.rs:17:1 | -LL | / impl extern_crate::StructNoAttr { -LL | | -LL | | #[rustc_allow_incoherent_impl] -LL | | fn bar() {} -LL | | } - | |_^ +LL | impl extern_crate::StructNoAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider moving this inherent impl into the crate defining the type if possible help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items --> $DIR/needs-has-incoherent-impls.rs:17:1 | -LL | / impl extern_crate::StructNoAttr { -LL | | -LL | | #[rustc_allow_incoherent_impl] -LL | | fn bar() {} -LL | | } - | |_^ +LL | impl extern_crate::StructNoAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined --> $DIR/needs-has-incoherent-impls.rs:22:1 | -LL | / impl extern_crate::EnumWithAttr { -LL | | -LL | | fn foo() {} -LL | | } - | |_^ +LL | impl extern_crate::EnumWithAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider moving this inherent impl into the crate defining the type if possible help: alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items --> $DIR/needs-has-incoherent-impls.rs:24:5 | LL | fn foo() {} - | ^^^^^^^^^^^ + | ^^^^^^^^ error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined --> $DIR/needs-has-incoherent-impls.rs:30:1 | -LL | / impl extern_crate::EnumNoAttr { -LL | | -LL | | fn foo() {} -LL | | } - | |_^ +LL | impl extern_crate::EnumNoAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider moving this inherent impl into the crate defining the type if possible help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items --> $DIR/needs-has-incoherent-impls.rs:30:1 | -LL | / impl extern_crate::EnumNoAttr { -LL | | -LL | | fn foo() {} -LL | | } - | |_^ +LL | impl extern_crate::EnumNoAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined --> $DIR/needs-has-incoherent-impls.rs:34:1 | -LL | / impl extern_crate::EnumNoAttr { -LL | | -LL | | #[rustc_allow_incoherent_impl] -LL | | fn bar() {} -LL | | } - | |_^ +LL | impl extern_crate::EnumNoAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: consider moving this inherent impl into the crate defining the type if possible help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items --> $DIR/needs-has-incoherent-impls.rs:34:1 | -LL | / impl extern_crate::EnumNoAttr { -LL | | -LL | | #[rustc_allow_incoherent_impl] -LL | | fn bar() {} -LL | | } - | |_^ +LL | impl extern_crate::EnumNoAttr { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 6 previous errors diff --git a/tests/ui/incoherent-inherent-impls/no-attr-empty-impl.stderr b/tests/ui/incoherent-inherent-impls/no-attr-empty-impl.stderr index b3f8b51d0ea..6dc1680cf89 100644 --- a/tests/ui/incoherent-inherent-impls/no-attr-empty-impl.stderr +++ b/tests/ui/incoherent-inherent-impls/no-attr-empty-impl.stderr @@ -2,7 +2,7 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher --> $DIR/no-attr-empty-impl.rs:4:1 | LL | impl extern_crate::StructWithAttr {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. | = note: define and implement a trait or new type instead @@ -10,7 +10,7 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher --> $DIR/no-attr-empty-impl.rs:7:1 | LL | impl extern_crate::StructNoAttr {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. | = note: define and implement a trait or new type instead @@ -18,7 +18,7 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher --> $DIR/no-attr-empty-impl.rs:10:1 | LL | impl extern_crate::EnumWithAttr {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. | = note: define and implement a trait or new type instead @@ -26,15 +26,15 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher --> $DIR/no-attr-empty-impl.rs:13:1 | LL | impl extern_crate::EnumNoAttr {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. | = note: define and implement a trait or new type instead error[E0390]: cannot define inherent `impl` for primitive types - --> $DIR/no-attr-empty-impl.rs:16:6 + --> $DIR/no-attr-empty-impl.rs:16:1 | LL | impl f32 {} - | ^^^ + | ^^^^^^^^ | = help: consider using an extension trait instead diff --git a/tests/ui/kinds-of-primitive-impl.stderr b/tests/ui/kinds-of-primitive-impl.stderr index f4dbd1c40e8..21aac58f1f2 100644 --- a/tests/ui/kinds-of-primitive-impl.stderr +++ b/tests/ui/kinds-of-primitive-impl.stderr @@ -1,32 +1,32 @@ error[E0390]: cannot define inherent `impl` for primitive types - --> $DIR/kinds-of-primitive-impl.rs:1:6 + --> $DIR/kinds-of-primitive-impl.rs:1:1 | LL | impl u8 { - | ^^ + | ^^^^^^^ | = help: consider using an extension trait instead error[E0390]: cannot define inherent `impl` for primitive types - --> $DIR/kinds-of-primitive-impl.rs:6:6 + --> $DIR/kinds-of-primitive-impl.rs:6:1 | LL | impl str { - | ^^^ + | ^^^^^^^^ | = help: consider using an extension trait instead error[E0390]: cannot define inherent `impl` for primitive types - --> $DIR/kinds-of-primitive-impl.rs:12:6 + --> $DIR/kinds-of-primitive-impl.rs:12:1 | LL | impl char { - | ^^^^ + | ^^^^^^^^^ | = help: consider using an extension trait instead error[E0390]: cannot define inherent `impl` for primitive types - --> $DIR/kinds-of-primitive-impl.rs:21:6 + --> $DIR/kinds-of-primitive-impl.rs:21:1 | LL | impl &MyType { - | ^^^^^^^ + | ^^^^^^^^^^^^ | = help: consider using an extension trait instead = note: you could also try moving the reference to uses of `MyType` (such as `self`) within the implementation diff --git a/tests/ui/privacy/private-in-public-ill-formed.stderr b/tests/ui/privacy/private-in-public-ill-formed.stderr index e7c94bc301b..abc8538e5b3 100644 --- a/tests/ui/privacy/private-in-public-ill-formed.stderr +++ b/tests/ui/privacy/private-in-public-ill-formed.stderr @@ -1,16 +1,16 @@ error[E0118]: no nominal type found for inherent implementation - --> $DIR/private-in-public-ill-formed.rs:14:10 + --> $DIR/private-in-public-ill-formed.rs:14:5 | LL | impl ::AssocAlias { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type | = note: either implement a trait on it or create a newtype to wrap it instead error[E0118]: no nominal type found for inherent implementation - --> $DIR/private-in-public-ill-formed.rs:31:10 + --> $DIR/private-in-public-ill-formed.rs:31:5 | LL | impl ::AssocAlias { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type | = note: either implement a trait on it or create a newtype to wrap it instead diff --git a/tests/ui/traits/trait-or-new-type-instead.stderr b/tests/ui/traits/trait-or-new-type-instead.stderr index 4726b0668e5..6fd8a03fd8f 100644 --- a/tests/ui/traits/trait-or-new-type-instead.stderr +++ b/tests/ui/traits/trait-or-new-type-instead.stderr @@ -1,11 +1,8 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined --> $DIR/trait-or-new-type-instead.rs:1:1 | -LL | / impl Option { -LL | | -LL | | pub fn foo(&self) { } -LL | | } - | |_^ impl for type defined outside of crate. +LL | impl Option { + | ^^^^^^^^^^^^^^^^^ impl for type defined outside of crate. | = note: define and implement a trait or new type instead