forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#120836 - lcnr:param-env-hide-impl, r=BoxyUwU hide impls if trait bound is proven from env AVERT YOUR EYES `@compiler-errors` fixes rust-lang/trait-system-refactor-initiative#76 and rust-lang/trait-system-refactor-initiative#12 (comment) this is kinda ugly and I hate it, but I wasn't able to think of a cleaner approach for now. I am also unsure whether we have to refine this filtering later on, so by making the change pretty minimal it should be easier to improve going forward. r? `@BoxyUwU`
- Loading branch information
Showing
18 changed files
with
388 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/ui/traits/next-solver/cycles/fixpoint-rerun-all-cycle-heads.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/ui/traits/next-solver/env-shadows-impls/ambig-env-no-shadow.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// compile-flags: -Znext-solver | ||
// check-pass | ||
|
||
// If a trait goal is proven using the environment, we discard | ||
// impl candidates when normalizing. However, in this example | ||
// the env candidates start as ambiguous and end up not applying, | ||
// so normalization should succeed later on. | ||
|
||
trait Trait<T>: Sized { | ||
type Assoc: From<Self>; | ||
} | ||
|
||
impl<T, U> Trait<U> for T { | ||
type Assoc = T; | ||
} | ||
|
||
fn mk_assoc<T: Trait<U>, U>(t: T, _: U) -> <T as Trait<U>>::Assoc { | ||
t.into() | ||
} | ||
|
||
fn generic<T>(t: T) -> T | ||
where | ||
T: Trait<u32>, | ||
T: Trait<i16>, | ||
{ | ||
let u = Default::default(); | ||
|
||
// at this point we have 2 ambig env candidates | ||
let ret: T = mk_assoc(t, u); | ||
|
||
// now both env candidates don't apply, so we're now able to | ||
// normalize using this impl candidates. For this to work | ||
// the normalizes-to must have remained ambiguous above. | ||
let _: u8 = u; | ||
ret | ||
} | ||
|
||
fn main() { | ||
assert_eq!(generic(1), 1); | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/ui/traits/next-solver/env-shadows-impls/discard-impls-shadowed-by-env-1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// compile-flags: -Znext-solver | ||
// check-pass | ||
|
||
// Normalizing `<T as Trait>::TraitAssoc` in the elaborated environment | ||
// `[T: Trait, T: Super, <T as Super>::SuperAssoc = <T as Trait>::TraitAssoc]` | ||
// has a single impl candidate, which uses the environment to | ||
// normalize `<T as Trait>::TraitAssoc` to itself. We avoid this overflow | ||
// by discarding impl candidates the trait bound is proven by a where-clause. | ||
|
||
// https://github.com/rust-lang/trait-system-refactor-initiative/issues/76 | ||
trait Super { | ||
type SuperAssoc; | ||
} | ||
|
||
trait Trait: Super<SuperAssoc = Self::TraitAssoc> { | ||
type TraitAssoc; | ||
} | ||
|
||
impl<T, U> Trait for T | ||
where | ||
T: Super<SuperAssoc = U>, | ||
{ | ||
type TraitAssoc = U; | ||
} | ||
|
||
fn overflow<T: Trait>() { | ||
let x: <T as Trait>::TraitAssoc; | ||
} | ||
|
||
fn main() {} |
29 changes: 29 additions & 0 deletions
29
tests/ui/traits/next-solver/env-shadows-impls/discard-impls-shadowed-by-env-2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// revisions: next current | ||
//[next] compile-flags: -Znext-solver | ||
// check-pass | ||
|
||
#![allow(warnings)] | ||
trait Trait<U> { | ||
type Assoc; | ||
} | ||
|
||
impl<T> Trait<u64> for T { | ||
type Assoc = T; | ||
} | ||
|
||
fn lazy_init<T: Trait<U>, U>() -> (T, <T as Trait<U>>::Assoc) { | ||
todo!() | ||
} | ||
|
||
fn foo<T: Trait<u32, Assoc = T>>(x: T) { | ||
// When considering impl candidates to be equally valid as env candidates | ||
// this ends up being ambiguous as `U` can be both `u32´ and `u64` here. | ||
// | ||
// This is acceptable breakage but we should still note that it's | ||
// theoretically breaking. | ||
let (delayed, mut proj) = lazy_init::<_, _>(); | ||
proj = x; | ||
let _: T = delayed; | ||
} | ||
|
||
fn main() {} |
18 changes: 18 additions & 0 deletions
18
tests/ui/traits/next-solver/env-shadows-impls/discard-impls-shadowed-by-env-3.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// compile-flags: -Znext-solver | ||
// check-pass | ||
|
||
// If we normalize using the impl here the constraints from normalization and | ||
// trait goals can differ. This is especially bad if normalization results | ||
// in stronger constraints. | ||
trait Trait<'a> { | ||
type Assoc; | ||
} | ||
|
||
impl<T> Trait<'static> for T { | ||
type Assoc = (); | ||
} | ||
|
||
// normalizing requires `'a == 'static`, the trait bound does not. | ||
fn foo<'a, T: Trait<'a>>(_: T::Assoc) {} | ||
|
||
fn main() {} |
29 changes: 29 additions & 0 deletions
29
...ui/traits/next-solver/env-shadows-impls/normalizes_to_ignores_unnormalizable_candidate.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// compile-flags: -Znext-solver | ||
|
||
// Checks whether the new solver is smart enough to infer `?0 = U` when solving: | ||
// `normalizes-to(<Vec<?0> as Trait>::Assoc, u8)` | ||
// with `normalizes-to(<Vec<U> as Trait>::Assoc, u8)` in the paramenv even when | ||
// there is a separate `Vec<T>: Trait` bound in the paramenv. | ||
// | ||
// We currently intentionally do not guide inference this way. | ||
|
||
trait Trait { | ||
type Assoc; | ||
} | ||
|
||
fn foo<T: Trait<Assoc = u8>>(x: T) {} | ||
|
||
fn unconstrained<T>() -> Vec<T> { | ||
todo!() | ||
} | ||
|
||
fn bar<T, U>() | ||
where | ||
Vec<T>: Trait, | ||
Vec<U>: Trait<Assoc = u8>, | ||
{ | ||
foo(unconstrained()) | ||
//~^ ERROR type annotations needed | ||
} | ||
|
||
fn main() {} |
10 changes: 5 additions & 5 deletions
10
...nnormalizable_candidate.self_infer.stderr → ...o_ignores_unnormalizable_candidate.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.