Skip to content

Commit b9317c7

Browse files
committed
Special case 'generic param from outer item' message for Self
1 parent 059e49f commit b9317c7

File tree

8 files changed

+20
-11
lines changed

8 files changed

+20
-11
lines changed

Diff for: compiler/rustc_resolve/messages.ftl

+8-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,14 @@ resolve_forward_declared_generic_param =
114114
.label = defaulted generic parameters cannot be forward declared
115115
116116
resolve_generic_params_from_outer_item =
117-
can't use generic parameters from outer item
118-
.label = use of generic parameter from outer item
117+
can't use {$is_self ->
118+
[true] `Self`
119+
*[false] generic parameters
120+
} from outer item
121+
.label = use of {$is_self ->
122+
[true] `Self`
123+
*[false] generic parameter
124+
} from outer item
119125
.refer_to_type_directly = refer to the type directly here instead
120126
.suggestion = try introducing a local generic parameter here
121127

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
570570
IsStaticOrConst::Const => Some(errs::GenericParamsFromOuterItemStaticOrConst::Const),
571571
IsStaticOrConst::No => None,
572572
};
573+
let is_self = matches!(outer_res, Res::SelfTyParam { .. } | Res::SelfTyAlias { .. });
573574
let mut err = errs::GenericParamsFromOuterItem {
574575
span,
575576
label: None,
576577
refer_to_type_directly: None,
577578
sugg: None,
578579
static_or_const,
580+
is_self,
579581
};
580582
let sm = self.tcx.sess.source_map();
581583
let def_id = match outer_res {

Diff for: compiler/rustc_resolve/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub(crate) struct GenericParamsFromOuterItem {
4646
pub(crate) sugg: Option<GenericParamsFromOuterItemSugg>,
4747
#[subdiagnostic]
4848
pub(crate) static_or_const: Option<GenericParamsFromOuterItemStaticOrConst>,
49+
pub(crate) is_self: bool,
4950
}
5051

5152
#[derive(Subdiagnostic)]

Diff for: tests/ui/error-codes/E0401.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | fn baz<U,
2020
LL | (y: T) {
2121
| ^ use of generic parameter from outer item
2222

23-
error[E0401]: can't use generic parameters from outer item
23+
error[E0401]: can't use `Self` from outer item
2424
--> $DIR/E0401.rs:24:25
2525
|
2626
LL | impl<T> Iterator for A<T> {
@@ -29,7 +29,7 @@ LL | impl<T> Iterator for A<T> {
2929
LL | fn helper(sel: &Self) -> u8 {
3030
| ^^^^
3131
| |
32-
| use of generic parameter from outer item
32+
| use of `Self` from outer item
3333
| refer to the type directly here instead
3434

3535
error[E0283]: type annotations needed

Diff for: tests/ui/resolve/issue-12796.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
trait Trait {
22
fn outer(&self) {
33
fn inner(_: &Self) {
4-
//~^ ERROR can't use generic parameters from outer item
4+
//~^ ERROR can't use `Self` from outer item
55
}
66
}
77
}

Diff for: tests/ui/resolve/issue-12796.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0401]: can't use generic parameters from outer item
1+
error[E0401]: can't use `Self` from outer item
22
--> $DIR/issue-12796.rs:3:22
33
|
44
LL | fn inner(_: &Self) {
55
| ^^^^
66
| |
7-
| use of generic parameter from outer item
7+
| use of `Self` from outer item
88
| can't use `Self` here
99

1010
error: aborting due to 1 previous error

Diff for: tests/ui/resolve/use-self-in-inner-fn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ impl A {
44
//~^ NOTE `Self` type implicitly declared here, by this `impl`
55
fn banana(&mut self) {
66
fn peach(this: &Self) {
7-
//~^ ERROR can't use generic parameters from outer item
8-
//~| NOTE use of generic parameter from outer item
7+
//~^ ERROR can't use `Self` from outer item
8+
//~| NOTE use of `Self` from outer item
99
//~| NOTE refer to the type directly here instead
1010
}
1111
}

Diff for: tests/ui/resolve/use-self-in-inner-fn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0401]: can't use generic parameters from outer item
1+
error[E0401]: can't use `Self` from outer item
22
--> $DIR/use-self-in-inner-fn.rs:6:25
33
|
44
LL | impl A {
@@ -7,7 +7,7 @@ LL | impl A {
77
LL | fn peach(this: &Self) {
88
| ^^^^
99
| |
10-
| use of generic parameter from outer item
10+
| use of `Self` from outer item
1111
| refer to the type directly here instead
1212

1313
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)