Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Only the first macro expansion of an `include!`d file is expanded. #35560

Open
emilio opened this Issue Aug 10, 2016 · 5 comments

Comments

Projects
None yet
7 participants
@emilio
Copy link
Contributor

emilio commented Aug 10, 2016

If you have a file that invokes a macro and you include! it, the following happens:

main.rs

fn main() {
    macro_rules! my_macro {
        ($name:expr) => {{
            printn!("{}", $name);
        }}
    }

    include!("helper.rs");
}

helper.rs

my_macro!("foo");
my_macro!("bar");

STR

$ rustc main.rs
$ ./main
foo

Expected output

Should print foo and bar, just as if the code would have been inlined.

Tested with latest nightly and stable:

rustc 1.12.0-nightly (080e0e072 2016-08-08)
rustc 1.10.0 (cfcb716cf 2016-07-03)
@TimNN

This comment has been minimized.

Copy link
Contributor

TimNN commented Aug 10, 2016

This is unrelated to the macros used in the included file, the following exhibits the same problem:

main.rs:

fn print(s: &str) { println!("{}", s); }

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

helper.rs:

print("foo");
print("bar");
print("foobar");

It looks like the issue is rather that include! inside of a function expands to the first statement only.

Wrapping the contents of helper.rs in braces prints all three lines.

Also note that the documentation of include! says:

Parse the current given file as an expression.

Thus the current behaviour is probably as documented.

@jonas-schievink

This comment has been minimized.

Copy link
Member

jonas-schievink commented Aug 10, 2016

But the contents of helper.rs aren't a valid expression, it's only valid as a list of statements. Maybe it's parsed as an expression and the remainder is thrown away (like macros used to do)?

@emilio

This comment has been minimized.

Copy link
Contributor Author

emilio commented Aug 10, 2016

Oh I see. If file contents are thrown away though, I think at least a warning would come handy, but yeah, I see now, thanks!

emilio added a commit to emilio/servo that referenced this issue Aug 10, 2016

emilio added a commit to emilio/servo that referenced this issue Aug 11, 2016

@ExpHP

This comment has been minimized.

Copy link
Contributor

ExpHP commented Jan 17, 2017

Someone reported a similar experience on users, trying to use [include!("path")] to populate an array.

While it doesn't surprise me that the result is limited to a single expression or statement, it seems like there ought to be an error message?

@est31

This comment has been minimized.

Copy link
Contributor

est31 commented Jan 3, 2018

Still happens, just ran into this today. Braces can be used as a workaround.

{
print("foo");
print("bar");
print("foobar");
}

cc @jseyfried @nrc

@jseyfried jseyfried self-assigned this Jan 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.