rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.89k stars 12.67k forks source link

include!() in statement position expects an expression #41497

Open SimonSapin opened 7 years ago

SimonSapin commented 7 years ago

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(){}
  | ^^
qnighy commented 7 years ago

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

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.

SimonSapin commented 7 years ago

Same error message with a semi-colon:

fn main() {
    include!("b.rs");
}
SimonSapin commented 7 years ago

Or curly braces:

fn main() {
    include! { "b.rs" }
}
fn main() {
    include! { "b.rs" };
}
qnighy commented 7 years ago

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

Kixunil commented 5 years ago

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

lolbinarycat commented 1 month ago

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() {}