Skip to content

Module path attribute override with nested macros doesnt work #87681

Open
@Veetaha

Description

@Veetaha
Contributor

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

Activity

ehuss

ehuss commented on Aug 1, 2021

@ehuss
Contributor

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.

danobi

danobi commented on Aug 2, 2021

@danobi
Contributor

@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

ehuss commented on Aug 2, 2021

@ehuss
Contributor

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

Mark-Simulacrum commented on Aug 2, 2021

@Mark-Simulacrum
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

petrochenkov commented on Aug 2, 2021

@petrochenkov
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

N3xed commented on Aug 15, 2021

@N3xed
Contributor

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;
added a commit that references this issue on Aug 15, 2021
ehuss

ehuss commented on Aug 15, 2021

@ehuss
Contributor

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

added a commit that references this issue on Aug 19, 2021

Rollup merge of rust-lang#88057 - ehuss:releases-doc-macros, r=Mark-S…

6a81d56
added a commit that references this issue on Aug 22, 2021

Rollup merge of rust-lang#88057 - ehuss:releases-doc-macros, r=Mark-S…

9e8b143

10 remaining items

Loading
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-attributesArea: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ehuss@Nemo157@danobi@jonas-schievink@Mark-Simulacrum

        Issue actions

          Module path attribute override with nested macros doesnt work · Issue #87681 · rust-lang/rust