Add feature gates for for and ? in consts
This commit is contained in:
parent
a985d8e6c7
commit
dbd126901a
8 changed files with 91 additions and 4 deletions
8
src/test/ui/consts/const-for-feature-gate.rs
Normal file
8
src/test/ui/consts/const-for-feature-gate.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// gate-test-const_for
|
||||
|
||||
const _: () = {
|
||||
for _ in 0..5 {}
|
||||
//~^ error: `for` is not allowed in a `const`
|
||||
};
|
||||
|
||||
fn main() {}
|
||||
11
src/test/ui/consts/const-for-feature-gate.stderr
Normal file
11
src/test/ui/consts/const-for-feature-gate.stderr
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
error[E0658]: `for` is not allowed in a `const`
|
||||
--> $DIR/const-for-feature-gate.rs:4:5
|
||||
|
|
||||
LL | for _ in 0..5 {}
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= 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`.
|
||||
9
src/test/ui/consts/const-try-feature-gate.rs
Normal file
9
src/test/ui/consts/const-try-feature-gate.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// gate-test-const_try
|
||||
|
||||
const fn t() -> Option<()> {
|
||||
Some(())?;
|
||||
//~^ error: `?` is not allowed in a `const fn`
|
||||
None
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
11
src/test/ui/consts/const-try-feature-gate.stderr
Normal file
11
src/test/ui/consts/const-try-feature-gate.stderr
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
error[E0658]: `?` is not allowed in a `const fn`
|
||||
--> $DIR/const-try-feature-gate.rs:4:5
|
||||
|
|
||||
LL | Some(())?;
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= 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`.
|
||||
39
src/test/ui/consts/const-try.rs
Normal file
39
src/test/ui/consts/const-try.rs
Normal file
|
|
@ -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();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue