diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index e7c8886f054..86dbcba6c0d 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1914,6 +1914,8 @@ pub(crate) fn report_path_resolution_error( }; } (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 { diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 1a99bff610a..6b0b83d2cb5 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -332,6 +332,16 @@ struct BaseError<'a> { 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 @@ struct BaseError<'a> { ); } } + } 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()); } diff --git a/src/test/ui/error-codes/E0411.stderr b/src/test/ui/error-codes/E0411.stderr index c1c25e835c1..4c99f9fcbf1 100644 --- a/src/test/ui/error-codes/E0411.stderr +++ b/src/test/ui/error-codes/E0411.stderr @@ -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 | ::foo; | ^^^^ `Self` is only available in impls, traits, and type definitions diff --git a/src/test/ui/lifetimes/issue-97194.rs b/src/test/ui/lifetimes/issue-97194.rs index accb4a99830..5f3560dbe94 100644 --- a/src/test/ui/lifetimes/issue-97194.rs +++ b/src/test/ui/lifetimes/issue-97194.rs @@ -2,7 +2,7 @@ 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; } } diff --git a/src/test/ui/lifetimes/issue-97194.stderr b/src/test/ui/lifetimes/issue-97194.stderr index 15ad5aadf9f..93bde285a99 100644 --- a/src/test/ui/lifetimes/issue-97194.stderr +++ b/src/test/ui/lifetimes/issue-97194.stderr @@ -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 diff --git a/src/test/ui/resolve/issue-24968.rs b/src/test/ui/resolve/issue-24968.rs index 916b48205dc..19e16abcee3 100644 --- a/src/test/ui/resolve/issue-24968.rs +++ b/src/test/ui/resolve/issue-24968.rs @@ -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 +where + Self: Clone, +//~^ ERROR cannot find type `Self` += Vec; + +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() {} diff --git a/src/test/ui/resolve/issue-24968.stderr b/src/test/ui/resolve/issue-24968.stderr index c891952c42b..7e539d25804 100644 --- a/src/test/ui/resolve/issue-24968.stderr +++ b/src/test/ui/resolve/issue-24968.stderr @@ -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: aborting due to previous error +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 -For more information about this error, try `rustc --explain E0411`. +error[E0411]: cannot find type `Self` in this scope + --> $DIR/issue-24968.rs:14:5 + | +LL | type Foo + | --- `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`. diff --git a/src/test/ui/type-alias/issue-62263-self-in-atb.rs b/src/test/ui/type-alias/issue-62263-self-in-atb.rs index 1f64b4cfe5c..91522d8912f 100644 --- a/src/test/ui/type-alias/issue-62263-self-in-atb.rs +++ b/src/test/ui/type-alias/issue-62263-self-in-atb.rs @@ -3,6 +3,6 @@ pub trait Trait { } pub type Alias = dyn Trait; -//~^ ERROR failed to resolve: use of undeclared type `Self` [E0433] +//~^ ERROR failed to resolve: `Self` fn main() {} diff --git a/src/test/ui/type-alias/issue-62263-self-in-atb.stderr b/src/test/ui/type-alias/issue-62263-self-in-atb.stderr index d34b6ed5038..c20074dc27c 100644 --- a/src/test/ui/type-alias/issue-62263-self-in-atb.stderr +++ b/src/test/ui/type-alias/issue-62263-self-in-atb.stderr @@ -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; - | ^^^^ use of undeclared type `Self` + | ^^^^ `Self` is only available in impls, traits, and type definitions error: aborting due to previous error diff --git a/src/test/ui/type-alias/issue-62305-self-assoc-ty.rs b/src/test/ui/type-alias/issue-62305-self-assoc-ty.rs index 999902fb18b..a4d9a285485 100644 --- a/src/test/ui/type-alias/issue-62305-self-assoc-ty.rs +++ b/src/test/ui/type-alias/issue-62305-self-assoc-ty.rs @@ -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() {} diff --git a/src/test/ui/type-alias/issue-62305-self-assoc-ty.stderr b/src/test/ui/type-alias/issue-62305-self-assoc-ty.stderr index 823a5fa50fc..f3da50df926 100644 --- a/src/test/ui/type-alias/issue-62305-self-assoc-ty.stderr +++ b/src/test/ui/type-alias/issue-62305-self-assoc-ty.stderr @@ -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 diff --git a/src/test/ui/type-alias/issue-62364-self-ty-arg.stderr b/src/test/ui/type-alias/issue-62364-self-ty-arg.stderr index cae41672ead..6063512e294 100644 --- a/src/test/ui/type-alias/issue-62364-self-ty-arg.stderr +++ b/src/test/ui/type-alias/issue-62364-self-ty-arg.stderr @@ -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 diff --git a/src/test/ui/use/use-self-type.rs b/src/test/ui/use/use-self-type.rs index 370593b2eb2..3b4ce429701 100644 --- a/src/test/ui/use/use-self-type.rs +++ b/src/test/ui/use/use-self-type.rs @@ -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` } } diff --git a/src/test/ui/use/use-self-type.stderr b/src/test/ui/use/use-self-type.stderr index d1469fb3490..e6153941151 100644 --- a/src/test/ui/use/use-self-type.stderr +++ b/src/test/ui/use/use-self-type.stderr @@ -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