Mention what item is using an invalid Self type

This commit is contained in:
Michael Goulet 2022-06-19 19:24:28 -07:00
parent 611e7b9cea
commit 018c319b21
14 changed files with 117 additions and 20 deletions

View File

@ -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 {

View File

@ -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) {
@ -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());
}

View File

@ -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

View File

@ -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;
}
}

View File

@ -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

View File

@ -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() {}

View File

@ -1,9 +1,57 @@
error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-24968.rs:1:11
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
--> $DIR/issue-24968.rs:21:19
|
LL | fn foo(_: Self) {
LL | const FOO2: u32 = Self::bar();
| ^^^^ `Self` is only available in impls, traits, and type definitions
error: aborting due to previous error
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
For more information about this error, try `rustc --explain E0411`.
error[E0411]: cannot find type `Self` in this scope
--> $DIR/issue-24968.rs:3:11
|
LL | fn foo(_: Self) {
| --- ^^^^ `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 7 previous errors
Some errors have detailed explanations: E0411, E0433.
For more information about an error, try `rustc --explain E0411`.

View File

@ -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() {}

View File

@ -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

View File

@ -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() {}

View File

@ -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

View File

@ -2,9 +2,10 @@ 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
| | |
| | help: you might be missing a type parameter: `, Self`
| `Self` not allowed in a type alias
error: aborting due to previous error

View File

@ -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`
}
}

View File

@ -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