-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
break rustc_expand-rustc_middle dependency #161263
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "rustc_expand_queries" | ||
| version = "0.0.0" | ||
| edition = "2024" | ||
| build = false | ||
|
|
||
| [lib] | ||
| doctest = false | ||
|
|
||
| [dependencies] | ||
| # tidy-alphabetical-start | ||
| rustc_ast = { path = "../rustc_ast" } | ||
| rustc_expand = { path = "../rustc_expand" } | ||
| rustc_middle = { path = "../rustc_middle" } | ||
| # We must use the proc_macro version that we will compile proc-macros against, | ||
| # not the one from our own sysroot. | ||
| rustc_proc_macro = { path = "../rustc_proc_macro" } | ||
| rustc_span = { path = "../rustc_span" } | ||
| scoped-tls = "1.0" | ||
| # tidy-alphabetical-end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| use rustc_ast::tokenstream::TokenStream; | ||
| use rustc_expand::base::ExtCtxt; | ||
| use rustc_middle::ty::{TyCtxt, tls}; | ||
| use rustc_proc_macro as pm; | ||
| use rustc_span::LocalExpnId; | ||
|
|
||
| type DeriveClient = pm::bridge::client::Client; | ||
|
|
||
| /// Stores the context necessary to expand a derive proc macro via a query. | ||
| struct QueryDeriveExpandCtx { | ||
| /// Type-erased version of `&mut ExtCtxt` | ||
| expansion_ctx: *mut (), | ||
| client: DeriveClient, | ||
| } | ||
|
|
||
| impl QueryDeriveExpandCtx { | ||
| /// Store the extension context and the client into the thread local value. | ||
| /// It will be accessible via the `with` method while `f` is active. | ||
| fn enter<F, R>(ecx: &mut ExtCtxt<'_>, client: DeriveClient, f: F) -> R | ||
| where | ||
| F: FnOnce() -> R, | ||
| { | ||
| // We need erasure to get rid of the lifetime | ||
| let ctx = Self { expansion_ctx: ecx as *mut _ as *mut (), client }; | ||
| DERIVE_EXPAND_CTX.set(&ctx, f) | ||
| } | ||
|
|
||
| /// Accesses the thread local value of the derive expansion context. | ||
| /// Must be called while the `enter` function is active. | ||
| fn with<F, R>(f: F) -> R | ||
| where | ||
| F: for<'a, 'b> FnOnce(&'b mut ExtCtxt<'a>, DeriveClient) -> R, | ||
| { | ||
| DERIVE_EXPAND_CTX.with(|ctx| { | ||
| let ectx = { | ||
| let casted = ctx.expansion_ctx.cast::<ExtCtxt<'_>>(); | ||
| // SAFETY: We can only get the value from `with` while the `enter` function | ||
| // is active (on the callstack), and that function's signature ensures that the | ||
| // lifetime is valid. | ||
| // If `with` is called at some other time, it will panic due to usage of | ||
| // `scoped_tls::with`. | ||
| unsafe { casted.as_mut().unwrap() } | ||
| }; | ||
|
|
||
| f(ectx, ctx.client) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // When we invoke a query to expand a derive proc macro, we need to provide it with the expansion | ||
| // context and derive Client. We do that using a thread-local. | ||
| scoped_tls::scoped_thread_local!(static DERIVE_EXPAND_CTX: QueryDeriveExpandCtx); | ||
|
|
||
| pub(crate) fn expand_derive_macro_cached( | ||
| invoc_id: LocalExpnId, | ||
| input: TokenStream, | ||
| ecx: &mut ExtCtxt<'_>, | ||
| client: DeriveClient, | ||
| ) -> Result<TokenStream, ()> { | ||
| tls::with(|tcx| { | ||
| let input = &*tcx.arena.alloc(input); | ||
| let key: (LocalExpnId, &TokenStream) = (invoc_id, input); | ||
|
|
||
| QueryDeriveExpandCtx::enter(ecx, client, move || tcx.derive_macro_expansion(key).cloned()) | ||
| }) | ||
| } | ||
|
|
||
| /// Provide a query for computing the output of a derive macro. | ||
| pub(crate) fn derive_macro_expansion<'tcx>( | ||
| tcx: TyCtxt<'tcx>, | ||
| key: (LocalExpnId, &'tcx TokenStream), | ||
| ) -> Result<&'tcx TokenStream, ()> { | ||
| let (invoc_id, input) = key; | ||
|
|
||
| // Make sure that we invalidate the query when the crate defining the proc macro changes | ||
| let _ = tcx.crate_hash(invoc_id.expn_data().macro_def_id.unwrap().krate); | ||
|
|
||
| QueryDeriveExpandCtx::with(|ecx, client| { | ||
| rustc_expand::proc_macro::expand_derive_macro(invoc_id, input.clone(), ecx, client) | ||
| .map(|ts| &*tcx.arena.alloc(ts)) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #![allow(internal_features, reason = "proc macro internals")] | ||
| #![feature(proc_macro_internals)] | ||
|
|
||
| mod derive; | ||
|
|
||
| pub fn setup_callbacks() { | ||
| rustc_expand::proc_macro::EXPAND_DERIVE_MACRO_CACHED | ||
| .swap(&(derive::expand_derive_macro_cached as _)); | ||
| } | ||
|
|
||
| pub fn provide(providers: &mut rustc_middle::query::Providers) { | ||
| providers.derive_macro_expansion = derive::derive_macro_expansion; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.