Skip to content

Commit

Permalink
Convert SpannedTypeVisitor to use VisitorResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Feb 25, 2024
1 parent e9b4f60 commit 496f14b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,8 +1069,8 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
}

impl<'tcx> rustc_ty_utils::sig_types::SpannedTypeVisitor<'tcx> for TypePrivacyVisitor<'tcx> {
type BreakTy = ();
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> ControlFlow<()> {
type Result = ControlFlow<()>;
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> Self::Result {
self.span = span;
value.visit_with(&mut self.skeleton())
}
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_ty_utils/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
use rustc_span::Span;
use rustc_trait_selection::traits::check_args_compatible;
use std::ops::ControlFlow;

use crate::errors::{DuplicateArg, NotParam};

Expand Down Expand Up @@ -185,9 +184,8 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {

impl<'tcx> super::sig_types::SpannedTypeVisitor<'tcx> for OpaqueTypeCollector<'tcx> {
#[instrument(skip(self), ret, level = "trace")]
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> ControlFlow<!> {
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) {
self.visit_spanned(span, value);
ControlFlow::Continue(())
}
}

Expand Down Expand Up @@ -278,13 +276,11 @@ struct ImplTraitInAssocTypeCollector<'tcx>(OpaqueTypeCollector<'tcx>);

impl<'tcx> super::sig_types::SpannedTypeVisitor<'tcx> for ImplTraitInAssocTypeCollector<'tcx> {
#[instrument(skip(self), ret, level = "trace")]
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> ControlFlow<!> {
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) {
let old = self.0.span;
self.0.span = Some(span);
value.visit_with(self);
self.0.span = old;

ControlFlow::Continue(())
}
}

Expand Down
43 changes: 19 additions & 24 deletions compiler/rustc_ty_utils/src/sig_types.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
//! This module contains helpers for walking all types of
//! a signature, while preserving spans as much as possible

use std::ops::ControlFlow;

use rustc_hir::{def::DefKind, def_id::LocalDefId};
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::Span;
use rustc_type_ir::visit::TypeVisitable;
use rustc_type_ir::try_visit;
use rustc_type_ir::visit::{TypeVisitable, VisitorResult};

pub trait SpannedTypeVisitor<'tcx> {
type BreakTy = !;
fn visit(
&mut self,
span: Span,
value: impl TypeVisitable<TyCtxt<'tcx>>,
) -> ControlFlow<Self::BreakTy>;
type Result: VisitorResult = ();
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> Self::Result;
}

pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
tcx: TyCtxt<'tcx>,
item: LocalDefId,
visitor: &mut V,
) -> ControlFlow<V::BreakTy> {
) -> V::Result {
let kind = tcx.def_kind(item);
trace!(?kind);
match kind {
Expand All @@ -30,12 +25,12 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
let ty_sig = tcx.fn_sig(item).instantiate_identity();
let hir_sig = tcx.hir_node_by_def_id(item).fn_decl().unwrap();
// Walk over the inputs and outputs manually in order to get good spans for them.
visitor.visit(hir_sig.output.span(), ty_sig.output());
try_visit!(visitor.visit(hir_sig.output.span(), ty_sig.output()));
for (hir, ty) in hir_sig.inputs.iter().zip(ty_sig.inputs().iter()) {
visitor.visit(hir.span, ty.map_bound(|x| *x))?;
try_visit!(visitor.visit(hir.span, ty.map_bound(|x| *x)));
}
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
visitor.visit(span, pred)?;
try_visit!(visitor.visit(span, pred));
}
}
// Walk over the type behind the alias
Expand All @@ -44,32 +39,32 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
DefKind::Static(_) | DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => {
if let Some(ty) = tcx.hir_node_by_def_id(item).ty() {
// Associated types in traits don't necessarily have a type that we can visit
visitor.visit(ty.span, tcx.type_of(item).instantiate_identity())?;
try_visit!(visitor.visit(ty.span, tcx.type_of(item).instantiate_identity()));
}
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
visitor.visit(span, pred)?;
try_visit!(visitor.visit(span, pred));
}
}
DefKind::OpaqueTy => {
for (pred, span) in tcx.explicit_item_bounds(item).instantiate_identity_iter_copied() {
visitor.visit(span, pred)?;
try_visit!(visitor.visit(span, pred));
}
}
// Look at field types
DefKind::Struct | DefKind::Union | DefKind::Enum => {
let span = tcx.def_ident_span(item).unwrap();
let ty = tcx.type_of(item).instantiate_identity();
visitor.visit(span, ty);
try_visit!(visitor.visit(span, ty));
let ty::Adt(def, args) = ty.kind() else {
span_bug!(span, "invalid type for {kind:?}: {:#?}", ty.kind())
};
for field in def.all_fields() {
let span = tcx.def_ident_span(field.did).unwrap();
let ty = field.ty(tcx, args);
visitor.visit(span, ty);
try_visit!(visitor.visit(span, ty));
}
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
visitor.visit(span, pred)?;
try_visit!(visitor.visit(span, pred));
}
}
// These are not part of a public API, they can only appear as hidden types, and there
Expand All @@ -80,20 +75,20 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
if of_trait {
let span = tcx.hir_node_by_def_id(item).expect_item().expect_impl().of_trait.unwrap().path.span;
let args = &tcx.impl_trait_ref(item).unwrap().instantiate_identity().args[1..];
visitor.visit(span, args)?;
try_visit!(visitor.visit(span, args));
}
let span = match tcx.hir_node_by_def_id(item).ty() {
Some(ty) => ty.span,
_ => tcx.def_span(item),
};
visitor.visit(span, tcx.type_of(item).instantiate_identity());
try_visit!(visitor.visit(span, tcx.type_of(item).instantiate_identity()));
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
visitor.visit(span, pred)?;
try_visit!(visitor.visit(span, pred));
}
}
DefKind::TraitAlias | DefKind::Trait => {
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
visitor.visit(span, pred)?;
try_visit!(visitor.visit(span, pred));
}
}
| DefKind::Variant
Expand All @@ -116,5 +111,5 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
| DefKind::Mod
| DefKind::Use => {}
}
ControlFlow::Continue(())
V::Result::output()
}

0 comments on commit 496f14b

Please sign in to comment.