Skip to content

Minor improvements on error for Self type in items that don't allow it #98274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,8 @@ impl<'a> Resolver<'a> {
};
}
(msg, None)
} else if ident.name == kw::SelfUpper {
("`Self` is only available in impls, traits, and type definitions".to_string(), None)
} else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
// Check whether the name refers to an item in the value namespace.
let binding = if let Some(ribs) = ribs {
Expand Down
21 changes: 20 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
span,
"`Self` is only available in impls, traits, and type definitions".to_string(),
);
if let Some(item_kind) = self.diagnostic_metadata.current_item {
err.span_label(
item_kind.ident.span,
format!(
"`Self` not allowed in {} {}",
item_kind.kind.article(),
item_kind.kind.descr()
),
);
}
return (err, Vec::new());
}
if is_self_value(path, ns) {
Expand Down Expand Up @@ -389,6 +399,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
);
}
}
} else if let Some(item_kind) = self.diagnostic_metadata.current_item {
err.span_label(
item_kind.ident.span,
format!(
"`self` not allowed in {} {}",
item_kind.kind.article(),
item_kind.kind.descr()
),
);
}
return (err, Vec::new());
}
Expand Down Expand Up @@ -1788,7 +1807,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
path: &[Segment],
) -> Option<(Span, &'static str, String, Applicability)> {
let (ident, span) = match path {
[segment] if !segment.has_generic_args => {
[segment] if !segment.has_generic_args && segment.ident.name != kw::SelfUpper => {
(segment.ident.to_string(), segment.ident.span)
}
_ => return None,
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/error-codes/E0411.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0411]: cannot find type `Self` in this scope
--> $DIR/E0411.rs:2:6
|
LL | fn main() {
| ---- `Self` not allowed in a function
LL | <Self>::foo;
| ^^^^ `Self` is only available in impls, traits, and type definitions

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lifetimes/issue-97194.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern "C" {
fn bget(&self, index: [usize; Self::DIM]) -> bool {
//~^ ERROR incorrect function inside `extern` block
//~| ERROR `self` parameter is only allowed in associated functions
//~| ERROR use of undeclared type `Self`
//~| ERROR failed to resolve: `Self`
type T<'a> = &'a str;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/lifetimes/issue-97194.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ LL | fn bget(&self, index: [usize; Self::DIM]) -> bool {
|
= note: associated functions are those in `impl` or `trait` definitions

error[E0433]: failed to resolve: use of undeclared type `Self`
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
--> $DIR/issue-97194.rs:2:35
|
LL | fn bget(&self, index: [usize; Self::DIM]) -> bool {
| ^^^^ use of undeclared type `Self`
| ^^^^ `Self` is only available in impls, traits, and type definitions

error: aborting due to 3 previous errors

Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/resolve/issue-24968.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
// Also includes more Self usages per #93796

fn foo(_: Self) {
//~^ ERROR cannot find type `Self`
}

fn foo2() {
let x: Self;
//~^ ERROR cannot find type `Self`
}

type Foo<T>
where
Self: Clone,
//~^ ERROR cannot find type `Self`
= Vec<T>;

const FOO: Self = 0;
//~^ ERROR cannot find type `Self`

const FOO2: u32 = Self::bar();
//~^ ERROR failed to resolve: `Self`

static FOO_S: Self = 0;
//~^ ERROR cannot find type `Self`

static FOO_S2: u32 = Self::bar();
//~^ ERROR failed to resolve: `Self`

fn main() {}
56 changes: 52 additions & 4 deletions src/test/ui/resolve/issue-24968.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
--> $DIR/issue-24968.rs:21:19
|
LL | const FOO2: u32 = Self::bar();
| ^^^^ `Self` is only available in impls, traits, and type definitions

error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
--> $DIR/issue-24968.rs:27:22
|
LL | static FOO_S2: u32 = Self::bar();
| ^^^^ `Self` is only available in impls, traits, and type definitions

error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-24968.rs:1:11
--> $DIR/issue-24968.rs:3:11
|
LL | fn foo(_: Self) {
| ^^^^ `Self` is only available in impls, traits, and type definitions
| --- ^^^^ `Self` is only available in impls, traits, and type definitions
| |
| `Self` not allowed in a function

error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-24968.rs:8:12
|
LL | fn foo2() {
| ---- `Self` not allowed in a function
LL | let x: Self;
| ^^^^ `Self` is only available in impls, traits, and type definitions

error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-24968.rs:14:5
|
LL | type Foo<T>
| --- `Self` not allowed in a type alias
LL | where
LL | Self: Clone,
| ^^^^ `Self` is only available in impls, traits, and type definitions

error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-24968.rs:18:12
|
LL | const FOO: Self = 0;
| --- ^^^^ `Self` is only available in impls, traits, and type definitions
| |
| `Self` not allowed in a constant item

error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-24968.rs:24:15
|
LL | static FOO_S: Self = 0;
| ----- ^^^^ `Self` is only available in impls, traits, and type definitions
| |
| `Self` not allowed in a static item

error: aborting due to previous error
error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0411`.
Some errors have detailed explanations: E0411, E0433.
For more information about an error, try `rustc --explain E0411`.
2 changes: 1 addition & 1 deletion src/test/ui/type-alias/issue-62263-self-in-atb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pub trait Trait {
}

pub type Alias = dyn Trait<A = Self::A>;
//~^ ERROR failed to resolve: use of undeclared type `Self` [E0433]
//~^ ERROR failed to resolve: `Self`

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/type-alias/issue-62263-self-in-atb.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type `Self`
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
--> $DIR/issue-62263-self-in-atb.rs:5:32
|
LL | pub type Alias = dyn Trait<A = Self::A>;
| ^^^^ use of undeclared type `Self`
| ^^^^ `Self` is only available in impls, traits, and type definitions

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/type-alias/issue-62305-self-assoc-ty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Alias = Self::Target;
//~^ ERROR failed to resolve: use of undeclared type `Self` [E0433]
//~^ ERROR failed to resolve: `Self`

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/type-alias/issue-62305-self-assoc-ty.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0433]: failed to resolve: use of undeclared type `Self`
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
--> $DIR/issue-62305-self-assoc-ty.rs:1:14
|
LL | type Alias = Self::Target;
| ^^^^ use of undeclared type `Self`
| ^^^^ `Self` is only available in impls, traits, and type definitions

error: aborting due to previous error

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/type-alias/issue-62364-self-ty-arg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-62364-self-ty-arg.rs:5:29
|
LL | type Alias<'a> = Struct<&'a Self>;
| - ^^^^ `Self` is only available in impls, traits, and type definitions
| |
| help: you might be missing a type parameter: `, Self`
| ----- ^^^^ `Self` is only available in impls, traits, and type definitions
| |
| `Self` not allowed in a type alias

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/use/use-self-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ impl S {
fn f() {}
fn g() {
use Self::f; //~ ERROR unresolved import
pub(in Self::f) struct Z; //~ ERROR use of undeclared type `Self`
pub(in Self::f) struct Z; //~ ERROR failed to resolve: `Self`
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/use/use-self-type.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0433]: failed to resolve: use of undeclared type `Self`
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
--> $DIR/use-self-type.rs:7:16
|
LL | pub(in Self::f) struct Z;
| ^^^^ use of undeclared type `Self`
| ^^^^ `Self` is only available in impls, traits, and type definitions

error[E0432]: unresolved import `Self`
--> $DIR/use-self-type.rs:6:13
|
LL | use Self::f;
| ^^^^ use of undeclared type `Self`
| ^^^^ `Self` is only available in impls, traits, and type definitions

error: aborting due to 2 previous errors

Expand Down