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

Regression when doc string invokes a macro which is defined within the same module #124535

Closed
danielhenrymantilla opened this issue Apr 29, 2024 · 13 comments · Fixed by #125734
Closed
Assignees
Labels
A-resolve Area: Path resolution C-bug Category: This is a bug. regression-from-stable-to-beta Performance or correctness regression from stable to beta. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue
Milestone

Comments

@danielhenrymantilla
Copy link
Contributor

Code

I tried this code:

mod module {
    #![doc = foo!()]

    macro_rules! foo {() => (
        ""
    )}
}

I expected to see this happen:

Instead, this happened:

error: cannot find macro `foo` in this scope
 --> src/lib.rs:2:14
  |
2 |     #![doc = foo!()]
  |              ^^^ consider moving the definition of `foo` before this call
  |
note: a macro with the same name exists, but it appears later at here
 --> src/lib.rs:4:18
  |
4 |     macro_rules! foo {
  |                  ^^^

Remark

Note that this problem still does not occur at the top-most level.

That is, removing mod module { above makes it work:

#![doc = foo!()]

macro_rules! foo {() => (
    ""
)}

Version it worked on

It most recently worked on: +nightly-2024-04-27

Version with regression

rustc --version --verbose:

+nightly-2024-04-28

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged

@danielhenrymantilla danielhenrymantilla added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Apr 29, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. and removed regression-untriaged Untriaged performance or correctness regression. labels Apr 29, 2024
@lqd

This comment was marked as resolved.

@danielhenrymantilla

This comment was marked as resolved.

@lqd

This comment was marked as resolved.

@bjorn3
Copy link
Member

bjorn3 commented Apr 30, 2024

Why did it work at all? The doc comment is defined earlier in the source order than the macro, so it shouldn't be able to see the macro.

@lqd lqd added regression-from-stable-to-beta Performance or correctness regression from stable to beta. and removed regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels May 1, 2024
@lqd lqd added this to the 1.79.0 milestone May 1, 2024
@lqd
Copy link
Member

lqd commented May 1, 2024

This is from #124382 cc @petrochenkov

@petrochenkov petrochenkov self-assigned this May 1, 2024
@petrochenkov
Copy link
Contributor

The AST visiting order (mutable and immutable) was tweaked in #124382, yes.
Not sure which exactly pass is responsible for the change though.

The new behavior looks correct to me, foo is not in scope at the doc comment's location.
I wonder how widespread this breakage is.

@danielhenrymantilla
Copy link
Contributor Author

danielhenrymantilla commented May 2, 2024

I agree it could be sensible not to try to keep backcompat too much, and "tank" the breakage. Especially since adding a use foo; after the macro definition (as it is already customarily done so that a macro become nameable before its definition) does make the error go away:

  mod module {
    #![doc = foo!()]

    macro_rules! foo {() => (
        ""
    )}
+   use foo;
  }

I am fine doing this for my safer-ffi crate which runs into this; I was just worried about doc strings potentially ending up, eventually, fully unable to refer to macros defined within the module.


I do think something ought to be done w.r.t. the root/top-level lib.rs module, since that one has not regressed, meaning it is currently still possible to have:

#![doc = foo!()]

macro_rules! foo {() => (
    ""
)}

working, which given the current issue, hints at some future regression eventually happening in the future: it could thus warrant a FCW, or a fully-fledged breakage here and now?

@petrochenkov
Copy link
Contributor

Note, that name resolution for inner attributes is not well defined, and that's why non-builtin inner attributes are unstable.

#![my::attribute] // ERROR: custom inner attributes are unstable

For the same reasons it would be reasonable to keep #![key = value!()] unstable as well, but apparently nobody thought about this in time.

The situation with #![key = value!()] is better than with inner attributes in general though, because at least it cannot transform content of the module it's applied to into something different.
So in mod foo { #![key = value!()] /*items*/ } we can resolve value inside mod foo without it going from under our feet.

@petrochenkov
Copy link
Contributor

I think my preference would be fixing the root module case, auditing the implementation for other possible interesting cases, and then running crater to see what code is affected.

@rami3l
Copy link
Member

rami3l commented May 3, 2024

I've encountered this very issue (with rustdoc, but I guess it's the same thing), but the error messages seem a bit different, so it took me quite a while to find this issue here... I'll paste my minimal repro here so that it might help someone after me:

src/lib.rs

mod foo {
  #![doc = foo!()]
  macro_rules! foo { () => { "foo" } }
  #[doc = foo!()] struct Foo;
}

Before

> cargo doc --all-features --document-private-items --no-deps
 Documenting foo v0.1.0 (/Users/rami3l/Downloads/foo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s
   Generated /Users/rami3l/Downloads/foo/target/doc/foo/index.html

> rustdoc --version
rustdoc 1.78.0 (9b00956e5 2024-04-29)

After

> cargo +beta doc --all-features --document-private-items --no-deps
 Documenting foo v0.1.0 (/Users/rami3l/Downloads/foo)
error: cannot find macro `foo` in this scope
 --> src/lib.rs:2:12
  |
2 |   #![doc = foo!()]
  |            ^^^
  |
  = help: have you added the `#[macro_use]` on the module/import?

error: could not document `foo`

> rustdoc +beta --version
rustdoc 1.79.0-beta.2 (4bf9354b9 2024-05-02)

Conclusion

If the use foo; approach is recommended, then the help message could use some improvements here.

@jieyouxu jieyouxu added A-resolve Area: Path resolution S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 13, 2024
@petrochenkov
Copy link
Contributor

Fixed in #125734.

fmease added a commit to fmease/rust that referenced this issue May 29, 2024
ast: Revert a breaking attribute visiting order change

Fixes rust-lang#124535
Fixes rust-lang#125201
@bors bors closed this as completed in fdfffc0 May 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 30, 2024
Rollup merge of rust-lang#125734 - petrochenkov:macinattr, r=wesleywiser

ast: Revert a breaking attribute visiting order change

Fixes rust-lang#124535
Fixes rust-lang#125201
@lqd
Copy link
Member

lqd commented May 30, 2024

Reopening to track beta backport

@lqd lqd reopened this May 30, 2024
@Mark-Simulacrum
Copy link
Member

Closing, backported in #126093.

@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jun 11, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 25, 2024
ast: Standardize visiting order for attributes and node IDs

This should only affect `macro_rules` scopes and order of diagnostics.

Also add a deprecation lint for `macro_rules` called outside of their scope, like in rust-lang#124535.
tiif pushed a commit to tiif/miri that referenced this issue Jun 25, 2024
ast: Standardize visiting order for attributes and node IDs

This should only affect `macro_rules` scopes and order of diagnostics.

Also add a deprecation lint for `macro_rules` called outside of their scope, like in rust-lang/rust#124535.
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Jun 27, 2024
ast: Standardize visiting order for attributes and node IDs

This should only affect `macro_rules` scopes and order of diagnostics.

Also add a deprecation lint for `macro_rules` called outside of their scope, like in rust-lang/rust#124535.
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Jun 28, 2024
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Jun 28, 2024
ast: Standardize visiting order for attributes and node IDs

This should only affect `macro_rules` scopes and order of diagnostics.

Also add a deprecation lint for `macro_rules` called outside of their scope, like in rust-lang/rust#124535.
ogoffart added a commit to slint-ui/document-features that referenced this issue Jul 11, 2024
```
warning: cannot find macro `self_test` in this scope
  --> lib.rs:44:10
   |
44 | #![doc = self_test!(/**
   |          ^^^^^^^^^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #124535 <rust-lang/rust#124535>
   = help: import `macro_rules` with `use` to make it callable above its definition
   = note: `#[warn(out_of_scope_macro_calls)]` on by default

```
estebank added a commit to estebank/rust that referenced this issue Jul 23, 2024
```
warning: cannot find macro `in_root` in the crate root
  --> $DIR/key-value-expansion-scope.rs:1:10
   |
LL | #![doc = in_root!()]
   |          ^^^^^^^ not found in the crate root
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue rust-lang#124535 <rust-lang#124535>
   = help: import `macro_rules` with `use` to make it callable above its definition
   = note: `#[warn(out_of_scope_macro_calls)]` on by default
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-resolve Area: Path resolution C-bug Category: This is a bug. regression-from-stable-to-beta Performance or correctness regression from stable to beta. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants