Skip to content

Commit

Permalink
Rollup merge of #103609 - BoxyUwU:fix_impl_self_cycle, r=compiler-errors
Browse files Browse the repository at this point in the history
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``
  • Loading branch information
matthiaskrgr authored Oct 28, 2022
2 parents 112fd02 + b342558 commit ab0d9df
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 76 deletions.
4 changes: 4 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,7 @@ 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 in the self type of an impl block
.note = replace `Self` with a different type
24 changes: 24 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2418,6 +2418,30 @@ impl<'hir> Ty<'hir> {
}
final_ty
}

pub fn find_self_aliases(&self) -> Vec<Span> {
use crate::intravisit::Visitor;
struct MyVisitor(Vec<Span>);
impl<'v> Visitor<'v> for MyVisitor {
fn visit_ty(&mut self, t: &'v Ty<'v>) {
if matches!(
&t.kind,
TyKind::Path(QPath::Resolved(
_,
Path { res: crate::def::Res::SelfTyAlias { .. }, .. },
))
) {
self.0.push(t.span);
return;
}
crate::intravisit::walk_ty(self, t);
}
}

let mut my_visitor = MyVisitor(vec![]);
my_visitor.visit_ty(self);
my_visitor.0
}
}

/// Not represented directly in the AST; referred to by name through a `ty_path`.
Expand Down
10 changes: 9 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,15 @@ 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, .. }) => {
match self_ty.find_self_aliases() {
spans if spans.len() > 0 => {
tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: (), });
tcx.ty_error()
},
_ => 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
11 changes: 10 additions & 1 deletion compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Errors emitted by `rustc_hir_analysis`.
use rustc_errors::IntoDiagnostic;
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
use rustc_errors::{IntoDiagnostic, MultiSpan};
use rustc_macros::{Diagnostic, LintDiagnostic};
use rustc_middle::ty::Ty;
use rustc_span::{symbol::Ident, Span, Symbol};
Expand Down Expand Up @@ -270,3 +270,12 @@ pub struct ConstBoundForNonConstTrait {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_self_in_impl_self)]
pub struct SelfInImplSelf {
#[primary_span]
pub span: MultiSpan,
#[note]
pub note: (),
}
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 in the self type of an impl block

fn main() {}
16 changes: 2 additions & 14 deletions src/test/ui/resolve/issue-23305.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
error[E0391]: cycle detected when computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:21>`
error: `Self` is not valid in the self type of an impl block
--> $DIR/issue-23305.rs:5:16
|
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() {}
| |____________^
= note: replace `Self` with a different type

error: aborting due to previous error

For more information about this error, try `rustc --explain E0391`.
9 changes: 5 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,11 @@ 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 in the self type of an impl block
impl Tr for S<Self> {} //~ ERROR `Self` is not valid in the self type of an impl block
impl Self {} //~ ERROR `Self` is not valid in the self type of an impl block
impl S<Self> {} //~ ERROR `Self` is not valid in the self type of an impl block
impl (Self, Self) {} //~ ERROR `Self` is not valid in the self type of an impl block
impl Tr<Self::A> for S {} //~ ERROR cycle detected

fn main() {}
74 changes: 19 additions & 55 deletions src/test/ui/resolve/resolve-self-in-impl.stderr
Original file line number Diff line number Diff line change
@@ -1,86 +1,50 @@
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 in the self type of an impl block
--> $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() {}
| |____________^
= note: replace `Self` with a different type

error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:15:1: 15:20>`
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:15:15
|
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() {}
| |____________^
= note: replace `Self` with a different type

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 in the self type of an impl block
--> $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() {}
| |____________^
= note: replace `Self` with a different type

error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-in-impl.rs:17:1: 17:13>`
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:17:8
|
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
= note: replace `Self` with a different type

error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:18:7
|
LL | / #![feature(associated_type_defaults)]
LL | |
LL | | struct S<T = u8>(T);
LL | | trait Tr<T = u8> {
... |
LL | |
LL | | fn main() {}
| |____________^
LL | impl (Self, Self) {}
| ^^^^ ^^^^
|
= note: replace `Self` with a different type

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
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>`
--> $DIR/resolve-self-in-impl.rs:19:1
|
LL | impl Tr<Self::A> for S {}
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:23>` again
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>` again
note: cycle used when collecting item types in top-level module
--> $DIR/resolve-self-in-impl.rs:1:1
|
Expand All @@ -93,6 +57,6 @@ LL | |
LL | | fn main() {}
| |____________^

error: aborting due to 5 previous errors
error: aborting due to 6 previous errors

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

0 comments on commit ab0d9df

Please sign in to comment.