Trait chalk_ir::fold::FallibleTypeFolder
source · pub trait FallibleTypeFolder<I: Interner> {
type Error;
Show 19 methods
// Required methods
fn as_dyn(&mut self) -> &mut dyn FallibleTypeFolder<I, Error = Self::Error>;
fn interner(&self) -> I;
// Provided methods
fn try_fold_ty(
&mut self,
ty: Ty<I>,
outer_binder: DebruijnIndex,
) -> Result<Ty<I>, Self::Error> { ... }
fn try_fold_lifetime(
&mut self,
lifetime: Lifetime<I>,
outer_binder: DebruijnIndex,
) -> Result<Lifetime<I>, Self::Error> { ... }
fn try_fold_const(
&mut self,
constant: Const<I>,
outer_binder: DebruijnIndex,
) -> Result<Const<I>, Self::Error> { ... }
fn try_fold_program_clause(
&mut self,
clause: ProgramClause<I>,
outer_binder: DebruijnIndex,
) -> Result<ProgramClause<I>, Self::Error> { ... }
fn try_fold_goal(
&mut self,
goal: Goal<I>,
outer_binder: DebruijnIndex,
) -> Result<Goal<I>, Self::Error> { ... }
fn forbid_free_vars(&self) -> bool { ... }
fn try_fold_free_var_ty(
&mut self,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Result<Ty<I>, Self::Error> { ... }
fn try_fold_free_var_lifetime(
&mut self,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Result<Lifetime<I>, Self::Error> { ... }
fn try_fold_free_var_const(
&mut self,
ty: Ty<I>,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Result<Const<I>, Self::Error> { ... }
fn forbid_free_placeholders(&self) -> bool { ... }
fn try_fold_free_placeholder_ty(
&mut self,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Result<Ty<I>, Self::Error> { ... }
fn try_fold_free_placeholder_lifetime(
&mut self,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Result<Lifetime<I>, Self::Error> { ... }
fn try_fold_free_placeholder_const(
&mut self,
ty: Ty<I>,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Result<Const<I>, Self::Error> { ... }
fn forbid_inference_vars(&self) -> bool { ... }
fn try_fold_inference_ty(
&mut self,
var: InferenceVar,
kind: TyVariableKind,
outer_binder: DebruijnIndex,
) -> Result<Ty<I>, Self::Error> { ... }
fn try_fold_inference_lifetime(
&mut self,
var: InferenceVar,
outer_binder: DebruijnIndex,
) -> Result<Lifetime<I>, Self::Error> { ... }
fn try_fold_inference_const(
&mut self,
ty: Ty<I>,
var: InferenceVar,
outer_binder: DebruijnIndex,
) -> Result<Const<I>, Self::Error> { ... }
}
Expand description
A “folder” is a transformer that can be used to make a copy of
some term – that is, some bit of IR, such as a Goal
– with
certain changes applied. The idea is that it contains methods that
let you swap types/lifetimes for new types/lifetimes; meanwhile,
each bit of IR implements the TypeFoldable
trait which, given a
FallibleTypeFolder
, will reconstruct itself, invoking the folder’s
methods to transform each of the types/lifetimes embedded within.
As the name suggests, folds performed by FallibleTypeFolder
can
fail (with type Error
); if the folder cannot fail, consider
implementing TypeFolder
instead (which is an infallible, but
otherwise equivalent, trait).
§Usage patterns
§Substituting for free variables
Most of the time, though, we are not interested in adjust arbitrary types/lifetimes, but rather just free variables (even more often, just free existential variables) that appear within the term.
For this reason, the FallibleTypeFolder
trait extends two other
traits that contain methods that are invoked when just those particular
In particular, folders can intercept references to free variables (either existentially or universally quantified) and replace them with other types/lifetimes as appropriate.
To create a folder F
, one never implements FallibleTypeFolder
directly, but instead implements one of each of these three sub-traits:
FreeVarFolder
– foldsBoundVar
instances that appear free in the term being folded (useDefaultFreeVarFolder
to ignore/forbid these altogether)InferenceFolder
– folds existentialInferenceVar
instances that appear in the term being folded (useDefaultInferenceFolder
to ignore/forbid these altogether)PlaceholderFolder
– folds universalPlaceholder
instances that appear in the term being folded (useDefaultPlaceholderFolder
to ignore/forbid these altogether)
To apply a folder, use the TypeFoldable::try_fold_with
method,
like so
let x = x.try_fold_with(&mut folder, 0);
Required Associated Types§
sourcetype Error
type Error
The type this folder returns when folding fails. This is
commonly NoSolution
.
Required Methods§
sourcefn as_dyn(&mut self) -> &mut dyn FallibleTypeFolder<I, Error = Self::Error>
fn as_dyn(&mut self) -> &mut dyn FallibleTypeFolder<I, Error = Self::Error>
Creates a dyn
value from this folder. Unfortunately, this
must be added manually to each impl of FallibleTypeFolder; it
permits the default implements below to create a
&mut dyn FallibleTypeFolder
from Self
without knowing what
Self
is (by invoking this method). Effectively, this limits
impls of FallibleTypeFolder
to types for which we are able to
create a dyn value (i.e., not [T]
types).
Provided Methods§
sourcefn try_fold_ty(
&mut self,
ty: Ty<I>,
outer_binder: DebruijnIndex,
) -> Result<Ty<I>, Self::Error>
fn try_fold_ty( &mut self, ty: Ty<I>, outer_binder: DebruijnIndex, ) -> Result<Ty<I>, Self::Error>
Top-level callback: invoked for each Ty<I>
that is
encountered when folding. By default, invokes
try_super_fold_with
, which will in turn invoke the more
specialized folding methods below, like try_fold_free_var_ty
.
sourcefn try_fold_lifetime(
&mut self,
lifetime: Lifetime<I>,
outer_binder: DebruijnIndex,
) -> Result<Lifetime<I>, Self::Error>
fn try_fold_lifetime( &mut self, lifetime: Lifetime<I>, outer_binder: DebruijnIndex, ) -> Result<Lifetime<I>, Self::Error>
Top-level callback: invoked for each Lifetime<I>
that is
encountered when folding. By default, invokes
try_super_fold_with
, which will in turn invoke the more
specialized folding methods below, like try_fold_free_var_lifetime
.
sourcefn try_fold_const(
&mut self,
constant: Const<I>,
outer_binder: DebruijnIndex,
) -> Result<Const<I>, Self::Error>
fn try_fold_const( &mut self, constant: Const<I>, outer_binder: DebruijnIndex, ) -> Result<Const<I>, Self::Error>
Top-level callback: invoked for each Const<I>
that is
encountered when folding. By default, invokes
try_super_fold_with
, which will in turn invoke the more
specialized folding methods below, like try_fold_free_var_const
.
sourcefn try_fold_program_clause(
&mut self,
clause: ProgramClause<I>,
outer_binder: DebruijnIndex,
) -> Result<ProgramClause<I>, Self::Error>
fn try_fold_program_clause( &mut self, clause: ProgramClause<I>, outer_binder: DebruijnIndex, ) -> Result<ProgramClause<I>, Self::Error>
Invoked for every program clause. By default, recursively folds the goals contents.
sourcefn try_fold_goal(
&mut self,
goal: Goal<I>,
outer_binder: DebruijnIndex,
) -> Result<Goal<I>, Self::Error>
fn try_fold_goal( &mut self, goal: Goal<I>, outer_binder: DebruijnIndex, ) -> Result<Goal<I>, Self::Error>
Invoked for every goal. By default, recursively folds the goals contents.
sourcefn forbid_free_vars(&self) -> bool
fn forbid_free_vars(&self) -> bool
If overridden to return true, then folding will panic if a free variable is encountered. This should be done if free type/lifetime variables are not expected.
sourcefn try_fold_free_var_ty(
&mut self,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Result<Ty<I>, Self::Error>
fn try_fold_free_var_ty( &mut self, bound_var: BoundVar, outer_binder: DebruijnIndex, ) -> Result<Ty<I>, Self::Error>
Invoked for TyKind::BoundVar
instances that are not bound
within the type being folded over:
depth
is the depth of theTyKind::BoundVar
; this has been adjusted to account for binders in scope.binders
is the number of binders in scope.
This should return a type suitable for a context with
binders
in scope.
sourcefn try_fold_free_var_lifetime(
&mut self,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Result<Lifetime<I>, Self::Error>
fn try_fold_free_var_lifetime( &mut self, bound_var: BoundVar, outer_binder: DebruijnIndex, ) -> Result<Lifetime<I>, Self::Error>
As try_fold_free_var_ty
, but for lifetimes.
sourcefn try_fold_free_var_const(
&mut self,
ty: Ty<I>,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Result<Const<I>, Self::Error>
fn try_fold_free_var_const( &mut self, ty: Ty<I>, bound_var: BoundVar, outer_binder: DebruijnIndex, ) -> Result<Const<I>, Self::Error>
As try_fold_free_var_ty
, but for constants.
sourcefn forbid_free_placeholders(&self) -> bool
fn forbid_free_placeholders(&self) -> bool
If overridden to return true, we will panic when a free placeholder type/lifetime/const is encountered.
sourcefn try_fold_free_placeholder_ty(
&mut self,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Result<Ty<I>, Self::Error>
fn try_fold_free_placeholder_ty( &mut self, universe: PlaceholderIndex, outer_binder: DebruijnIndex, ) -> Result<Ty<I>, Self::Error>
Invoked for each occurrence of a placeholder type; these are
used when we instantiate binders universally. Returns a type
to use instead, which should be suitably shifted to account
for binders
.
universe
is the universe of theTypeName::ForAll
that was foundbinders
is the number of binders in scope
sourcefn try_fold_free_placeholder_lifetime(
&mut self,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Result<Lifetime<I>, Self::Error>
fn try_fold_free_placeholder_lifetime( &mut self, universe: PlaceholderIndex, outer_binder: DebruijnIndex, ) -> Result<Lifetime<I>, Self::Error>
As with try_fold_free_placeholder_ty
, but for lifetimes.
sourcefn try_fold_free_placeholder_const(
&mut self,
ty: Ty<I>,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Result<Const<I>, Self::Error>
fn try_fold_free_placeholder_const( &mut self, ty: Ty<I>, universe: PlaceholderIndex, outer_binder: DebruijnIndex, ) -> Result<Const<I>, Self::Error>
As with try_fold_free_placeholder_ty
, but for constants.
sourcefn forbid_inference_vars(&self) -> bool
fn forbid_inference_vars(&self) -> bool
If overridden to return true, inference variables will trigger panics when folded. Used when inference variables are unexpected.
sourcefn try_fold_inference_ty(
&mut self,
var: InferenceVar,
kind: TyVariableKind,
outer_binder: DebruijnIndex,
) -> Result<Ty<I>, Self::Error>
fn try_fold_inference_ty( &mut self, var: InferenceVar, kind: TyVariableKind, outer_binder: DebruijnIndex, ) -> Result<Ty<I>, Self::Error>
Invoked for each occurrence of a inference type; these are
used when we instantiate binders universally. Returns a type
to use instead, which should be suitably shifted to account
for binders
.
universe
is the universe of theTypeName::ForAll
that was foundbinders
is the number of binders in scope
sourcefn try_fold_inference_lifetime(
&mut self,
var: InferenceVar,
outer_binder: DebruijnIndex,
) -> Result<Lifetime<I>, Self::Error>
fn try_fold_inference_lifetime( &mut self, var: InferenceVar, outer_binder: DebruijnIndex, ) -> Result<Lifetime<I>, Self::Error>
As with try_fold_inference_ty
, but for lifetimes.
sourcefn try_fold_inference_const(
&mut self,
ty: Ty<I>,
var: InferenceVar,
outer_binder: DebruijnIndex,
) -> Result<Const<I>, Self::Error>
fn try_fold_inference_const( &mut self, ty: Ty<I>, var: InferenceVar, outer_binder: DebruijnIndex, ) -> Result<Const<I>, Self::Error>
As with try_fold_inference_ty
, but for constants.