include!() in statement position expects an expression

06ec6ee
Opened by Simon Sapin at 2024-09-08 23:06:14

a.rs

fn main() {
    include!("b.rs")
}

b.rs

fn b() {}

rustc 1.18.0-nightly (2bd4b5c6d 2017-04-23)

error: expected expression, found keyword `fn`
 --> b.rs:1:1
  |
1 | fn b(){}
  | ^^
  1. I think you can just put ; after include!(..). The ; doesn't disturb parsing of the item therein.

    The current behavior is that a macro invocation at the start of a statement is considered to be a macro statement if

    • the macro invocation is followed by ;, or
    • the macro invocation uses {} to enclose its arguments.

    Otherwise the macro invocation is considered to be a macro expression, not a macro statement.

    I suppose the behavior is intentional. Otherwise the parser would not do such case analysis on parentheses/brackets/braces.

    Masaki Hara at 2017-04-28 14:13:37

  2. Same error message with a semi-colon:

    fn main() {
        include!("b.rs");
    }
    

    Simon Sapin at 2017-04-28 14:22:44

  3. Or curly braces:

    fn main() {
        include! { "b.rs" }
    }
    
    fn main() {
        include! { "b.rs" };
    }
    

    Simon Sapin at 2017-04-28 14:23:36

  4. I was only checking the behavior for another macro. Now I know what you mean. Sorry for bothering you.

    Masaki Hara at 2017-04-28 14:32:08

  5. I've hit this bug and it's pretty annoying in my case, since I'll have to resort to using build script.

    Martin Habovštiak at 2018-11-03 09:47:11

  6. triage: yep, this is still an issue

    here's a repro in txtar format.

    -- a.rs --
    fn main() {
        include!("b.rs");
    }
    -- b.rs --
    fn b() {}
    

    lolbinarycat at 2024-09-08 23:06:14