-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hide impls if trait bound is proven from env
- Loading branch information
Showing
18 changed files
with
367 additions
and
122 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
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
// compile-flags: -Znext-solver | ||
// check-pass | ||
|
||
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(); | ||
let ret: T = mk_assoc(t, u); | ||
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
31 changes: 31 additions & 0 deletions
31
tests/ui/traits/next-solver/env-shadows-impls/param-candidate-shadows-project.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,31 @@ | ||
// compile-flags: -Znext-solver | ||
|
||
trait Foo { | ||
type Assoc; | ||
} | ||
|
||
trait Bar {} | ||
|
||
impl<T> Foo for T { | ||
type Assoc = i32; | ||
} | ||
|
||
impl<T> Bar for T where T: Foo<Assoc = i32> {} | ||
|
||
fn require_bar<T: Bar>() {} | ||
|
||
fn foo<T: Foo>() { | ||
// Unlike the classic solver, the new solver previous projected | ||
// `<T as Foo>::Assoc = _` down to `i32` even though there's a param-env | ||
// candidate here, since we don't assemble any param-env projection | ||
// candidates for `T: Foo` alone. | ||
// | ||
// However, allowing impl candidates shadowed by env candidates results | ||
// in multiple issues, so we explicitly hide them, e.g. | ||
// | ||
// https://github.com/rust-lang/trait-system-refactor-initiative/issues/76 | ||
require_bar::<T>(); | ||
//~^ ERROR the trait bound `T: Bar` is not satisfied | ||
} | ||
|
||
fn main() {} |
19 changes: 19 additions & 0 deletions
19
tests/ui/traits/next-solver/env-shadows-impls/param-candidate-shadows-project.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0277]: the trait bound `T: Bar` is not satisfied | ||
--> $DIR/param-candidate-shadows-project.rs:27:19 | ||
| | ||
LL | require_bar::<T>(); | ||
| ^ the trait `Bar` is not implemented for `T` | ||
| | ||
note: required by a bound in `require_bar` | ||
--> $DIR/param-candidate-shadows-project.rs:15:19 | ||
| | ||
LL | fn require_bar<T: Bar>() {} | ||
| ^^^ required by this bound in `require_bar` | ||
help: consider further restricting this bound | ||
| | ||
LL | fn foo<T: Foo + Bar>() { | ||
| +++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
Oops, something went wrong.