1
Fork 0

Move folding & visiting traits to ir submodules

This commit is contained in:
Alan Egerton 2023-02-09 12:56:33 +00:00
parent 20081880ad
commit 62846d7c99
No known key found for this signature in database
GPG key ID: 7D4C2F6C22122532
3 changed files with 331 additions and 301 deletions

View file

@ -48,6 +48,11 @@ use rustc_hir::def_id::DefId;
use std::collections::BTreeMap;
pub use ir::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable};
pub mod ir {
use crate::ty::{self, ir::TypeVisitable, Binder, Ty, TyCtxt};
/// This trait is implemented for every type that can be folded,
/// providing the skeleton of the traversal.
///
@ -64,7 +69,10 @@ pub trait TypeFoldable<'tcx>: TypeVisitable<'tcx> {
/// calls a folder method specifically for that type (such as
/// `F::try_fold_ty`). This is where control transfers from `TypeFoldable`
/// to `TypeFolder`.
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error>;
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(
self,
folder: &mut F,
) -> Result<Self, F::Error>;
/// A convenient alternative to `try_fold_with` for use with infallible
/// folders. Do not override this method, to ensure coherence with
@ -154,7 +162,10 @@ pub trait FallibleTypeFolder<'tcx>: Sized {
t.try_super_fold_with(self)
}
fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
fn try_fold_region(
&mut self,
r: ty::Region<'tcx>,
) -> Result<ty::Region<'tcx>, Self::Error> {
r.try_super_fold_with(self)
}
@ -205,6 +216,7 @@ where
Ok(self.fold_predicate(p))
}
}
}
///////////////////////////////////////////////////////////////////////////
// Some sample folders

View file

@ -146,6 +146,10 @@ mod structural_impls;
mod sty;
mod typeck_results;
pub mod ir {
pub use super::{fold::ir::*, visit::ir::*};
}
// Data types
pub type RegisteredTools = FxHashSet<Ident>;

View file

@ -39,13 +39,22 @@
//! - u.visit_with(visitor)
//! ```
use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
use rustc_errors::ErrorGuaranteed;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sso::SsoHashSet;
use std::ops::ControlFlow;
pub use ir::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
pub mod ir {
use crate::ty::{self, Binder, Ty, TypeFlags};
use rustc_errors::ErrorGuaranteed;
use std::fmt;
use std::ops::ControlFlow;
use super::{FoundFlags, HasEscapingVarsVisitor, HasTypeFlagsVisitor};
/// This trait is implemented for every type that can be visited,
/// providing the skeleton of the traversal.
///
@ -89,8 +98,8 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
}
fn has_type_flags(&self, flags: TypeFlags) -> bool {
let res =
self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value() == Some(FoundFlags);
let res = self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value()
== Some(FoundFlags);
trace!(?self, ?flags, ?res, "has_type_flags");
res
}
@ -108,7 +117,8 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
}
fn error_reported(&self) -> Result<(), ErrorGuaranteed> {
if self.references_error() {
if let Some(reported) = ty::tls::with(|tcx| tcx.sess.is_compilation_going_to_fail()) {
if let Some(reported) = ty::tls::with(|tcx| tcx.sess.is_compilation_going_to_fail())
{
Err(reported)
} else {
bug!("expect tcx.sess.is_compilation_going_to_fail return `Some`");
@ -192,7 +202,10 @@ pub trait TypeSuperVisitable<'tcx>: TypeVisitable<'tcx> {
/// For example, in `MyVisitor::visit_ty(ty)`, it is valid to call
/// `ty.super_visit_with(self)`, but any other visiting should be done
/// with `xyz.visit_with(self)`.
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy>;
fn super_visit_with<V: TypeVisitor<'tcx>>(
&self,
visitor: &mut V,
) -> ControlFlow<V::BreakTy>;
}
/// This trait is implemented for every visiting traversal. There is a visit
@ -224,6 +237,7 @@ pub trait TypeVisitor<'tcx>: Sized {
p.super_visit_with(self)
}
}
}
///////////////////////////////////////////////////////////////////////////
// Region folder