-
Notifications
You must be signed in to change notification settings - Fork 13k
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 #119894 - fmease:tilde-const-assoc-ty-bounds, r=compi…
…ler-errors Allow `~const` on associated type bounds again This follows from [this Zulip discussion](https://rust-lang.zulipchat.com/#narrow/stream/419616-t-compiler.2Fproject-const-traits/topic/projections.20on.20.28~.29const.20Trait.20.26.20.28~.29const.20assoc.20ty.20bounds). Basically in my opinion, it makes sense to allow `~const` on associated type bounds again since they're quite useful even though we haven't implemented the proposed syntax `<Ty as ~const Trait>::Proj`/`<Ty as const Trait>::Proj` yet; that can happen as a follow-up. This already allows more code to compile since `T::Assoc` where `T` is a type parameter and where the predicate `<T as ~const Trait>` is in the environment gets elaborated to (pseudo) `<T as ~const Trait>::Assoc`. ```rs #[const_trait] trait Trait { type Assoc: ~const Trait; fn func() -> i32; } const fn function<T: ~const Trait>() -> i32 { T::Assoc::func() } ``` `~const` associated type bounds also work together with `const` bounds: ```rs struct Type<const N: i32>; fn procedure<T: const Trait>() -> Type<{ T::Assoc::func() }> { // `Trait` comes from above Type } ``` NB: This PR also starts allowing `~const` bounds in the generics and the where-clause of trait associated types since it's trivial to support them. However, I don't know if those bounds are actually useful. Maybe we should continue to reject them? For reference, it wouldn't make any sense to allow `~const Trait` in GACs (generic associated constants, `generic_const_items`) because they'd be absolutely useless (contrary to `const Trait`). ~~[``@]rustbot`` ping project-const-traits~~ r? project-const-traits
- Loading branch information
Showing
13 changed files
with
202 additions
and
64 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
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
14 changes: 14 additions & 0 deletions
14
tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type-const-bound-usage-0.qualified.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,14 @@ | ||
error[E0277]: the trait bound `T: Trait` is not satisfied | ||
--> $DIR/assoc-type-const-bound-usage-0.rs:21:6 | ||
| | ||
LL | <T as /* FIXME: ~const */ Trait>::Assoc::func() | ||
| ^ the trait `Trait` is not implemented for `T` | ||
| | ||
help: consider further restricting this bound | ||
| | ||
LL | const fn qualified<T: ~const Trait + Trait>() -> i32 { | ||
| +++++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
24 changes: 24 additions & 0 deletions
24
tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type-const-bound-usage-0.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,24 @@ | ||
// FIXME(effects): Collapse the revisions into one once we support `<Ty as ~const Trait>::Proj`. | ||
// revisions: unqualified qualified | ||
//[unqualified] check-pass | ||
//[qualified] known-bug: unknown | ||
|
||
#![feature(const_trait_impl, effects)] | ||
|
||
#[const_trait] | ||
trait Trait { | ||
type Assoc: ~const Trait; | ||
fn func() -> i32; | ||
} | ||
|
||
#[cfg(unqualified)] | ||
const fn unqualified<T: ~const Trait>() -> i32 { | ||
T::Assoc::func() | ||
} | ||
|
||
#[cfg(qualified)] | ||
const fn qualified<T: ~const Trait>() -> i32 { | ||
<T as /* FIXME: ~const */ Trait>::Assoc::func() | ||
} | ||
|
||
fn main() {} |
14 changes: 14 additions & 0 deletions
14
tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type-const-bound-usage-1.qualified.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,14 @@ | ||
error[E0277]: the trait bound `T: Trait` is not satisfied | ||
--> $DIR/assoc-type-const-bound-usage-1.rs:23:43 | ||
| | ||
LL | fn qualified<T: const Trait>() -> Type<{ <T as /* FIXME: const */ Trait>::Assoc::func() }> { | ||
| ^ the trait `Trait` is not implemented for `T` | ||
| | ||
help: consider further restricting this bound | ||
| | ||
LL | fn qualified<T: const Trait + Trait>() -> Type<{ <T as /* FIXME: const */ Trait>::Assoc::func() }> { | ||
| +++++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
27 changes: 27 additions & 0 deletions
27
tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type-const-bound-usage-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,27 @@ | ||
// FIXME(effects): Collapse the revisions into one once we support `<Ty as const Trait>::Proj`. | ||
// revisions: unqualified qualified | ||
//[unqualified] check-pass | ||
//[qualified] known-bug: unknown | ||
|
||
#![feature(const_trait_impl, effects, generic_const_exprs)] | ||
#![allow(incomplete_features)] | ||
|
||
#[const_trait] | ||
trait Trait { | ||
type Assoc: ~const Trait; | ||
fn func() -> i32; | ||
} | ||
|
||
struct Type<const N: i32>; | ||
|
||
#[cfg(unqualified)] | ||
fn unqualified<T: const Trait>() -> Type<{ T::Assoc::func() }> { | ||
Type | ||
} | ||
|
||
#[cfg(qualified)] | ||
fn qualified<T: const Trait>() -> Type<{ <T as /* FIXME: const */ Trait>::Assoc::func() }> { | ||
Type | ||
} | ||
|
||
fn main() {} |
15 changes: 0 additions & 15 deletions
15
tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type-const-bound-usage.rs
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type-const-bound-usage.stderr
This file was deleted.
Oops, something went wrong.
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
22 changes: 11 additions & 11 deletions
22
tests/ui/rfcs/rfc-2632-const-trait-impl/assoc-type.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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
error: `~const` is not allowed here | ||
--> $DIR/assoc-type.rs:17:15 | ||
error[E0277]: the trait bound `NonConstAdd: ~const Add` is not satisfied | ||
--> $DIR/assoc-type.rs:35:16 | ||
| | ||
LL | type Bar: ~const std::ops::Add; | ||
| ^^^^^^ | ||
LL | type Bar = NonConstAdd; | ||
| ^^^^^^^^^^^ the trait `~const Add` is not implemented for `NonConstAdd` | ||
| | ||
= note: this item cannot have `~const` trait bounds | ||
|
||
error: `~const` can only be applied to `#[const_trait]` traits | ||
--> $DIR/assoc-type.rs:17:22 | ||
= help: the trait `Add` is implemented for `NonConstAdd` | ||
note: required by a bound in `Foo::Bar` | ||
--> $DIR/assoc-type.rs:31:15 | ||
| | ||
LL | type Bar: ~const std::ops::Add; | ||
| ^^^^^^^^^^^^^ | ||
LL | type Bar: ~const Add; | ||
| ^^^^^^^^^^ required by this bound in `Foo::Bar` | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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
18 changes: 18 additions & 0 deletions
18
tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-trait-assoc-tys.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 @@ | ||
// check-pass | ||
#![feature(const_trait_impl, effects)] | ||
|
||
#[const_trait] | ||
trait Trait { | ||
// FIXME(effects): `~const` bounds in trait associated types (excluding associated type bounds) | ||
// don't look super useful. Should we forbid them again? | ||
type Assoc<T: ~const Bound>; | ||
} | ||
|
||
impl const Trait for () { | ||
type Assoc<T: ~const Bound> = T; | ||
} | ||
|
||
#[const_trait] | ||
trait Bound {} | ||
|
||
fn main() {} |