Skip to content

Commit

Permalink
Auto merge of #709 - LeSeulArtichaut:fallible-folder, r=jackh726
Browse files Browse the repository at this point in the history
Introduce `Folder::Error`

Equivalent to rust-lang/rust#85469, cc rust-lang/compiler-team#432 rust-lang/types-team#16.

This compiles, but there are implementations of `Folder` that still use `Fallible<T>` instead of `Result<T, Self::Error>`. I can make that change if it is deemed beneficial.

r? `@jackh726`
  • Loading branch information
bors committed Nov 28, 2021
2 parents 9240a00 + 0a4ba8a commit 514a60a
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 120 deletions.
6 changes: 3 additions & 3 deletions chalk-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ fn derive_fold(mut s: synstructure::Structure) -> TokenStream {
quote! {
type Result = #result;

fn fold_with<'i>(
fn fold_with<'i, E>(
self,
folder: &mut dyn ::chalk_ir::fold::Folder < 'i, #interner >,
folder: &mut dyn ::chalk_ir::fold::Folder < 'i, #interner, Error = E >,
outer_binder: ::chalk_ir::DebruijnIndex,
) -> ::chalk_ir::Fallible<Self::Result>
) -> ::std::result::Result<Self::Result, E>
where
#interner: 'i,
{
Expand Down
4 changes: 3 additions & 1 deletion chalk-engine/src/normalize_deep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ impl<'i, I: Interner> Folder<'i, I> for DeepNormalizer<'_, 'i, I>
where
I: 'i,
{
fn as_dyn(&mut self) -> &mut dyn Folder<'i, I> {
type Error = NoSolution;

fn as_dyn(&mut self) -> &mut dyn Folder<'i, I, Error = Self::Error> {
self
}

Expand Down
8 changes: 4 additions & 4 deletions chalk-engine/src/strand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt::Debug;
use chalk_derive::HasInterner;
use chalk_ir::fold::{Fold, Folder};
use chalk_ir::interner::Interner;
use chalk_ir::{Canonical, DebruijnIndex, Fallible, UniverseMap};
use chalk_ir::{Canonical, DebruijnIndex, UniverseMap};

#[derive(Clone, Debug, HasInterner)]
pub(crate) struct Strand<I: Interner> {
Expand Down Expand Up @@ -37,11 +37,11 @@ pub(crate) struct SelectedSubgoal {

impl<I: Interner> Fold<I> for Strand<I> {
type Result = Strand<I>;
fn fold_with<'i>(
fn fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i,
{
Expand Down
104 changes: 56 additions & 48 deletions chalk-ir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,24 @@ pub trait Folder<'i, I: Interner>
where
I: 'i,
{
/// The type this folder returns when folding fails. This is
/// commonly [`NoSolution`].
type Error;

/// Creates a `dyn` value from this folder. Unfortunately, this
/// must be added manually to each impl of Folder; it permits the
/// default implements below to create a `&mut dyn Folder` from
/// `Self` without knowing what `Self` is (by invoking this
/// method). Effectively, this limits impls of `Folder` to types
/// for which we are able to create a dyn value (i.e., not `[T]`
/// types).
fn as_dyn(&mut self) -> &mut dyn Folder<'i, I>;
fn as_dyn(&mut self) -> &mut dyn Folder<'i, I, Error = Self::Error>;

/// Top-level callback: invoked for each `Ty<I>` that is
/// encountered when folding. By default, invokes
/// `super_fold_with`, which will in turn invoke the more
/// specialized folding methods below, like `fold_free_var_ty`.
fn fold_ty(&mut self, ty: Ty<I>, outer_binder: DebruijnIndex) -> Fallible<Ty<I>> {
fn fold_ty(&mut self, ty: Ty<I>, outer_binder: DebruijnIndex) -> Result<Ty<I>, Self::Error> {
ty.super_fold_with(self.as_dyn(), outer_binder)
}

Expand All @@ -83,7 +87,7 @@ where
&mut self,
lifetime: Lifetime<I>,
outer_binder: DebruijnIndex,
) -> Fallible<Lifetime<I>> {
) -> Result<Lifetime<I>, Self::Error> {
lifetime.super_fold_with(self.as_dyn(), outer_binder)
}

Expand All @@ -95,7 +99,7 @@ where
&mut self,
constant: Const<I>,
outer_binder: DebruijnIndex,
) -> Fallible<Const<I>> {
) -> Result<Const<I>, Self::Error> {
constant.super_fold_with(self.as_dyn(), outer_binder)
}

Expand All @@ -104,12 +108,16 @@ where
&mut self,
clause: ProgramClause<I>,
outer_binder: DebruijnIndex,
) -> Fallible<ProgramClause<I>> {
) -> Result<ProgramClause<I>, Self::Error> {
clause.super_fold_with(self.as_dyn(), outer_binder)
}

/// Invoked for every goal. By default, recursively folds the goals contents.
fn fold_goal(&mut self, goal: Goal<I>, outer_binder: DebruijnIndex) -> Fallible<Goal<I>> {
fn fold_goal(
&mut self,
goal: Goal<I>,
outer_binder: DebruijnIndex,
) -> Result<Goal<I>, Self::Error> {
goal.super_fold_with(self.as_dyn(), outer_binder)
}

Expand All @@ -133,7 +141,7 @@ where
&mut self,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Fallible<Ty<I>> {
) -> Result<Ty<I>, Self::Error> {
if self.forbid_free_vars() {
panic!(
"unexpected free variable with depth `{:?}` with outer binder {:?}",
Expand All @@ -150,7 +158,7 @@ where
&mut self,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Fallible<Lifetime<I>> {
) -> Result<Lifetime<I>, Self::Error> {
if self.forbid_free_vars() {
panic!(
"unexpected free variable with depth `{:?}` with outer binder {:?}",
Expand All @@ -168,7 +176,7 @@ where
ty: Ty<I>,
bound_var: BoundVar,
outer_binder: DebruijnIndex,
) -> Fallible<Const<I>> {
) -> Result<Const<I>, Self::Error> {
if self.forbid_free_vars() {
panic!(
"unexpected free variable with depth `{:?}` with outer binder {:?}",
Expand Down Expand Up @@ -202,7 +210,7 @@ where
&mut self,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Fallible<Ty<I>> {
) -> Result<Ty<I>, Self::Error> {
if self.forbid_free_placeholders() {
panic!("unexpected placeholder type `{:?}`", universe)
} else {
Expand All @@ -216,7 +224,7 @@ where
&mut self,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Fallible<Lifetime<I>> {
) -> Result<Lifetime<I>, Self::Error> {
if self.forbid_free_placeholders() {
panic!("unexpected placeholder lifetime `{:?}`", universe)
} else {
Expand All @@ -231,7 +239,7 @@ where
ty: Ty<I>,
universe: PlaceholderIndex,
outer_binder: DebruijnIndex,
) -> Fallible<Const<I>> {
) -> Result<Const<I>, Self::Error> {
if self.forbid_free_placeholders() {
panic!("unexpected placeholder const `{:?}`", universe)
} else {
Expand Down Expand Up @@ -259,7 +267,7 @@ where
var: InferenceVar,
kind: TyVariableKind,
outer_binder: DebruijnIndex,
) -> Fallible<Ty<I>> {
) -> Result<Ty<I>, Self::Error> {
if self.forbid_inference_vars() {
panic!("unexpected inference type `{:?}`", var)
} else {
Expand All @@ -273,7 +281,7 @@ where
&mut self,
var: InferenceVar,
outer_binder: DebruijnIndex,
) -> Fallible<Lifetime<I>> {
) -> Result<Lifetime<I>, Self::Error> {
if self.forbid_inference_vars() {
panic!("unexpected inference lifetime `'{:?}`", var)
} else {
Expand All @@ -288,7 +296,7 @@ where
ty: Ty<I>,
var: InferenceVar,
outer_binder: DebruijnIndex,
) -> Fallible<Const<I>> {
) -> Result<Const<I>, Self::Error> {
if self.forbid_inference_vars() {
panic!("unexpected inference const `{:?}`", var)
} else {
Expand Down Expand Up @@ -318,11 +326,11 @@ pub trait Fold<I: Interner>: Debug {
/// folder. Typically `binders` starts as 0, but is adjusted when
/// we encounter `Binders<T>` in the IR or other similar
/// constructs.
fn fold_with<'i>(
fn fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i;
}
Expand All @@ -332,11 +340,11 @@ pub trait Fold<I: Interner>: Debug {
/// the contents of the type.
pub trait SuperFold<I: Interner>: Fold<I> {
/// Recursively folds the value.
fn super_fold_with<'i>(
fn super_fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i;
}
Expand All @@ -347,11 +355,11 @@ pub trait SuperFold<I: Interner>: Fold<I> {
impl<I: Interner> Fold<I> for Ty<I> {
type Result = Ty<I>;

fn fold_with<'i>(
fn fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i,
{
Expand All @@ -364,11 +372,11 @@ impl<I> SuperFold<I> for Ty<I>
where
I: Interner,
{
fn super_fold_with<'i>(
fn super_fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Ty<I>>
) -> Result<Ty<I>, E>
where
I: 'i,
{
Expand Down Expand Up @@ -477,11 +485,11 @@ where
impl<I: Interner> Fold<I> for Lifetime<I> {
type Result = Lifetime<I>;

fn fold_with<'i>(
fn fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i,
{
Expand All @@ -493,11 +501,11 @@ impl<I> SuperFold<I> for Lifetime<I>
where
I: Interner,
{
fn super_fold_with<'i>(
fn super_fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Lifetime<I>>
) -> Result<Lifetime<I>, E>
where
I: 'i,
{
Expand Down Expand Up @@ -535,11 +543,11 @@ where
impl<I: Interner> Fold<I> for Const<I> {
type Result = Const<I>;

fn fold_with<'i>(
fn fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i,
{
Expand All @@ -551,11 +559,11 @@ impl<I> SuperFold<I> for Const<I>
where
I: Interner,
{
fn super_fold_with<'i>(
fn super_fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Const<I>>
) -> Result<Const<I>, E>
where
I: 'i,
{
Expand Down Expand Up @@ -592,11 +600,11 @@ where
impl<I: Interner> Fold<I> for Goal<I> {
type Result = Goal<I>;

fn fold_with<'i>(
fn fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i,
{
Expand All @@ -606,11 +614,11 @@ impl<I: Interner> Fold<I> for Goal<I> {

/// Superfold folds recursively.
impl<I: Interner> SuperFold<I> for Goal<I> {
fn super_fold_with<'i>(
fn super_fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i,
{
Expand All @@ -630,11 +638,11 @@ impl<I: Interner> SuperFold<I> for Goal<I> {
impl<I: Interner> Fold<I> for ProgramClause<I> {
type Result = ProgramClause<I>;

fn fold_with<'i>(
fn fold_with<'i, E>(
self,
folder: &mut dyn Folder<'i, I>,
folder: &mut dyn Folder<'i, I, Error = E>,
outer_binder: DebruijnIndex,
) -> Fallible<Self::Result>
) -> Result<Self::Result, E>
where
I: 'i,
{
Expand Down
Loading

0 comments on commit 514a60a

Please sign in to comment.