You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#![cfg_attr(feature = "test_generator", feature(generators, generator_trait))]
fn main() {
println!("Hello, world!");
}
#[cfg(feature = "test_generator")]
fn test_generator() {
// Use some `yield`.
use std::ops::{GeneratorState, Generator};
use std::pin::Pin;
let mut generator = || {
yield 1;
return "foo"
};
match Pin::new(&mut generator).resume() {
GeneratorState::Yielded(1) => {}
_ => panic!("unexpected return from resume"),
}
match Pin::new(&mut generator).resume() {
GeneratorState::Complete("foo") => {}
_ => panic!("unexpected return from resume"),
}
}
When compiling this with the feature.
$ cargo run --features "test_generator"
warning: function is never used: `test_generator`
--> src\main.rs:8:4
|
8 | fn test_generator() {
| ^^^^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
Finished dev [unoptimized + debuginfo] target(s) in 0.04s
Running `target\debug\cfg-yield-test.exe`
Hello, world!
It's OK.
When compiling this without the feature.
$ cargo run
Compiling cfg-yield-test v0.1.0 (C:\Users\hato2\Desktop\cfg-yield-test)
error[E0658]: yield syntax is experimental
--> src\main.rs:14:9
|
14 | yield 1;
| ^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/43122
= help: add `#![feature(generators)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.
error: could not compile `cfg-yield-test`.
To learn more, run the command again with --verbose.
I expected to see this happen:
Compiling without the feature should be a success because codes that uses yield is hidden by the attribute.
I don't know this behavior is a bug but I want to write codes that use yield syntax when a feature is enabled and can be compiled by the stable compiler without a feature.
The text was updated successfully, but these errors were encountered:
hatoo
changed the title
Using yield syntax causes error even if it's hided by #[cfg] attribute
Using yield syntax causes error even if it's hidden by #[cfg] attribute
Nov 26, 2019
hatoo
changed the title
Using yield syntax causes error even if it's hidden by #[cfg] attribute
Using yield syntax without feature(generators) causes error even if it's hidden by #[cfg] attribute
Nov 26, 2019
This is currently intentional, though there is ongoing discussion about whether we should try to be better here, see #65860 which I'm going to close this in favor of.
I couldn't write a code that uses
yield
syntax only if some feature is enabled.I tried this code:
The complete project is here.
When compiling this with the feature.
It's OK.
When compiling this without the feature.
I expected to see this happen:
Compiling without the feature should be a success because codes that uses
yield
is hidden by the attribute.I don't know this behavior is a bug but I want to write codes that use
yield
syntax when a feature is enabled and can be compiled by thestable
compiler without a feature.The text was updated successfully, but these errors were encountered: