Skip to content

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

@SimonSapin

Description

@SimonSapin
Contributor

a.rs

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

b.rs

fn b() {}

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

error: expected expression, found keyword `fn`
 --> b.rs:1:1
  |
1 | fn b(){}
  | ^^

Activity

qnighy

qnighy commented on Apr 28, 2017

@qnighy
Contributor

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.

SimonSapin

SimonSapin commented on Apr 28, 2017

@SimonSapin
ContributorAuthor

Same error message with a semi-colon:

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

SimonSapin commented on Apr 28, 2017

@SimonSapin
ContributorAuthor

Or curly braces:

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

qnighy commented on Apr 28, 2017

@qnighy
Contributor

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

added
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
T-langRelevant to the language team
on Jun 22, 2017
Kixunil

Kixunil commented on Nov 3, 2018

@Kixunil
Contributor

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

lolbinarycat

lolbinarycat commented on Sep 8, 2024

@lolbinarycat
Contributor

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() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.I-needs-decisionIssue: In need of a decision.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @qnighy@SimonSapin@Kixunil@Mark-Simulacrum@lolbinarycat

        Issue actions

          include!() in statement position expects an expression · Issue #41497 · rust-lang/rust