forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#87237 - jonas-schievink:const-for-and-try, r=…
…oli-obk Add feature gates for `for` and `?` in consts These operations seems *relatively* straightforward to support, and only seem to be blocked on `impl const Trait`. I have included a working test for `const_try`, but `const_for` is currently unusable without reimplementing *every single* defaulted `Iterator` method, so I didn't do that. (both features still need tracking issues before this is merged)
- Loading branch information
Showing
19 changed files
with
162 additions
and
34 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
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
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,8 @@ | ||
// gate-test-const_for | ||
|
||
const _: () = { | ||
for _ in 0..5 {} | ||
//~^ error: `for` is not allowed in a `const` | ||
}; | ||
|
||
fn main() {} |
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,12 @@ | ||
error[E0658]: `for` is not allowed in a `const` | ||
--> $DIR/const-for-feature-gate.rs:4:5 | ||
| | ||
LL | for _ in 0..5 {} | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #87575 <https://github.com/rust-lang/rust/issues/87575> for more information | ||
= help: add `#![feature(const_for)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
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,10 @@ | ||
#![feature(const_for)] | ||
#![feature(const_mut_refs)] | ||
|
||
const _: () = { | ||
for _ in 0..5 {} | ||
//~^ error: calls in constants are limited to | ||
//~| error: calls in constants are limited to | ||
}; | ||
|
||
fn main() {} |
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,15 @@ | ||
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants | ||
--> $DIR/const-for.rs:5:14 | ||
| | ||
LL | for _ in 0..5 {} | ||
| ^^^^ | ||
|
||
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants | ||
--> $DIR/const-for.rs:5:14 | ||
| | ||
LL | for _ in 0..5 {} | ||
| ^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0015`. |
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,9 @@ | ||
// gate-test-const_try | ||
|
||
const fn t() -> Option<()> { | ||
Some(())?; | ||
//~^ error: `?` is not allowed in a `const fn` | ||
None | ||
} | ||
|
||
fn main() {} |
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,12 @@ | ||
error[E0658]: `?` is not allowed in a `const fn` | ||
--> $DIR/const-try-feature-gate.rs:4:5 | ||
| | ||
LL | Some(())?; | ||
| ^^^^^^^^^ | ||
| | ||
= note: see issue #74935 <https://github.com/rust-lang/rust/issues/74935> for more information | ||
= help: add `#![feature(const_try)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
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,39 @@ | ||
// check-pass | ||
|
||
// Demonstrates what's needed to make use of `?` in const contexts. | ||
|
||
#![crate_type = "lib"] | ||
#![feature(try_trait_v2)] | ||
#![feature(const_trait_impl)] | ||
#![feature(const_try)] | ||
|
||
use std::ops::{ControlFlow, FromResidual, Try}; | ||
|
||
struct TryMe; | ||
struct Error; | ||
|
||
impl const FromResidual<Error> for TryMe { | ||
fn from_residual(residual: Error) -> Self { | ||
TryMe | ||
} | ||
} | ||
|
||
impl const Try for TryMe { | ||
type Output = (); | ||
type Residual = Error; | ||
fn from_output(output: Self::Output) -> Self { | ||
TryMe | ||
} | ||
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> { | ||
ControlFlow::Break(Error) | ||
} | ||
} | ||
|
||
const fn t() -> TryMe { | ||
TryMe?; | ||
TryMe | ||
} | ||
|
||
const _: () = { | ||
t(); | ||
}; |
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,19 +1,25 @@ | ||
error[E0744]: `for` is not allowed in a `const` | ||
error[E0658]: `for` is not allowed in a `const` | ||
--> $DIR/loop.rs:53:5 | ||
| | ||
LL | / for i in 0..4 { | ||
LL | | x += i; | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: see issue #87575 <https://github.com/rust-lang/rust/issues/87575> for more information | ||
= help: add `#![feature(const_for)]` to the crate attributes to enable | ||
|
||
error[E0744]: `for` is not allowed in a `const` | ||
error[E0658]: `for` is not allowed in a `const` | ||
--> $DIR/loop.rs:57:5 | ||
| | ||
LL | / for i in 0..4 { | ||
LL | | x += i; | ||
LL | | } | ||
| |_____^ | ||
| | ||
= note: see issue #87575 <https://github.com/rust-lang/rust/issues/87575> for more information | ||
= help: add `#![feature(const_for)]` to the crate attributes to enable | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0744`. | ||
For more information about this error, try `rustc --explain E0658`. |
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,9 +1,12 @@ | ||
error[E0744]: `?` is not allowed in a `const fn` | ||
error[E0658]: `?` is not allowed in a `const fn` | ||
--> $DIR/try.rs:6:5 | ||
| | ||
LL | x?; | ||
| ^^ | ||
| | ||
= note: see issue #74935 <https://github.com/rust-lang/rust/issues/74935> for more information | ||
= help: add `#![feature(const_try)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0744`. | ||
For more information about this error, try `rustc --explain E0658`. |
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
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,9 +1,12 @@ | ||
error[E0744]: `?` is not allowed in a `const fn` | ||
error[E0658]: `?` is not allowed in a `const fn` | ||
--> $DIR/hir-const-check.rs:11:9 | ||
| | ||
LL | Some(())?; | ||
| ^^^^^^^^^ | ||
| | ||
= note: see issue #74935 <https://github.com/rust-lang/rust/issues/74935> for more information | ||
= help: add `#![feature(const_try)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0744`. | ||
For more information about this error, try `rustc --explain E0658`. |