Skip to content

Commit ab0d9df

Browse files
authored
Rollup merge of #103609 - BoxyUwU:fix_impl_self_cycle, r=compiler-errors
Emit a nicer error on `impl Self {` currently it emits a "cycle detected error" but this PR makes it emit a more user friendly error specifically saying that `Self` is disallowed in that position. this is a pretty hacky fix so i dont expect this to be merged (I basically only made this PR because i wanted to see if CI passes) r? ``@compiler-errors``
2 parents 112fd02 + b342558 commit ab0d9df

File tree

8 files changed

+74
-76
lines changed

8 files changed

+74
-76
lines changed

compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,7 @@ hir_analysis_const_impl_for_non_const_trait =
146146
147147
hir_analysis_const_bound_for_non_const_trait =
148148
~const can only be applied to `#[const_trait]` traits
149+
150+
hir_analysis_self_in_impl_self =
151+
`Self` is not valid in the self type of an impl block
152+
.note = replace `Self` with a different type

compiler/rustc_hir/src/hir.rs

+24
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,30 @@ impl<'hir> Ty<'hir> {
24182418
}
24192419
final_ty
24202420
}
2421+
2422+
pub fn find_self_aliases(&self) -> Vec<Span> {
2423+
use crate::intravisit::Visitor;
2424+
struct MyVisitor(Vec<Span>);
2425+
impl<'v> Visitor<'v> for MyVisitor {
2426+
fn visit_ty(&mut self, t: &'v Ty<'v>) {
2427+
if matches!(
2428+
&t.kind,
2429+
TyKind::Path(QPath::Resolved(
2430+
_,
2431+
Path { res: crate::def::Res::SelfTyAlias { .. }, .. },
2432+
))
2433+
) {
2434+
self.0.push(t.span);
2435+
return;
2436+
}
2437+
crate::intravisit::walk_ty(self, t);
2438+
}
2439+
}
2440+
2441+
let mut my_visitor = MyVisitor(vec![]);
2442+
my_visitor.visit_ty(self);
2443+
my_visitor.0
2444+
}
24212445
}
24222446

24232447
/// Not represented directly in the AST; referred to by name through a `ty_path`.

compiler/rustc_hir_analysis/src/collect/type_of.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,15 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
319319
}
320320
}
321321
ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty),
322-
ItemKind::Impl(hir::Impl { self_ty, .. }) => icx.to_ty(*self_ty),
322+
ItemKind::Impl(hir::Impl { self_ty, .. }) => {
323+
match self_ty.find_self_aliases() {
324+
spans if spans.len() > 0 => {
325+
tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: (), });
326+
tcx.ty_error()
327+
},
328+
_ => icx.to_ty(*self_ty),
329+
}
330+
},
323331
ItemKind::Fn(..) => {
324332
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
325333
tcx.mk_fn_def(def_id.to_def_id(), substs)

compiler/rustc_hir_analysis/src/errors.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Errors emitted by `rustc_hir_analysis`.
22
3-
use rustc_errors::IntoDiagnostic;
43
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
4+
use rustc_errors::{IntoDiagnostic, MultiSpan};
55
use rustc_macros::{Diagnostic, LintDiagnostic};
66
use rustc_middle::ty::Ty;
77
use rustc_span::{symbol::Ident, Span, Symbol};
@@ -270,3 +270,12 @@ pub struct ConstBoundForNonConstTrait {
270270
#[primary_span]
271271
pub span: Span,
272272
}
273+
274+
#[derive(Diagnostic)]
275+
#[diag(hir_analysis_self_in_impl_self)]
276+
pub struct SelfInImplSelf {
277+
#[primary_span]
278+
pub span: MultiSpan,
279+
#[note]
280+
pub note: (),
281+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ pub trait ToNbt<T> {
33
}
44

55
impl dyn ToNbt<Self> {}
6-
//~^ ERROR cycle detected
6+
//~^ ERROR `Self` is not valid in the self type of an impl block
77

88
fn main() {}
+2-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
error[E0391]: cycle detected when computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:21>`
1+
error: `Self` is not valid in the self type of an impl block
22
--> $DIR/issue-23305.rs:5:16
33
|
44
LL | impl dyn ToNbt<Self> {}
55
| ^^^^
66
|
7-
= note: ...which immediately requires computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:21>` again
8-
note: cycle used when collecting item types in top-level module
9-
--> $DIR/issue-23305.rs:1:1
10-
|
11-
LL | / pub trait ToNbt<T> {
12-
LL | | fn new(val: T) -> Self;
13-
LL | | }
14-
LL | |
15-
... |
16-
LL | |
17-
LL | | fn main() {}
18-
| |____________^
7+
= note: replace `Self` with a different type
198

209
error: aborting due to previous error
2110

22-
For more information about this error, try `rustc --explain E0391`.

src/test/ui/resolve/resolve-self-in-impl.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ impl Tr for S where Self: Copy {} // OK
1111
impl Tr for S where S<Self>: Copy {} // OK
1212
impl Tr for S where Self::A: Copy {} // OK
1313

14-
impl Tr for Self {} //~ ERROR cycle detected
15-
impl Tr for S<Self> {} //~ ERROR cycle detected
16-
impl Self {} //~ ERROR cycle detected
17-
impl S<Self> {} //~ ERROR cycle detected
14+
impl Tr for Self {} //~ ERROR `Self` is not valid in the self type of an impl block
15+
impl Tr for S<Self> {} //~ ERROR `Self` is not valid in the self type of an impl block
16+
impl Self {} //~ ERROR `Self` is not valid in the self type of an impl block
17+
impl S<Self> {} //~ ERROR `Self` is not valid in the self type of an impl block
18+
impl (Self, Self) {} //~ ERROR `Self` is not valid in the self type of an impl block
1819
impl Tr<Self::A> for S {} //~ ERROR cycle detected
1920

2021
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,50 @@
1-
error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:14:1: 14:17>`
1+
error: `Self` is not valid in the self type of an impl block
22
--> $DIR/resolve-self-in-impl.rs:14:13
33
|
44
LL | impl Tr for Self {}
55
| ^^^^
66
|
7-
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:14:1: 14:17>` again
8-
note: cycle used when collecting item types in top-level module
9-
--> $DIR/resolve-self-in-impl.rs:1:1
10-
|
11-
LL | / #![feature(associated_type_defaults)]
12-
LL | |
13-
LL | | struct S<T = u8>(T);
14-
LL | | trait Tr<T = u8> {
15-
... |
16-
LL | |
17-
LL | | fn main() {}
18-
| |____________^
7+
= note: replace `Self` with a different type
198

20-
error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:15:1: 15:20>`
9+
error: `Self` is not valid in the self type of an impl block
2110
--> $DIR/resolve-self-in-impl.rs:15:15
2211
|
2312
LL | impl Tr for S<Self> {}
2413
| ^^^^
2514
|
26-
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:15:1: 15:20>` again
27-
note: cycle used when collecting item types in top-level module
28-
--> $DIR/resolve-self-in-impl.rs:1:1
29-
|
30-
LL | / #![feature(associated_type_defaults)]
31-
LL | |
32-
LL | | struct S<T = u8>(T);
33-
LL | | trait Tr<T = u8> {
34-
... |
35-
LL | |
36-
LL | | fn main() {}
37-
| |____________^
15+
= note: replace `Self` with a different type
3816

39-
error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:16:1: 16:10>`
17+
error: `Self` is not valid in the self type of an impl block
4018
--> $DIR/resolve-self-in-impl.rs:16:6
4119
|
4220
LL | impl Self {}
4321
| ^^^^
4422
|
45-
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:16:1: 16:10>` again
46-
note: cycle used when collecting item types in top-level module
47-
--> $DIR/resolve-self-in-impl.rs:1:1
48-
|
49-
LL | / #![feature(associated_type_defaults)]
50-
LL | |
51-
LL | | struct S<T = u8>(T);
52-
LL | | trait Tr<T = u8> {
53-
... |
54-
LL | |
55-
LL | | fn main() {}
56-
| |____________^
23+
= note: replace `Self` with a different type
5724

58-
error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:17:1: 17:13>`
25+
error: `Self` is not valid in the self type of an impl block
5926
--> $DIR/resolve-self-in-impl.rs:17:8
6027
|
6128
LL | impl S<Self> {}
6229
| ^^^^
6330
|
64-
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:17:1: 17:13>` again
65-
note: cycle used when collecting item types in top-level module
66-
--> $DIR/resolve-self-in-impl.rs:1:1
31+
= note: replace `Self` with a different type
32+
33+
error: `Self` is not valid in the self type of an impl block
34+
--> $DIR/resolve-self-in-impl.rs:18:7
6735
|
68-
LL | / #![feature(associated_type_defaults)]
69-
LL | |
70-
LL | | struct S<T = u8>(T);
71-
LL | | trait Tr<T = u8> {
72-
... |
73-
LL | |
74-
LL | | fn main() {}
75-
| |____________^
36+
LL | impl (Self, Self) {}
37+
| ^^^^ ^^^^
38+
|
39+
= note: replace `Self` with a different type
7640

77-
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:23>`
78-
--> $DIR/resolve-self-in-impl.rs:18:1
41+
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>`
42+
--> $DIR/resolve-self-in-impl.rs:19:1
7943
|
8044
LL | impl Tr<Self::A> for S {}
8145
| ^^^^^^^^^^^^^^^^^^^^^^
8246
|
83-
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:23>` again
47+
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>` again
8448
note: cycle used when collecting item types in top-level module
8549
--> $DIR/resolve-self-in-impl.rs:1:1
8650
|
@@ -93,6 +57,6 @@ LL | |
9357
LL | | fn main() {}
9458
| |____________^
9559

96-
error: aborting due to 5 previous errors
60+
error: aborting due to 6 previous errors
9761

9862
For more information about this error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)