Skip to content

Commit

Permalink
tidy + move logic to fn
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Oct 27, 2022
1 parent ca5a6e4 commit b342558
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,6 @@ 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 =
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
.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
33 changes: 4 additions & 29 deletions compiler/rustc_hir_analysis/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,36 +319,11 @@ 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, .. }
) => {
struct MyVisitor(Vec<Span>);
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.push(t.span);
return;
}
hir::intravisit::walk_ty(self, t);
}
}

let mut my_visitor = MyVisitor(vec![]);
my_visitor.visit_ty(self_ty);

match my_visitor.0 {
spans if spans.len() > 0 => {
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()
tcx.ty_error()
},
_ => icx.to_ty(*self_ty),
}
Expand Down

0 comments on commit b342558

Please sign in to comment.