Skip to content

Commit c00ff9c

Browse files
committed
DoIt
1 parent 0da281b commit c00ff9c

File tree

7 files changed

+56
-83
lines changed

7 files changed

+56
-83
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,5 @@ 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 = `Self` is not valid at this location

compiler/rustc_hir_analysis/src/collect/type_of.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,37 @@ 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(
323+
hir::Impl { self_ty, .. }
324+
) => {
325+
struct MyVisitor(bool);
326+
impl<'v> hir::intravisit::Visitor<'v> for MyVisitor {
327+
fn visit_ty(&mut self, t: &'v Ty<'v>) {
328+
if matches!(
329+
&t.kind,
330+
TyKind::Path(hir::QPath::Resolved(
331+
_,
332+
Path {
333+
res: hir::def::Res::SelfTyAlias { .. },
334+
..
335+
},
336+
))
337+
) {
338+
self.0 = true;
339+
return;
340+
}
341+
hir::intravisit::walk_ty(self, t);
342+
}
343+
}
344+
345+
let mut my_visitor = MyVisitor(false);
346+
my_visitor.visit_ty(self_ty);
347+
348+
match my_visitor.0 {
349+
true => { tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: self_ty.span}); tcx.ty_error() },
350+
false => icx.to_ty(*self_ty),
351+
}
352+
},
323353
ItemKind::Fn(..) => {
324354
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
325355
tcx.mk_fn_def(def_id.to_def_id(), substs)

compiler/rustc_hir_analysis/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,10 @@ 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: Span,
279+
}

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 at this location
77

88
fn main() {}
+3-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
error[E0391]: cycle detected when computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:21>`
2-
--> $DIR/issue-23305.rs:5:16
1+
error: `Self` is not valid at this location
2+
--> $DIR/issue-23305.rs:5:6
33
|
44
LL | impl dyn ToNbt<Self> {}
5-
| ^^^^
6-
|
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-
| |____________^
5+
| ^^^^^^^^^^^^^^^
196

207
error: aborting due to previous error
218

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

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ 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 at this location
15+
impl Tr for S<Self> {} //~ ERROR `Self` is not valid at this location
16+
impl Self {} //~ ERROR `Self` is not valid at this location
17+
impl S<Self> {} //~ ERROR `Self` is not valid at this location
1818
impl Tr<Self::A> for S {} //~ ERROR cycle detected
1919

2020
fn main() {}

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

+8-60
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,26 @@
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 at this location
22
--> $DIR/resolve-self-in-impl.rs:14:13
33
|
44
LL | impl Tr for Self {}
55
| ^^^^
6-
|
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-
| |____________^
196

20-
error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:15:1: 15:20>`
21-
--> $DIR/resolve-self-in-impl.rs:15:15
7+
error: `Self` is not valid at this location
8+
--> $DIR/resolve-self-in-impl.rs:15:13
229
|
2310
LL | impl Tr for S<Self> {}
24-
| ^^^^
25-
|
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-
| |____________^
11+
| ^^^^^^^
3812

39-
error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:16:1: 16:10>`
13+
error: `Self` is not valid at this location
4014
--> $DIR/resolve-self-in-impl.rs:16:6
4115
|
4216
LL | impl Self {}
4317
| ^^^^
44-
|
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-
| |____________^
5718

58-
error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:17:1: 17:13>`
59-
--> $DIR/resolve-self-in-impl.rs:17:8
19+
error: `Self` is not valid at this location
20+
--> $DIR/resolve-self-in-impl.rs:17:6
6021
|
6122
LL | impl S<Self> {}
62-
| ^^^^
63-
|
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
67-
|
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-
| |____________^
23+
| ^^^^^^^
7624

7725
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:23>`
7826
--> $DIR/resolve-self-in-impl.rs:18:1

0 commit comments

Comments
 (0)