Point at inner item on E0401 generated by hir_typeck

```
error[E0401]: can't reference `Self` constructor from outer item
  --> $DIR/do-not-ice-on-note_and_explain.rs:6:13
   |
LL | impl<B> A<B> {
   | ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
LL |     fn d() {
LL |         fn d() {
   |            - `Self` used in this inner item
LL |             Self(1)
   |             ^^^^ help: replace `Self` with the actual type: `A`
```
This commit is contained in:
Esteban Küber 2025-11-01 19:09:56 +00:00
parent f6938709c8
commit 8fdd34713d
7 changed files with 40 additions and 3 deletions

View file

@ -262,6 +262,8 @@ hir_typeck_self_ctor_from_outer_item = can't reference `Self` constructor from o
.label = the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
.suggestion = replace `Self` with the actual type
hir_typeck_self_ctor_from_outer_item_inner_item = `Self` used in this inner item
hir_typeck_slicing_suggestion = consider slicing here
hir_typeck_struct_expr_non_exhaustive =

View file

@ -978,6 +978,15 @@ pub(crate) struct SelfCtorFromOuterItem {
pub impl_span: Span,
#[subdiagnostic]
pub sugg: Option<ReplaceWithName>,
#[subdiagnostic]
pub item: Option<InnerItem>,
}
#[derive(Subdiagnostic)]
#[label(hir_typeck_self_ctor_from_outer_item_inner_item)]
pub(crate) struct InnerItem {
#[primary_span]
pub span: Span,
}
#[derive(LintDiagnostic)]
@ -987,6 +996,8 @@ pub(crate) struct SelfCtorFromOuterItemLint {
pub impl_span: Span,
#[subdiagnostic]
pub sugg: Option<ReplaceWithName>,
#[subdiagnostic]
pub item: Option<InnerItem>,
}
#[derive(Subdiagnostic)]

View file

@ -1094,11 +1094,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: path_span,
name: self.tcx.item_name(def.did()).to_ident_string(),
});
let item = match self
.tcx
.hir_node_by_def_id(self.tcx.hir_get_parent_item(hir_id).def_id)
{
hir::Node::Item(item) => Some(errors::InnerItem {
span: item.kind.ident().map(|i| i.span).unwrap_or(item.span),
}),
_ => None,
};
if ty.raw.has_param() {
let guar = self.dcx().emit_err(errors::SelfCtorFromOuterItem {
span: path_span,
impl_span: tcx.def_span(impl_def_id),
sugg,
item,
});
return (Ty::new_error(self.tcx, guar), res);
} else {
@ -1109,6 +1119,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
errors::SelfCtorFromOuterItemLint {
impl_span: tcx.def_span(impl_def_id),
sugg,
item,
},
);
}

View file

@ -3,7 +3,9 @@ error[E0401]: can't reference `Self` constructor from outer item
|
LL | impl<B> A<B> {
| ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
...
LL | fn d() {
LL | fn d() {
| - `Self` used in this inner item
LL | Self(1)
| ^^^^ help: replace `Self` with the actual type: `A`

View file

@ -5,7 +5,9 @@ LL | impl S0 {
| ------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
LL | fn foo() {
LL | const C: S0 = Self(0);
| ^^^^ help: replace `Self` with the actual type: `S0`
| - ^^^^ help: replace `Self` with the actual type: `S0`
| |
| `Self` used in this inner item
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #124186 <https://github.com/rust-lang/rust/issues/124186>
@ -17,6 +19,8 @@ warning: can't reference `Self` constructor from outer item
LL | impl S0 {
| ------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
...
LL | fn bar() -> S0 {
| --- `Self` used in this inner item
LL | Self(0)
| ^^^^ help: replace `Self` with the actual type: `S0`
|

View file

@ -5,7 +5,9 @@ LL | impl<T> S0<T> {
| ------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
LL | fn foo() {
LL | const C: S0<i32> = Self(0);
| ^^^^ help: replace `Self` with the actual type: `S0`
| - ^^^^ help: replace `Self` with the actual type: `S0`
| |
| `Self` used in this inner item
error[E0401]: can't reference `Self` constructor from outer item
--> $DIR/self-ctor.rs:8:13
@ -13,6 +15,8 @@ error[E0401]: can't reference `Self` constructor from outer item
LL | impl<T> S0<T> {
| ------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
...
LL | fn bar() -> S0<i32> {
| --- `Self` used in this inner item
LL | Self(0)
| ^^^^ help: replace `Self` with the actual type: `S0`

View file

@ -24,6 +24,9 @@ error[E0401]: can't reference `Self` constructor from outer item
LL | impl<'a, T> Foo<'a> for Repeated<T> {
| ----------------------------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
...
LL | fn inner<Q>(value: Option<()>) -> Repeated<Q> {
| ----- `Self` used in this inner item
LL | match value {
LL | _ => Self(unimplemented!()),
| ^^^^ help: replace `Self` with the actual type: `Repeated`