Skip to content

Conversation

Aversefun
Copy link

The tracking issue for this is #130856. This PR begins reorganizing the proc_macro crate as a prerequisite to beginning implementing the main portion of the linked issue.

r? @tgross35

@rustbot
Copy link
Collaborator

rustbot commented Dec 17, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @tgross35 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 17, 2024
@rust-log-analyzer

This comment has been minimized.

@tgross35
Copy link
Contributor

tgross35 commented Dec 17, 2024

This PR may fail because https://github.com/rust-lang/rust/blob/6d9f6ae36ae1299d6126ba40c15191f7aa3b79d8/compiler/rustc_expand/src/proc_macro.rs uses proc_macro::bridge. Mybe bridge should remain a top-level module.

Edit: guess that already happened, see above.

@tgross35
Copy link
Contributor

tgross35 commented Dec 17, 2024

To clarify some of my thoughts here: the reason for this refactor would be to give most of proc_macro (i.e. the things in diagnostic.rs and lib.rs) an interface that abstracts over what the actual backend is - rustc via RPC or the standalone backend. So for now, backend should pub use bridge::{Diagnostic, client::{TokenStream, Span, Symbol}} and then everything else in the crate should use backend::Foo rather than backend::bridge::Foo. Then once there is an actual standalone backend, backend will wrap this instead of just reexporting bridge.

^ That should be captured in the module docs for backend somehow.

For the above error, I think the cleanest way to fix this while keeping refactoring is to reexport the bridge code in lib.rs:

#[doc(hidden)]
pub mod bridge {
    pub use super::backend::bridge::*;
}

@tgross35
Copy link
Contributor

tgross35 commented Dec 17, 2024

After thinking about it a bit further, this naming convention seems like it would be more accurate:

  • "Backend": Everything related to how proc_macro is backed. Parent of everything else on this list.
  • "Client": The proc_macro side of a backend (definition unchanged)
  • "Server": Something abstract that communicates with the client
  • "Bridge": A specific server that communicates with rustc
  • "Standalone": A specific server that doesn't need rustc (does not yet exist)

So, suggestions for how this could be refactored a bit (each mod is an actual file):

// lib.rs

mod backend { // backend/mod.rs
    mod support { // backend/support/mod.rs
        //! All data structures that support backends but aren't interface-specific.
        // None of this should depend on `client`, `server`, or `bridge`.

        mod fxhash; // backend/support/fxhash.rs
        mod arena; // backend/support/arena.rs
        mod buffer; // backend/support/buffer.rs
        mod closure; // backend/support/closure.rs
        mod handle; // backend/support/handle.rs
        mod selfless_reify; // backend/support/selfless_reify.rs
    }

    /// Interfaces and types representing `proc_macro` as a client.
    pub mod client; // backend/client.rs

    /// Abstract interfaces for `proc_macro` servers.
    pub mod server; // backend/server.rs

    /// Serialization between a client and a server.
    pub mod rpc; // backend/rpc.rs

    /// Everything for a bridged backend to `rustc` or `rust-analyzer`.
    pub mod bridge; // backend/bridge.rs

    // Eventually there will be a `mod standalone` here

    mod symbol; // backend/symbol.rs

    pub use support::{fxhash::FxHashMap, arena::Arena, etc};
}

/// Public interfaces for use by the bridge backend
// This one can just be within lib.rs
#[doc(hidden)]
pub mod bridge {
    pub use super::backend::bridge::{
        DelimSpan, Diagnostic, ExpnGlobals, Group,
        Ident, LitKind, Literal, Punct, TokenTree,
    };

    pub use super::backend::{client, server};
}

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 17, 2024
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 17, 2024
@Aversefun
Copy link
Author

Aversefun commented Dec 17, 2024

One more thing that we should come up with a vocab for(not feeling very creative right now) is one of (bridge, standalone).

EDIT: Whoops, wasn't thinking, the name is "servers" 😅

@Aversefun
Copy link
Author

Separate from that, I think that under the server/client modules we should have separate files for bridge v. standalone and they'd be conditionally compiled while implementing the same interface. Thoughts?

@Aversefun
Copy link
Author

That might've been what you were saying, not sure.

@Aversefun
Copy link
Author

Also, thoughts on adding a module common for things that both the server and client may need?

@Aversefun
Copy link
Author

Annnd nevermind. I don't think that's actually necessary.

@rust-log-analyzer

This comment has been minimized.

@Aversefun
Copy link
Author

Sorry for all of the pings, but I'm trying to decide where bridge/mod.rs should go. It contains a good amount of code that appears as though it should be exported, should I just keep it as bridge.rs? Until further notice, that's what I'll do.

@Aversefun
Copy link
Author

Aaand I do need to make a common directory. bridge/mod.rs, the client, and the server all need access to rpc.rs.

@rust-log-analyzer

This comment has been minimized.

@tgross35
Copy link
Contributor

Separate from that, I think that under the server/client modules we should have separate files for bridge v. standalone and they'd be conditionally compiled while implementing the same interface. Thoughts?

We can't have any conditional compilation in the final product. Formerly I thought that using cfg to select a module would be easiest for development, but I'm not really thinking so anymore. Instead, once there is a standalone module, we just need to expose a way to register this (along the lines of what I mentioned here https://rust-lang.zulipchat.com/#narrow/channel/404510-wg-macros/topic/Design.20work.20for.20.60proc_macro.60.20outside.20of.20proc.20macro.20crates/near/489377716).

Also, thoughts on adding a module common for things that both the server and client may need?

The data structure-type things like fxhash.rs, arena.rs etc I would put in backend/support/.

Sorry for all of the pings,

Don't be, happy to work through this :)

but I'm trying to decide where bridge/mod.rs should go. It contains a good amount of code that appears as though it should be exported, should I just keep it as bridge.rs? Until further notice, that's what I'll do.

Just rename this one to backend/bridge.rs. Since everything moves out from under it, it shouldn't need to be a directory anymore.

There will wind up being two modules called bridge: one as an inline module mod bridge {...} in lib.rs, and this one at backend/bridge.rs. The inline module will just reexport specific things from backend::bridge, client, server, etc that are needed by rustc or rust-analyzer. (Specific reexports rather than the whole module or * is an improvement over what we have now, makes the API more clear and avoids exporting more than necessary).

Aaand I do need to make a common directory. bridge/mod.rs, the client, and the server all need access to rpc.rs.

I think this can just be at backend/rpc.rs since it is kind of glue between a client and server.

@tgross35 tgross35 changed the title Begun reorganization of proc_macro crate Begin reorganization of proc_macro crate Dec 17, 2024
@Aversefun
Copy link
Author

Aversefun commented Dec 17, 2024

We can't have any conditional compilation in the final product. Formerly I thought that using cfg to select a module would be easiest for development, but I'm not really thinking so anymore. Instead, once there is a standalone module, we just need to expose a way to register this (along the lines of what I mentioned here rust-lang.zulipchat.com#narrow/channel/404510-wg-macros/topic/Design.20work.20for.20.60proc_macro.60.20outside.20of.20proc.20macro.20crates/near/489377716).

Okay I see, that would make sense. Actually, would we even need a standalone server? Since proc macros are meant to be compiled as a dynamic library, if we add a lint to prevent using proc macros as regular functions, then we should be able to just not have a standalone server as the proc macros would never get actually called.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 17, 2024
@rustbot

This comment has been minimized.

@Aversefun
Copy link
Author

Why does github not have the option to force-push in the online thing 😭

@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 17, 2024
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#20 exporting to docker image format
#20 sending tarball 26.9s done
#20 DONE 39.7s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
To only update this specific test, also pass `--test-args proc-macro/bad-projection.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/proc-macro/bad-projection.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/proc-macro/bad-projection" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- stderr -------------------------------
error[E0277]: the trait bound `(): Project` is not satisfied
##[error]  --> /checkout/tests/ui/proc-macro/bad-projection.rs:14:17
   |
   |
LL | pub fn uwu() -> <() as Project>::Assoc {}
   |                 ^^^^^^^^^^^^^^^^^^^^^^ the trait `Project` is not implemented for `()`
help: this trait has no implementations, consider adding one
  --> /checkout/tests/ui/proc-macro/bad-projection.rs:9:1
   |
LL | trait Project {
LL | trait Project {
   | ^^^^^^^^^^^^^

error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
##[error]  --> /checkout/tests/ui/proc-macro/bad-projection.rs:14:1
   |
LL | pub fn uwu() -> <() as Project>::Assoc {}
   | |
   | expected function that takes 1 argument
   | takes 0 arguments
   | required by a bound introduced by this call
   | required by a bound introduced by this call
   |
note: required by a bound in `ProcMacro::bang`
  --> /rustc/FAKE_PREFIX/library/proc_macro/src/backend/bridge/client.rs:409:5

error[E0277]: the trait bound `(): Project` is not satisfied
##[error]  --> /checkout/tests/ui/proc-macro/bad-projection.rs:14:1
   |
LL | pub fn uwu() -> <() as Project>::Assoc {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Project` is not implemented for `()`
help: this trait has no implementations, consider adding one
  --> /checkout/tests/ui/proc-macro/bad-projection.rs:9:1
   |
LL | trait Project {
LL | trait Project {
   | ^^^^^^^^^^^^^

error[E0277]: the trait bound `(): Project` is not satisfied
##[error]  --> /checkout/tests/ui/proc-macro/bad-projection.rs:14:1
   |
LL | pub fn uwu() -> <() as Project>::Assoc {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Project` is not implemented for `()`
help: this trait has no implementations, consider adding one
  --> /checkout/tests/ui/proc-macro/bad-projection.rs:9:1
   |
LL | trait Project {
LL | trait Project {
   | ^^^^^^^^^^^^^

error[E0277]: the trait bound `(): Project` is not satisfied
##[error]  --> /checkout/tests/ui/proc-macro/bad-projection.rs:14:40
   |
LL | pub fn uwu() -> <() as Project>::Assoc {}
   |                                        ^^ the trait `Project` is not implemented for `()`
help: this trait has no implementations, consider adding one
  --> /checkout/tests/ui/proc-macro/bad-projection.rs:9:1
   |
LL | trait Project {

@tgross35
Copy link
Contributor

We can't have any conditional compilation in the final product. Formerly I thought that using cfg to select a module would be easiest for development, but I'm not really thinking so anymore. Instead, once there is a standalone module, we just need to expose a way to register this (along the lines of what I mentioned here rust-lang.zulipchat.com#narrow/channel/404510-wg-macros/topic/Design.20work.20for.20.60proc_macro.60.20outside.20of.20proc.20macro.20crates/near/489377716).

Okay I see, that would make sense. Actually, would we even need a standalone server? Since proc macros are meant to be compiled as a dynamic library, if we add a lint to prevent using proc macros as regular functions, then we should be able to just not have a standalone server as the proc macros would never get actually called.

I am not sure I follow - the end goal is to make proc_macro useful when not actually built as a proc macro, how does linting against their use help us?

Why does github not have the option to force-push in the online thing 😭

Don't worry about the merge commit warning, I'll ask you to clean up history before merge.

@tgross35
Copy link
Contributor

@rustbot author for the things discussed in #134401 (comment) (just comment @rustbot review when this is ready for another look)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 17, 2024
@tgross35
Copy link
Contributor

tgross35 commented Jan 8, 2025

Hey @AverseABFun, I know you were on the fence about continued macro work but are you interested in finishing this up? Changing this to reflect #134401 (comment) is still needed, I don't think there is too much more needed for that.

@Aversefun
Copy link
Author

Aversefun commented Jan 9, 2025 via email

@alex-semenyuk
Copy link
Member

@AverseABFun
Thanks for your contribution
From wg-triage. Do you have any updates on this PR

@Aversefun
Copy link
Author

Aversefun commented Feb 17, 2025 via email

@apiraino apiraino added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Mar 26, 2025
@alex-semenyuk
Copy link
Member

@Aversefun
From wg-triage. Closed this PR due to inactivity. Feel free to reopen or raised new one. Thanks for your efforts.

@lmmx
Copy link

lmmx commented May 30, 2025

Aw no, looks like I just arrived a few days too late. This PR was mentioned in this video (at 9:18):

The proc macro API is not available to non-proc macro crates so if you want to be able to write unit tests, you need some sort of abstraction layer. There's a tracking issue to make the proc macro API available to non-proc macro crates with a PR that needs someone to adopt it.

Is there a concise TODO list of what is unfinished exactly? From reviewing the above I can see

  1. this comment Dec 17th 2024

After thinking about it a bit further, this naming convention seems like it would be more accurate:

  • "Backend": Everything related to how proc_macro is backed. Parent of everything else on this list.
  • "Client": The proc_macro side of a backend (definition unchanged)
  • "Server": Something abstract that communicates with the client
  • "Bridge": A specific server that communicates with rustc
  • "Standalone": A specific server that doesn't need rustc (does not yet exist)
  • Create a backend/support/ directory for shared data structures
  • Properly separate client, server, and bridge components
  • Set up the module structure to eventually support both bridged and standalone backends
  1. Fix the compilation errors
  • The PR was failing tests because some imports weren't properly updated
  1. Create the abstraction layer
  • Eventually implement a way to switch between the rustc bridge backend and a standalone backend

@tgross35
Copy link
Contributor

tgross35 commented May 30, 2025

@lmmx your description sounds accurate to me! If you are interested, feel free to put up a reorganization PR and request a review from me (either with this one as a base or from scratch). The reorg should be pretty easy, after that I think adding the abstractions / bridge should be straightforward.

Happy to help if you have any questions! We also have a chat for dev-related things at https://rust-lang.zulipchat.com/.

@cyrgani cyrgani added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants