Skip to content

Fix built-in Fn impls when generics are involved #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions chalk-solve/src/clauses/builtin_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ pub fn add_builtin_assoc_program_clauses<I: Interner>(
) -> Result<(), Floundered> {
match well_known {
WellKnownTrait::FnOnce => {
fn_family::add_fn_trait_program_clauses(db, builder, well_known, self_ty)?;
// If `self_ty` contains bound vars, we want to universally quantify them.
// `Generalize` collects them for us.
let generalized = generalize::Generalize::apply(db.interner(), &self_ty);

builder.push_binders(&generalized, |builder, self_ty| {
fn_family::add_fn_trait_program_clauses(db, builder, well_known, self_ty)?;
Ok(())
})
}
_ => {}
_ => Ok(()),
}
Ok(())
}

/// Given a trait ref `T0: Trait` and a list of types `U0..Un`, pushes a clause of the form
Expand Down
5 changes: 0 additions & 5 deletions chalk-solve/src/clauses/builtin_traits/fn_family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ pub fn add_fn_trait_program_clauses<I: Interner>(
let bound = fn_def_datum
.binders
.substitute(builder.interner(), &apply.substitution);
let self_ty = ApplicationTy {
name: apply.name,
substitution: builder.substitution_in_scope(),
}
.intern(interner);
push_clauses_for_apply(
db,
builder,
Expand Down
68 changes: 68 additions & 0 deletions tests/test/fn_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,73 @@ fn fn_def_implements_fn_traits() {
} yields {
"Unique"
}

goal {
bar: Fn<(i32,)>
} yields {
"Unique"
}

goal {
Normalize(<bar as FnOnce<(i32,)>>::Output -> ())
} yields {
"Unique"
}

goal {
baz: Fn<(i32,)>
} yields {
"Unique"
}

goal {
Normalize(<baz as FnOnce<(i32,)>>::Output -> u8)
} yields {
"Unique"
}
}
}

#[test]
fn generic_fn_implements_fn_traits() {
test! {
program {
#[lang(fn_once)]
trait FnOnce<Args> {
type Output;
}

#[lang(fn_mut)]
trait FnMut<Args> where Self: FnOnce<Args> { }

#[lang(fn)]
trait Fn<Args> where Self: FnMut<Args> { }

fn foo<T>(t: T) -> T;
}

goal {
exists<T> { foo<T>: Fn<(T,)> }
} yields {
"Unique"
}

goal {
forall<T> { foo<T>: Fn<(T,)> }
} yields {
"Unique"
}

goal {
exists<T> { Normalize(<foo<T> as FnOnce<(T,)>>::Output -> T) }
} yields {
"Unique"
}

goal {
forall<T> { Normalize(<foo<T> as FnOnce<(T,)>>::Output -> T) }
} yields {
"Unique"
}
}
}