From ea9ae30671f5bc42c4af392e5556dd16be297e82 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Sat, 24 Feb 2024 19:25:04 -0500 Subject: [PATCH] Convert `SpannedTypeVisitor` to use `VisitorResult` --- Cargo.lock | 1 + compiler/rustc_privacy/src/lib.rs | 4 +- compiler/rustc_ty_utils/Cargo.toml | 1 + compiler/rustc_ty_utils/src/opaque_types.rs | 4 +- compiler/rustc_ty_utils/src/sig_types.rs | 42 ++++++++++----------- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dfb0c066ee6..635146492b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4689,6 +4689,7 @@ name = "rustc_ty_utils" version = "0.0.0" dependencies = [ "itertools 0.11.0", + "rustc_ast_ir", "rustc_data_structures", "rustc_errors", "rustc_fluent_macro", diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index fc110afd8e4..fe359861681 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -1065,8 +1065,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>) -> ControlFlow<()> { + type Result = ControlFlow<()>; + fn visit(&mut self, span: Span, value: impl TypeVisitable>) -> Self::Result { self.span = span; value.visit_with(&mut self.skeleton()) } diff --git a/compiler/rustc_ty_utils/Cargo.toml b/compiler/rustc_ty_utils/Cargo.toml index f8eb82da8f6..2a30bd5d539 100644 --- a/compiler/rustc_ty_utils/Cargo.toml +++ b/compiler/rustc_ty_utils/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] # tidy-alphabetical-start itertools = "0.11" +rustc_ast_ir = { path = "../rustc_ast_ir" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_errors = { path = "../rustc_errors" } rustc_fluent_macro = { path = "../rustc_fluent_macro" } diff --git a/compiler/rustc_ty_utils/src/opaque_types.rs b/compiler/rustc_ty_utils/src/opaque_types.rs index c69aba1c352..aaf968cbb75 100644 --- a/compiler/rustc_ty_utils/src/opaque_types.rs +++ b/compiler/rustc_ty_utils/src/opaque_types.rs @@ -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}; @@ -194,9 +193,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>) -> ControlFlow { + fn visit(&mut self, span: Span, value: impl TypeVisitable>) { self.visit_spanned(span, value); - ControlFlow::Continue(()) } } diff --git a/compiler/rustc_ty_utils/src/sig_types.rs b/compiler/rustc_ty_utils/src/sig_types.rs index 38cc558380c..5527d853e30 100644 --- a/compiler/rustc_ty_utils/src/sig_types.rs +++ b/compiler/rustc_ty_utils/src/sig_types.rs @@ -1,27 +1,23 @@ //! This module contains helpers for walking all types of //! a signature, while preserving spans as much as possible -use std::ops::ControlFlow; - +use rustc_ast_ir::try_visit; +use rustc_ast_ir::visit::VisitorResult; use rustc_hir::{def::DefKind, def_id::LocalDefId}; use rustc_middle::ty::{self, TyCtxt}; use rustc_span::Span; use rustc_type_ir::visit::TypeVisitable; pub trait SpannedTypeVisitor<'tcx> { - type BreakTy = !; - fn visit( - &mut self, - span: Span, - value: impl TypeVisitable>, - ) -> ControlFlow; + type Result: VisitorResult = (); + fn visit(&mut self, span: Span, value: impl TypeVisitable>) -> Self::Result; } pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>( tcx: TyCtxt<'tcx>, item: LocalDefId, visitor: &mut V, -) -> ControlFlow { +) -> V::Result { let kind = tcx.def_kind(item); trace!(?kind); match kind { @@ -30,12 +26,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 @@ -44,32 +40,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 @@ -80,20 +76,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 @@ -116,5 +112,5 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>( | DefKind::Mod | DefKind::Use => {} } - ControlFlow::Continue(()) + V::Result::output() }