Skip to content

Using yield syntax without feature(generators) causes error even if it's hidden by #[cfg] attribute #66778

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
hatoo opened this issue Nov 26, 2019 · 1 comment

Comments

@hatoo
Copy link
Contributor

hatoo commented Nov 26, 2019

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.

#![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.

@hatoo 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 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
@Mark-Simulacrum
Copy link
Member

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants