Skip to content

Commit

Permalink
Special case 'generic param from outer item' message for Self
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Jan 13, 2024
1 parent 36c8f9b commit 483852a
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 11 deletions.
10 changes: 8 additions & 2 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ resolve_forward_declared_generic_param =
.label = defaulted generic parameters cannot be forward declared
resolve_generic_params_from_outer_item =
can't use generic parameters from outer item
.label = use of generic parameter from outer item
can't use {$is_self ->
[true] `Self`
*[false] generic parameters
} from outer item
.label = use of {$is_self ->
[true] `Self`
*[false] generic parameter
} from outer item
.refer_to_type_directly = refer to the type directly here instead
.suggestion = try introducing a local generic parameter here
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
DefKind::Const => Some(errs::GenericParamsFromOuterItemStaticOrConst::Const),
_ => None,
};
let is_self = matches!(outer_res, Res::SelfTyParam { .. } | Res::SelfTyAlias { .. });
let mut err = errs::GenericParamsFromOuterItem {
span,
label: None,
refer_to_type_directly: None,
sugg: None,
static_or_const,
is_self,
};

let sm = self.tcx.sess.source_map();
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub(crate) struct GenericParamsFromOuterItem {
pub(crate) sugg: Option<GenericParamsFromOuterItemSugg>,
#[subdiagnostic]
pub(crate) static_or_const: Option<GenericParamsFromOuterItemStaticOrConst>,
pub(crate) is_self: bool,
}

#[derive(Subdiagnostic)]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/error-codes/E0401.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LL | fn baz<U,
LL | (y: T) {
| ^ use of generic parameter from outer item

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

error[E0283]: type annotations needed
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/resolve/issue-12796.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
trait Trait {
fn outer(&self) {
fn inner(_: &Self) {
//~^ ERROR can't use generic parameters from outer item
//~^ ERROR can't use `Self` from outer item
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/resolve/issue-12796.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0401]: can't use generic parameters from outer item
error[E0401]: can't use `Self` from outer item
--> $DIR/issue-12796.rs:3:22
|
LL | fn inner(_: &Self) {
| ^^^^
| |
| use of generic parameter from outer item
| use of `Self` from outer item
| can't use `Self` here

error: aborting due to 1 previous error
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/resolve/use-self-in-inner-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ impl A {
//~^ NOTE `Self` type implicitly declared here, by this `impl`
fn banana(&mut self) {
fn peach(this: &Self) {
//~^ ERROR can't use generic parameters from outer item
//~| NOTE use of generic parameter from outer item
//~^ ERROR can't use `Self` from outer item
//~| NOTE use of `Self` from outer item
//~| NOTE refer to the type directly here instead
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/resolve/use-self-in-inner-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0401]: can't use generic parameters from outer item
error[E0401]: can't use `Self` from outer item
--> $DIR/use-self-in-inner-fn.rs:6:25
|
LL | impl A {
Expand All @@ -7,7 +7,7 @@ LL | impl A {
LL | fn peach(this: &Self) {
| ^^^^
| |
| use of generic parameter from outer item
| use of `Self` from outer item
| refer to the type directly here instead

error: aborting due to 1 previous error
Expand Down

0 comments on commit 483852a

Please sign in to comment.