This commit is contained in:
Boxy 2022-10-27 21:48:41 +01:00
parent 0da281b606
commit c00ff9c4d0
7 changed files with 56 additions and 83 deletions

View File

@ -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

View File

@ -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)

View File

@ -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,
}

View File

@ -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() {}

View File

@ -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`.

View File

@ -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() {}

View File

@ -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