Error to enable feature gate prevents error from incorrect placement of feature gate

76cccf1
Opened by comex at 2020-06-11 18:02:16

Suppose I'm on nightly and I try to use gated syntax in a module (other than the crate root). As expected, I get an error:

feature_gated.rs:3:5: 3:10 error: box expression syntax is experimental; you can call `Box::new` instead. (see issue #27779)
feature_gated.rs:3     box 2;
                       ^~~~~
feature_gated.rs:3:5: 3:10 help: add #![feature(box_syntax)] to the crate attributes to enable

An easy mistake to make is to misunderstand "crate attributes" and add the suggested feature attribute to the top of the current file instead. The problem is that if I do so, rustc doesn't tell me what I did wrong: it just seems to silently ignore the attribute.

In fact, there is a lint that triggers on the attribute:

feature_gated.rs:1:1: 1:24 warning: crate-level attribute should be in the root module, #[warn(unused_attributes)] on by default
feature_gated.rs:1 #![feature(box_syntax)]

...but it only shows up if I remove the gated syntax to allow compilation to proceed to that phase. This doesn't help someone stuck on the original error.

  1. Triage: no fundamental changes here. For an example that reproduces today:

    mod foo {
        fn lol() {
            asm!("");
        }
    }
    

    errors with this on nightly:

    error[E0658]: use of unstable library feature 'asm': inline assembly is not stable enough for use and is subject to change
     --> src/lib.rs:4:9
      |
    4 |         asm!("");
      |         ^^^
      |
      = note: for more information, see https://github.com/rust-lang/rust/issues/29722
      = help: add `#![feature(asm)]` to the crate attributes to enable
    

    This is because stable doesn't even suggest adding the gate, which I bet reduces this issue a lot. Anyway, if you try to fix it by adding it to the top of the file, that looks like this in the playpen:

    mod foo {
        #![feature(asm)]
        fn lol() {
            asm!();
        }
    }
    

    and you get

    error[E0658]: use of unstable library feature 'asm': inline assembly is not stable enough for use and is subject to change
     --> src/lib.rs:4:9
      |
    4 |         asm!("");
      |         ^^^
      |
      = note: for more information, see https://github.com/rust-lang/rust/issues/29722
      = help: add `#![feature(asm)]` to the crate attributes to enable
    

    if you remove the asm call:

    mod foo {
        #![feature(asm)]
        fn lol() {
        }
    }
    

    you still get the good diagnostic

    warning: crate-level attribute should be in the root module
     --> src/lib.rs:2:5
      |
    2 |     #![feature(asm)]
      |     ^^^^^^^^^^^^^^^^
    
    

    Steve Klabnik at 2019-12-25 17:07:34