-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 expression in an include!
d file is included.
#35560
Comments
This is unrelated to the macros used in the included file, the following exhibits the same problem:
fn print(s: &str) { println!("{}", s); }
fn main() {
include!("helper.rs");
}
print("foo");
print("bar");
print("foobar"); It looks like the issue is rather that Wrapping the contents of Also note that the documentation of
Thus the current behaviour is probably as documented. |
But the contents of |
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! |
Someone reported a similar experience on users, trying to use 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? |
Still happens, just ran into this today. Braces can be used as a workaround. {
print("foo");
print("bar");
print("foobar");
} cc @jseyfried @nrc |
include!
d file is expanded.include!
d file is included.
…rochenkov Warn if include macro fails to include entire file This currently introduces an error, mainly because that was just simpler, and I'm not entirely certain if we can introduce a lint without an RFC and such. This is primarily to get feedback on the approach and overall aim -- in particular, do we think this is helpful? If so, we probably will need lang-team sign off and decide if it should be an error (as currently introduced by this PR), a lint, or a warning. r? @petrochenkov cc rust-lang#35560
The compiler now gives an error if there are multiple expressions in the included file:
(presumably it's a deny-by-default lint and not a hard error for backwards-compatibility reasons.) |
I mean, I personally don't think we should be making things hard errors unless there's a specific reason to, the only benefit is that it slightly simplifies the compiler. I guess it doesn't hurt to have an issue open; but I would rather bring this up in a T-lang meeting and have a decision one way or another than leave this open indefinitely. |
@jyn514 yeah if the decision by the lang team is to keep it a deny by default lint indefinitely, it's okay too. As you've rightly pointed out, errors should be introduced cautiously to previously compiling code. There should be a decision on it, though, because there are possible other resolutions. For example, this works right now and prints fn main() {
macro_rules! my_macro {
() => {
println!("hello 1");
println!("hello 2");
}
}
my_macro!();
} So why can't the fn main() {
macro_rules! my_macro {
() => {
1,2,3 //~ERROR macro expansion ignores token `,` and any following
}
}
let v = [my_macro!()];
println!("{:?}", v);
} |
If you have a file that invokes a macro and you
include!
it, the following happens:main.rs
helper.rs
STR
Expected output
Should print
foo
andbar
, just as if the code would have been inlined.Tested with latest nightly and stable:
The text was updated successfully, but these errors were encountered: