Skip to content

Commit 8fdd347

Browse files
committed
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` ```
1 parent f693870 commit 8fdd347

File tree

7 files changed

+40
-3
lines changed

7 files changed

+40
-3
lines changed

compiler/rustc_hir_typeck/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ hir_typeck_self_ctor_from_outer_item = can't reference `Self` constructor from o
262262
.label = the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
263263
.suggestion = replace `Self` with the actual type
264264
265+
hir_typeck_self_ctor_from_outer_item_inner_item = `Self` used in this inner item
266+
265267
hir_typeck_slicing_suggestion = consider slicing here
266268
267269
hir_typeck_struct_expr_non_exhaustive =

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,15 @@ pub(crate) struct SelfCtorFromOuterItem {
978978
pub impl_span: Span,
979979
#[subdiagnostic]
980980
pub sugg: Option<ReplaceWithName>,
981+
#[subdiagnostic]
982+
pub item: Option<InnerItem>,
983+
}
984+
985+
#[derive(Subdiagnostic)]
986+
#[label(hir_typeck_self_ctor_from_outer_item_inner_item)]
987+
pub(crate) struct InnerItem {
988+
#[primary_span]
989+
pub span: Span,
981990
}
982991

983992
#[derive(LintDiagnostic)]
@@ -987,6 +996,8 @@ pub(crate) struct SelfCtorFromOuterItemLint {
987996
pub impl_span: Span,
988997
#[subdiagnostic]
989998
pub sugg: Option<ReplaceWithName>,
999+
#[subdiagnostic]
1000+
pub item: Option<InnerItem>,
9901001
}
9911002

9921003
#[derive(Subdiagnostic)]

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,11 +1094,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10941094
span: path_span,
10951095
name: self.tcx.item_name(def.did()).to_ident_string(),
10961096
});
1097+
let item = match self
1098+
.tcx
1099+
.hir_node_by_def_id(self.tcx.hir_get_parent_item(hir_id).def_id)
1100+
{
1101+
hir::Node::Item(item) => Some(errors::InnerItem {
1102+
span: item.kind.ident().map(|i| i.span).unwrap_or(item.span),
1103+
}),
1104+
_ => None,
1105+
};
10971106
if ty.raw.has_param() {
10981107
let guar = self.dcx().emit_err(errors::SelfCtorFromOuterItem {
10991108
span: path_span,
11001109
impl_span: tcx.def_span(impl_def_id),
11011110
sugg,
1111+
item,
11021112
});
11031113
return (Ty::new_error(self.tcx, guar), res);
11041114
} else {
@@ -1109,6 +1119,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11091119
errors::SelfCtorFromOuterItemLint {
11101120
impl_span: tcx.def_span(impl_def_id),
11111121
sugg,
1122+
item,
11121123
},
11131124
);
11141125
}

tests/ui/malformed/do-not-ice-on-note_and_explain.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ error[E0401]: can't reference `Self` constructor from outer item
33
|
44
LL | impl<B> A<B> {
55
| ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
6-
...
6+
LL | fn d() {
7+
LL | fn d() {
8+
| - `Self` used in this inner item
79
LL | Self(1)
810
| ^^^^ help: replace `Self` with the actual type: `A`
911

tests/ui/self/self-ctor-nongeneric.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ LL | impl S0 {
55
| ------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
66
LL | fn foo() {
77
LL | const C: S0 = Self(0);
8-
| ^^^^ help: replace `Self` with the actual type: `S0`
8+
| - ^^^^ help: replace `Self` with the actual type: `S0`
9+
| |
10+
| `Self` used in this inner item
911
|
1012
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1113
= 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
1719
LL | impl S0 {
1820
| ------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
1921
...
22+
LL | fn bar() -> S0 {
23+
| --- `Self` used in this inner item
2024
LL | Self(0)
2125
| ^^^^ help: replace `Self` with the actual type: `S0`
2226
|

tests/ui/self/self-ctor.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ LL | impl<T> S0<T> {
55
| ------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
66
LL | fn foo() {
77
LL | const C: S0<i32> = Self(0);
8-
| ^^^^ help: replace `Self` with the actual type: `S0`
8+
| - ^^^^ help: replace `Self` with the actual type: `S0`
9+
| |
10+
| `Self` used in this inner item
911

1012
error[E0401]: can't reference `Self` constructor from outer item
1113
--> $DIR/self-ctor.rs:8:13
1214
|
1315
LL | impl<T> S0<T> {
1416
| ------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
1517
...
18+
LL | fn bar() -> S0<i32> {
19+
| --- `Self` used in this inner item
1620
LL | Self(0)
1721
| ^^^^ help: replace `Self` with the actual type: `S0`
1822

tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ error[E0401]: can't reference `Self` constructor from outer item
2424
LL | impl<'a, T> Foo<'a> for Repeated<T> {
2525
| ----------------------------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference
2626
...
27+
LL | fn inner<Q>(value: Option<()>) -> Repeated<Q> {
28+
| ----- `Self` used in this inner item
29+
LL | match value {
2730
LL | _ => Self(unimplemented!()),
2831
| ^^^^ help: replace `Self` with the actual type: `Repeated`
2932

0 commit comments

Comments
 (0)