Skip to content

Commit

Permalink
DoIt
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Oct 27, 2022
1 parent 0da281b commit c00ff9c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 83 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,5 @@ hir_analysis_const_impl_for_non_const_trait =
hir_analysis_const_bound_for_non_const_trait =
~const can only be applied to `#[const_trait]` traits
hir_analysis_self_in_impl_self = `Self` is not valid at this location
32 changes: 31 additions & 1 deletion compiler/rustc_hir_analysis/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,37 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
}
}
ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty),
ItemKind::Impl(hir::Impl { self_ty, .. }) => icx.to_ty(*self_ty),
ItemKind::Impl(
hir::Impl { self_ty, .. }
) => {
struct MyVisitor(bool);
impl<'v> hir::intravisit::Visitor<'v> for MyVisitor {
fn visit_ty(&mut self, t: &'v Ty<'v>) {
if matches!(
&t.kind,
TyKind::Path(hir::QPath::Resolved(
_,
Path {
res: hir::def::Res::SelfTyAlias { .. },
..
},
))
) {
self.0 = true;
return;
}
hir::intravisit::walk_ty(self, t);
}
}

let mut my_visitor = MyVisitor(false);
my_visitor.visit_ty(self_ty);

match my_visitor.0 {
true => { tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: self_ty.span}); tcx.ty_error() },
false => icx.to_ty(*self_ty),
}
},
ItemKind::Fn(..) => {
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
tcx.mk_fn_def(def_id.to_def_id(), substs)
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,10 @@ pub struct ConstBoundForNonConstTrait {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_self_in_impl_self)]
pub struct SelfInImplSelf {
#[primary_span]
pub span: Span,
}
2 changes: 1 addition & 1 deletion src/test/ui/resolve/issue-23305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pub trait ToNbt<T> {
}

impl dyn ToNbt<Self> {}
//~^ ERROR cycle detected
//~^ ERROR `Self` is not valid at this location

fn main() {}
20 changes: 3 additions & 17 deletions src/test/ui/resolve/issue-23305.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
error[E0391]: cycle detected when computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:21>`
--> $DIR/issue-23305.rs:5:16
error: `Self` is not valid at this location
--> $DIR/issue-23305.rs:5:6
|
LL | impl dyn ToNbt<Self> {}
| ^^^^
|
= note: ...which immediately requires computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:21>` again
note: cycle used when collecting item types in top-level module
--> $DIR/issue-23305.rs:1:1
|
LL | / pub trait ToNbt<T> {
LL | | fn new(val: T) -> Self;
LL | | }
LL | |
... |
LL | |
LL | | fn main() {}
| |____________^
| ^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0391`.
8 changes: 4 additions & 4 deletions src/test/ui/resolve/resolve-self-in-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ impl Tr for S where Self: Copy {} // OK
impl Tr for S where S<Self>: Copy {} // OK
impl Tr for S where Self::A: Copy {} // OK

impl Tr for Self {} //~ ERROR cycle detected
impl Tr for S<Self> {} //~ ERROR cycle detected
impl Self {} //~ ERROR cycle detected
impl S<Self> {} //~ ERROR cycle detected
impl Tr for Self {} //~ ERROR `Self` is not valid at this location
impl Tr for S<Self> {} //~ ERROR `Self` is not valid at this location
impl Self {} //~ ERROR `Self` is not valid at this location
impl S<Self> {} //~ ERROR `Self` is not valid at this location
impl Tr<Self::A> for S {} //~ ERROR cycle detected

fn main() {}
68 changes: 8 additions & 60 deletions src/test/ui/resolve/resolve-self-in-impl.stderr
Original file line number Diff line number Diff line change
@@ -1,78 +1,26 @@
error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:14:1: 14:17>`
error: `Self` is not valid at this location
--> $DIR/resolve-self-in-impl.rs:14:13
|
LL | impl Tr for Self {}
| ^^^^
|
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:14:1: 14:17>` again
note: cycle used when collecting item types in top-level module
--> $DIR/resolve-self-in-impl.rs:1:1
|
LL | / #![feature(associated_type_defaults)]
LL | |
LL | | struct S<T = u8>(T);
LL | | trait Tr<T = u8> {
... |
LL | |
LL | | fn main() {}
| |____________^

error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:15:1: 15:20>`
--> $DIR/resolve-self-in-impl.rs:15:15
error: `Self` is not valid at this location
--> $DIR/resolve-self-in-impl.rs:15:13
|
LL | impl Tr for S<Self> {}
| ^^^^
|
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:15:1: 15:20>` again
note: cycle used when collecting item types in top-level module
--> $DIR/resolve-self-in-impl.rs:1:1
|
LL | / #![feature(associated_type_defaults)]
LL | |
LL | | struct S<T = u8>(T);
LL | | trait Tr<T = u8> {
... |
LL | |
LL | | fn main() {}
| |____________^
| ^^^^^^^

error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:16:1: 16:10>`
error: `Self` is not valid at this location
--> $DIR/resolve-self-in-impl.rs:16:6
|
LL | impl Self {}
| ^^^^
|
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:16:1: 16:10>` again
note: cycle used when collecting item types in top-level module
--> $DIR/resolve-self-in-impl.rs:1:1
|
LL | / #![feature(associated_type_defaults)]
LL | |
LL | | struct S<T = u8>(T);
LL | | trait Tr<T = u8> {
... |
LL | |
LL | | fn main() {}
| |____________^

error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:17:1: 17:13>`
--> $DIR/resolve-self-in-impl.rs:17:8
error: `Self` is not valid at this location
--> $DIR/resolve-self-in-impl.rs:17:6
|
LL | impl S<Self> {}
| ^^^^
|
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:17:1: 17:13>` again
note: cycle used when collecting item types in top-level module
--> $DIR/resolve-self-in-impl.rs:1:1
|
LL | / #![feature(associated_type_defaults)]
LL | |
LL | | struct S<T = u8>(T);
LL | | trait Tr<T = u8> {
... |
LL | |
LL | | fn main() {}
| |____________^
| ^^^^^^^

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

0 comments on commit c00ff9c

Please sign in to comment.