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

Should include! be recommended? #824

Closed
SimonSapin opened this issue Nov 9, 2014 · 6 comments
Closed

Should include! be recommended? #824

SimonSapin opened this issue Nov 9, 2014 · 6 comments

Comments

@SimonSapin
Copy link
Contributor

Cargo recommends the include! macro to use generated code.

include!(concat!(env!("OUT_DIR"), "/hello.rs"))

However, it doesn’t seem to be documented, and its source comments say

This is generally a bad idea because it's going to behave unhygienically.

So, is include! the way forward, or should we find some other mechanism? Maybe make something like this work? (It currently gives error: unexpected token: concat``)

#[path = concat!(env!("OUT_DIR"), "/hello.rs")]
mod foo;
@alexcrichton
Copy link
Member

This seems more relevant as a Rust bug than a Cargo bug in the sense that this is the only way I know of to do this sort of inclusion today. I'd personally love to have #[path = concat!(...)], but that would require language changes to the attribute syntax and this doesn't seem like a super-motivating example.

I suppose I would say, "I agree", but this isn't entirely Cargo's problem. If we had another solution it would be nice, but we need to develop on in rustc first. The answer may be adding another macro which does behave like mod (perhaps include_mod!) which would solve issues like rust-lang/rust#18810.

Note that the bit about being hygienic I think would mean that the entire output of include! would be unusable unless it specifically listed identifiers that it wanted to be reachable, and it's quite an old macro, so the comments may just be a little dated.

@cuviper
Copy link
Member

cuviper commented Nov 10, 2014

I also noticed that include! causes the target to be rebuilt every time, regardless of whether any of the source files have actually changed. I thought at first this might be conservatism about what the compile-time env! contains, but even a hard-coded include! path does this.

@alexcrichton
Copy link
Member

Due to the lack of other infrastructure in the compiler for doing an equivalent task, I'm going to close this issue.

@alexcrichton
Copy link
Member

@cuviper sorry for taking awhile to get back to you, but you may have been running into #1162

@SimonSapin
Copy link
Contributor Author

Is it possible to add support for #[path = concat!(...)] (rust-lang/rust#18849) later, backward-compatibly?

@alexcrichton
Copy link
Member

Of course!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants