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

Module path attribute override with nested macros doesnt work #87681

Open
Veetaha opened this issue Aug 1, 2021 · 13 comments
Open

Module path attribute override with nested macros doesnt work #87681

Veetaha opened this issue Aug 1, 2021 · 13 comments
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) C-bug Category: This is a bug.

Comments

@Veetaha
Copy link
Contributor

Veetaha commented Aug 1, 2021

I tried this code:

Repository with the reproduction

I expected to see this happen:

The code should compile without errors, because this feature was advertised in Rust 1.54 announcement and in #83366.

Instead, this happened:

Compile error file not found for module generated was issued

More info in README.md of the reproduction repo

Meta

rustc --version --verbose:

rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-unknown-linux-gnu
release: 1.54.0
LLVM version: 12.0.1

cc @jyn514

@Veetaha Veetaha added the C-bug Category: This is a bug. label Aug 1, 2021
@ehuss
Copy link
Contributor

ehuss commented Aug 1, 2021

I can confirm this doesn't work, and is a bit unfortunate it slipped through (I admit I didn't test it).

From what I can tell, the extended attr=expr expansion happens during lowering to HIR (no, I don't know how that works), but #[path] is needed during expansion before that happens. The check here silently ignores the attribute because the value is not a string (it is a macro expression).

I don't think there is any true support for eager expansion, so I don't think there is any existing support for something like this. At a bare minimum, the #[path] attribute should generate an error if the value is not a string literal.

I think we should maybe update the release post, since it is misleading.

@jonas-schievink jonas-schievink added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Aug 2, 2021
@danobi
Copy link
Contributor

danobi commented Aug 2, 2021

@ehuss do you think adding support for this eager expansion is feasible / welcome? I'd be interested in hacking on it if it's not terribly difficult. I also really want this feature for another project I maintain.

@ehuss
Copy link
Contributor

ehuss commented Aug 2, 2021

Unfortunately I think it might be difficult to support. #[path] would likely need special handling because it is needed during expansion. I am not on the compiler or language teams, so I'm not the one to say definitively.

@Mark-Simulacrum
Copy link
Member

cc @petrochenkov @jyn514 -- I would have expected this to work based on discussions when landing this feature, so I'm a little surprised that it doesn't.

@petrochenkov
Copy link
Contributor

@Mark-Simulacrum

I would have expected this to work based on discussions when landing this feature, so I'm a little surprised that it doesn't.

Internally "mod m; -> mod m { /* contents */ }" effectively works as "#[include_it] mod m; -> mod m { /* contents */ }", i.e. it's an expansion of the primary item m, which is performed before any nested expansions (including expansions inside attributes).

At a bare minimum, the #[path] attribute should generate an error if the value is not a string literal.

+1

@N3xed
Copy link
Contributor

N3xed commented Aug 15, 2021

Note: It doesn't work at all, even if the macros aren't nested. For example #[path = concat!("file.rs")] doesn't work either.

There is also the issue #48250 which is pretty much the same as this. Also the RELEASES.md should be changed to reflect that this doesn't work, it currently states that it does:

You can also use this to include auto-generated modules:

#[path = concat!(env!("OUT_DIR"), "/generated.rs")]
mod generated;

N3xed added a commit to N3xed/esp-idf-sys that referenced this issue Aug 15, 2021
This works around the go to definition feature of
rust-analyzer, which otherwise just goes to the `include!`
line. When `#[path = concat!(env!(...), ...)]` works (issue
rust-lang/rust#87681), we should switch to that.
@ehuss
Copy link
Contributor

ehuss commented Aug 15, 2021

Posted #88057 to update the RELEASES, thanks for catching that!

camsteffen added a commit to camsteffen/rust that referenced this issue Aug 19, 2021
…imulacrum

Update RELEASES to clarify attribute macro values.

As noted in rust-lang#87681, macros do not work with the `#[path]` attribute.  Since the places where macros *can* be used is very limited, I have changed this to just focus on `#[doc]` which is the only attribute where this is really useful.
jackh726 added a commit to jackh726/rust that referenced this issue Aug 22, 2021
…imulacrum

Update RELEASES to clarify attribute macro values.

As noted in rust-lang#87681, macros do not work with the `#[path]` attribute.  Since the places where macros *can* be used is very limited, I have changed this to just focus on `#[doc]` which is the only attribute where this is really useful.
@ehuss
Copy link
Contributor

ehuss commented Sep 6, 2021

Posted #88680 to add errors when attempting to use macros with #[path].

bors added a commit to rust-lang-ci/rust that referenced this issue Sep 26, 2021
…enkov

Validate builtin attributes for macro args.

This adds some validation for `path`, `crate_type`, and `recursion_limit` attributes so that they will now return an error if you attempt to pass a macro into them (such as `#[path = foo!()]`). Previously, the attribute would be completely ignored. These attributes are special because their values need to be known before/during expansion.

cc rust-lang#87681
@malaire
Copy link

malaire commented Dec 4, 2021

Rust 1.54 release notes should be updated to say that this doesn't work. It currently says Read here for more details with here linking to #83366 which says that this is supported syntax.

@ehuss
Copy link
Contributor

ehuss commented Dec 4, 2021

@malaire I have updated the description of #83366 to remove the mentions of #[path].

@Veetaha
Copy link
Contributor Author

Veetaha commented Mar 31, 2022

Should we consider this issue resolved then?

@jyn514
Copy link
Member

jyn514 commented Mar 31, 2022

I think it should work, imo it's a bug that it doesn't.

@Nemo157
Copy link
Member

Nemo157 commented Nov 26, 2023

Current example error message for searchability:

error: malformed `path` attribute input
 --> src/main.rs:1:1
  |
1 | #[path = concat!(env!("OUT_DIR"), "/zbus_xmlgen_output.rs")]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[path = "file"]`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

10 participants