Skip to content
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

rework winnowing to sensibly handle global where-bounds #132325

Merged
merged 2 commits into from
Dec 17, 2024
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
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#![feature(extract_if)]
#![feature(if_let_guard)]
#![feature(iter_intersperse)]
#![feature(iterator_try_reduce)]
#![feature(let_chains)]
#![feature(never_type)]
#![feature(rustdoc_internals)]
Expand Down
546 changes: 241 additions & 305 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@ check-pass

// This caused a regression in a crater run in #132325.
//
// The underlying issue is a really subtle implementation detail.
//
// When building the `param_env` for `Trait` we start out with its
// explicit predicates `Self: Trait` and `Self: for<'a> Super<'a, { 1 + 1 }>`.
//
// When normalizing the environment we also elaborate. This implicitly
// deduplicates its returned predicates. We currently first eagerly
// normalize constants in the unnormalized param env to avoid issues
// caused by our lack of deferred alias equality.
//
// So we actually elaborate `Self: Trait` and `Self: for<'a> Super<'a, 2>`,
// resulting in a third `Self: for<'a> Super<'a, { 1 + 1 }>` predicate which
// then gets normalized to `Self: for<'a> Super<'a, 2>` at which point we
// do not deduplicate however. By failing to handle equal where-bounds in
// candidate selection, this caused ambiguity when checking that `Trait` is
// well-formed.
trait Super<'a, const N: usize> {}
trait Trait: for<'a> Super<'a, { 1 + 1 }> {}
fn main() {}
18 changes: 18 additions & 0 deletions tests/ui/traits/winnowing/global-non-global-env-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ check-pass

// A regression test for an edge case of candidate selection
// in the old trait solver, see #132325 for more details.

trait Trait<T> {}
impl<T> Trait<T> for () {}

fn impls_trait<T: Trait<U>, U>(_: T) -> U { todo!() }
fn foo<T>() -> u32
where
(): Trait<u32>,
(): Trait<T>,
{
impls_trait(())
}

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/traits/winnowing/global-non-global-env-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ check-pass

// A regression test for an edge case of candidate selection
// in the old trait solver, see #132325 for more details. Unlike
// the first test, this one has two impl candidates.

trait Trait<T> {}
impl Trait<u32> for () {}
impl Trait<u64> for () {}

fn impls_trait<T: Trait<U>, U>(_: T) -> U { todo!() }
fn foo<T>() -> u32
where
(): Trait<u32>,
(): Trait<T>,
{
impls_trait(())
}

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/traits/winnowing/global-non-global-env-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ check-pass

// A regression test for an edge case of candidate selection
// in the old trait solver, see #132325 for more details. Unlike
// the second test, the where-bounds are in a different order.

trait Trait<T> {}
impl Trait<u32> for () {}
impl Trait<u64> for () {}

fn impls_trait<T: Trait<U>, U>(_: T) -> U { todo!() }
fn foo<T>() -> u32
where
(): Trait<T>,
(): Trait<u32>,
{
impls_trait(())
}

fn main() {}
21 changes: 21 additions & 0 deletions tests/ui/traits/winnowing/global-non-global-env-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ check-pass

// A regression test for an edge case of candidate selection
// in the old trait solver, see #132325 for more details. Unlike
// the third test, this one has 3 impl candidates.

trait Trait<T> {}
impl Trait<u32> for () {}
impl Trait<u64> for () {}
impl Trait<u128> for () {}

fn impls_trait<T: Trait<U>, U>(_: T) -> U { todo!() }
fn foo<T>() -> u32
where
(): Trait<T>,
(): Trait<u32>,
{
impls_trait(())
}

fn main() {}
Loading