Skip to content

Commit b342558

Browse files
committed
tidy + move logic to fn
1 parent ca5a6e4 commit b342558

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,6 @@ hir_analysis_const_impl_for_non_const_trait =
147147
hir_analysis_const_bound_for_non_const_trait =
148148
~const can only be applied to `#[const_trait]` traits
149149
150-
hir_analysis_self_in_impl_self =
150+
hir_analysis_self_in_impl_self =
151151
`Self` is not valid in the self type of an impl block
152-
.note = replace `Self` with a different type
152+
.note = replace `Self` with a different type

compiler/rustc_hir/src/hir.rs

+24
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,30 @@ impl<'hir> Ty<'hir> {
24182418
}
24192419
final_ty
24202420
}
2421+
2422+
pub fn find_self_aliases(&self) -> Vec<Span> {
2423+
use crate::intravisit::Visitor;
2424+
struct MyVisitor(Vec<Span>);
2425+
impl<'v> Visitor<'v> for MyVisitor {
2426+
fn visit_ty(&mut self, t: &'v Ty<'v>) {
2427+
if matches!(
2428+
&t.kind,
2429+
TyKind::Path(QPath::Resolved(
2430+
_,
2431+
Path { res: crate::def::Res::SelfTyAlias { .. }, .. },
2432+
))
2433+
) {
2434+
self.0.push(t.span);
2435+
return;
2436+
}
2437+
crate::intravisit::walk_ty(self, t);
2438+
}
2439+
}
2440+
2441+
let mut my_visitor = MyVisitor(vec![]);
2442+
my_visitor.visit_ty(self);
2443+
my_visitor.0
2444+
}
24212445
}
24222446

24232447
/// Not represented directly in the AST; referred to by name through a `ty_path`.

compiler/rustc_hir_analysis/src/collect/type_of.rs

+4-29
Original file line numberDiff line numberDiff line change
@@ -319,36 +319,11 @@ 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(
323-
hir::Impl { self_ty, .. }
324-
) => {
325-
struct MyVisitor(Vec<Span>);
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.push(t.span);
339-
return;
340-
}
341-
hir::intravisit::walk_ty(self, t);
342-
}
343-
}
344-
345-
let mut my_visitor = MyVisitor(vec![]);
346-
my_visitor.visit_ty(self_ty);
347-
348-
match my_visitor.0 {
349-
spans if spans.len() > 0 => {
322+
ItemKind::Impl(hir::Impl { self_ty, .. }) => {
323+
match self_ty.find_self_aliases() {
324+
spans if spans.len() > 0 => {
350325
tcx.sess.emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: (), });
351-
tcx.ty_error()
326+
tcx.ty_error()
352327
},
353328
_ => icx.to_ty(*self_ty),
354329
}

0 commit comments

Comments
 (0)