Move folding & visiting traits to ir submodules
This commit is contained in:
parent
20081880ad
commit
62846d7c99
3 changed files with 331 additions and 301 deletions
|
@ -48,6 +48,11 @@ use rustc_hir::def_id::DefId;
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
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,
|
/// This trait is implemented for every type that can be folded,
|
||||||
/// providing the skeleton of the traversal.
|
/// 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
|
/// calls a folder method specifically for that type (such as
|
||||||
/// `F::try_fold_ty`). This is where control transfers from `TypeFoldable`
|
/// `F::try_fold_ty`). This is where control transfers from `TypeFoldable`
|
||||||
/// to `TypeFolder`.
|
/// 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
|
/// A convenient alternative to `try_fold_with` for use with infallible
|
||||||
/// folders. Do not override this method, to ensure coherence with
|
/// 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)
|
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)
|
r.try_super_fold_with(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,6 +216,7 @@ where
|
||||||
Ok(self.fold_predicate(p))
|
Ok(self.fold_predicate(p))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
// Some sample folders
|
// Some sample folders
|
||||||
|
|
|
@ -146,6 +146,10 @@ mod structural_impls;
|
||||||
mod sty;
|
mod sty;
|
||||||
mod typeck_results;
|
mod typeck_results;
|
||||||
|
|
||||||
|
pub mod ir {
|
||||||
|
pub use super::{fold::ir::*, visit::ir::*};
|
||||||
|
}
|
||||||
|
|
||||||
// Data types
|
// Data types
|
||||||
|
|
||||||
pub type RegisteredTools = FxHashSet<Ident>;
|
pub type RegisteredTools = FxHashSet<Ident>;
|
||||||
|
|
|
@ -39,13 +39,22 @@
|
||||||
//! - u.visit_with(visitor)
|
//! - u.visit_with(visitor)
|
||||||
//! ```
|
//! ```
|
||||||
use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
|
use crate::ty::{self, flags::FlagComputation, Binder, Ty, TyCtxt, TypeFlags};
|
||||||
use rustc_errors::ErrorGuaranteed;
|
|
||||||
|
|
||||||
use rustc_data_structures::fx::FxHashSet;
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_data_structures::sso::SsoHashSet;
|
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::fmt;
|
||||||
use std::ops::ControlFlow;
|
use std::ops::ControlFlow;
|
||||||
|
|
||||||
|
use super::{FoundFlags, HasEscapingVarsVisitor, HasTypeFlagsVisitor};
|
||||||
|
|
||||||
/// This trait is implemented for every type that can be visited,
|
/// This trait is implemented for every type that can be visited,
|
||||||
/// providing the skeleton of the traversal.
|
/// 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 {
|
fn has_type_flags(&self, flags: TypeFlags) -> bool {
|
||||||
let res =
|
let res = self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value()
|
||||||
self.visit_with(&mut HasTypeFlagsVisitor { flags }).break_value() == Some(FoundFlags);
|
== Some(FoundFlags);
|
||||||
trace!(?self, ?flags, ?res, "has_type_flags");
|
trace!(?self, ?flags, ?res, "has_type_flags");
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
@ -108,7 +117,8 @@ pub trait TypeVisitable<'tcx>: fmt::Debug + Clone {
|
||||||
}
|
}
|
||||||
fn error_reported(&self) -> Result<(), ErrorGuaranteed> {
|
fn error_reported(&self) -> Result<(), ErrorGuaranteed> {
|
||||||
if self.references_error() {
|
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)
|
Err(reported)
|
||||||
} else {
|
} else {
|
||||||
bug!("expect tcx.sess.is_compilation_going_to_fail return `Some`");
|
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
|
/// For example, in `MyVisitor::visit_ty(ty)`, it is valid to call
|
||||||
/// `ty.super_visit_with(self)`, but any other visiting should be done
|
/// `ty.super_visit_with(self)`, but any other visiting should be done
|
||||||
/// with `xyz.visit_with(self)`.
|
/// 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
|
/// 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)
|
p.super_visit_with(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
// Region folder
|
// Region folder
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue