Skip to content

Commit 1888499

Browse files
authored
Rollup merge of #98274 - compiler-errors:self-type-error, r=cjgillot
Minor improvements on error for `Self` type in items that don't allow it Fixes #93796
2 parents f459d8d + 047de83 commit 1888499

14 files changed

+117
-21
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,8 @@ impl<'a> Resolver<'a> {
19141914
};
19151915
}
19161916
(msg, None)
1917+
} else if ident.name == kw::SelfUpper {
1918+
("`Self` is only available in impls, traits, and type definitions".to_string(), None)
19171919
} else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
19181920
// Check whether the name refers to an item in the value namespace.
19191921
let binding = if let Some(ribs) = ribs {

compiler/rustc_resolve/src/late/diagnostics.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
332332
span,
333333
"`Self` is only available in impls, traits, and type definitions".to_string(),
334334
);
335+
if let Some(item_kind) = self.diagnostic_metadata.current_item {
336+
err.span_label(
337+
item_kind.ident.span,
338+
format!(
339+
"`Self` not allowed in {} {}",
340+
item_kind.kind.article(),
341+
item_kind.kind.descr()
342+
),
343+
);
344+
}
335345
return (err, Vec::new());
336346
}
337347
if is_self_value(path, ns) {
@@ -389,6 +399,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
389399
);
390400
}
391401
}
402+
} else if let Some(item_kind) = self.diagnostic_metadata.current_item {
403+
err.span_label(
404+
item_kind.ident.span,
405+
format!(
406+
"`self` not allowed in {} {}",
407+
item_kind.kind.article(),
408+
item_kind.kind.descr()
409+
),
410+
);
392411
}
393412
return (err, Vec::new());
394413
}
@@ -1788,7 +1807,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
17881807
path: &[Segment],
17891808
) -> Option<(Span, &'static str, String, Applicability)> {
17901809
let (ident, span) = match path {
1791-
[segment] if !segment.has_generic_args => {
1810+
[segment] if !segment.has_generic_args && segment.ident.name != kw::SelfUpper => {
17921811
(segment.ident.to_string(), segment.ident.span)
17931812
}
17941813
_ => return None,

src/test/ui/error-codes/E0411.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0411]: cannot find type `Self` in this scope
22
--> $DIR/E0411.rs:2:6
33
|
4+
LL | fn main() {
5+
| ---- `Self` not allowed in a function
46
LL | <Self>::foo;
57
| ^^^^ `Self` is only available in impls, traits, and type definitions
68

src/test/ui/lifetimes/issue-97194.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern "C" {
22
fn bget(&self, index: [usize; Self::DIM]) -> bool {
33
//~^ ERROR incorrect function inside `extern` block
44
//~| ERROR `self` parameter is only allowed in associated functions
5-
//~| ERROR use of undeclared type `Self`
5+
//~| ERROR failed to resolve: `Self`
66
type T<'a> = &'a str;
77
}
88
}

src/test/ui/lifetimes/issue-97194.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ LL | fn bget(&self, index: [usize; Self::DIM]) -> bool {
2525
|
2626
= note: associated functions are those in `impl` or `trait` definitions
2727

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

3434
error: aborting due to 3 previous errors
3535

src/test/ui/resolve/issue-24968.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1+
// Also includes more Self usages per #93796
2+
13
fn foo(_: Self) {
24
//~^ ERROR cannot find type `Self`
35
}
46

7+
fn foo2() {
8+
let x: Self;
9+
//~^ ERROR cannot find type `Self`
10+
}
11+
12+
type Foo<T>
13+
where
14+
Self: Clone,
15+
//~^ ERROR cannot find type `Self`
16+
= Vec<T>;
17+
18+
const FOO: Self = 0;
19+
//~^ ERROR cannot find type `Self`
20+
21+
const FOO2: u32 = Self::bar();
22+
//~^ ERROR failed to resolve: `Self`
23+
24+
static FOO_S: Self = 0;
25+
//~^ ERROR cannot find type `Self`
26+
27+
static FOO_S2: u32 = Self::bar();
28+
//~^ ERROR failed to resolve: `Self`
29+
530
fn main() {}
+52-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
1+
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
2+
--> $DIR/issue-24968.rs:21:19
3+
|
4+
LL | const FOO2: u32 = Self::bar();
5+
| ^^^^ `Self` is only available in impls, traits, and type definitions
6+
7+
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
8+
--> $DIR/issue-24968.rs:27:22
9+
|
10+
LL | static FOO_S2: u32 = Self::bar();
11+
| ^^^^ `Self` is only available in impls, traits, and type definitions
12+
113
error[E0411]: cannot find type `Self` in this scope
2-
--> $DIR/issue-24968.rs:1:11
14+
--> $DIR/issue-24968.rs:3:11
315
|
416
LL | fn foo(_: Self) {
5-
| ^^^^ `Self` is only available in impls, traits, and type definitions
17+
| --- ^^^^ `Self` is only available in impls, traits, and type definitions
18+
| |
19+
| `Self` not allowed in a function
20+
21+
error[E0411]: cannot find type `Self` in this scope
22+
--> $DIR/issue-24968.rs:8:12
23+
|
24+
LL | fn foo2() {
25+
| ---- `Self` not allowed in a function
26+
LL | let x: Self;
27+
| ^^^^ `Self` is only available in impls, traits, and type definitions
28+
29+
error[E0411]: cannot find type `Self` in this scope
30+
--> $DIR/issue-24968.rs:14:5
31+
|
32+
LL | type Foo<T>
33+
| --- `Self` not allowed in a type alias
34+
LL | where
35+
LL | Self: Clone,
36+
| ^^^^ `Self` is only available in impls, traits, and type definitions
37+
38+
error[E0411]: cannot find type `Self` in this scope
39+
--> $DIR/issue-24968.rs:18:12
40+
|
41+
LL | const FOO: Self = 0;
42+
| --- ^^^^ `Self` is only available in impls, traits, and type definitions
43+
| |
44+
| `Self` not allowed in a constant item
45+
46+
error[E0411]: cannot find type `Self` in this scope
47+
--> $DIR/issue-24968.rs:24:15
48+
|
49+
LL | static FOO_S: Self = 0;
50+
| ----- ^^^^ `Self` is only available in impls, traits, and type definitions
51+
| |
52+
| `Self` not allowed in a static item
653

7-
error: aborting due to previous error
54+
error: aborting due to 7 previous errors
855

9-
For more information about this error, try `rustc --explain E0411`.
56+
Some errors have detailed explanations: E0411, E0433.
57+
For more information about an error, try `rustc --explain E0411`.

src/test/ui/type-alias/issue-62263-self-in-atb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ pub trait Trait {
33
}
44

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

88
fn main() {}

src/test/ui/type-alias/issue-62263-self-in-atb.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type `Self`
1+
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
22
--> $DIR/issue-62263-self-in-atb.rs:5:32
33
|
44
LL | pub type Alias = dyn Trait<A = Self::A>;
5-
| ^^^^ use of undeclared type `Self`
5+
| ^^^^ `Self` is only available in impls, traits, and type definitions
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
type Alias = Self::Target;
2-
//~^ ERROR failed to resolve: use of undeclared type `Self` [E0433]
2+
//~^ ERROR failed to resolve: `Self`
33

44
fn main() {}

src/test/ui/type-alias/issue-62305-self-assoc-ty.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0433]: failed to resolve: use of undeclared type `Self`
1+
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
22
--> $DIR/issue-62305-self-assoc-ty.rs:1:14
33
|
44
LL | type Alias = Self::Target;
5-
| ^^^^ use of undeclared type `Self`
5+
| ^^^^ `Self` is only available in impls, traits, and type definitions
66

77
error: aborting due to previous error
88

src/test/ui/type-alias/issue-62364-self-ty-arg.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0411]: cannot find type `Self` in this scope
22
--> $DIR/issue-62364-self-ty-arg.rs:5:29
33
|
44
LL | type Alias<'a> = Struct<&'a Self>;
5-
| - ^^^^ `Self` is only available in impls, traits, and type definitions
6-
| |
7-
| help: you might be missing a type parameter: `, Self`
5+
| ----- ^^^^ `Self` is only available in impls, traits, and type definitions
6+
| |
7+
| `Self` not allowed in a type alias
88

99
error: aborting due to previous error
1010

src/test/ui/use/use-self-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ impl S {
44
fn f() {}
55
fn g() {
66
use Self::f; //~ ERROR unresolved import
7-
pub(in Self::f) struct Z; //~ ERROR use of undeclared type `Self`
7+
pub(in Self::f) struct Z; //~ ERROR failed to resolve: `Self`
88
}
99
}
1010

src/test/ui/use/use-self-type.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0433]: failed to resolve: use of undeclared type `Self`
1+
error[E0433]: failed to resolve: `Self` is only available in impls, traits, and type definitions
22
--> $DIR/use-self-type.rs:7:16
33
|
44
LL | pub(in Self::f) struct Z;
5-
| ^^^^ use of undeclared type `Self`
5+
| ^^^^ `Self` is only available in impls, traits, and type definitions
66

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

1313
error: aborting due to 2 previous errors
1414

0 commit comments

Comments
 (0)