Skip to content

Commit be1bb37

Browse files
committed
Include crate names in global names and enforce uniqueness of global names during registration
1 parent b118890 commit be1bb37

File tree

12 files changed

+65
-48
lines changed

12 files changed

+65
-48
lines changed

turbopack/crates/turbo-tasks-backend/tests/task_statistics.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use std::future::IntoFuture;
66

77
use anyhow::Result;
8+
use once_cell::sync::Lazy;
9+
use regex::Regex;
810
use serde_json::json;
911
use turbo_tasks::Vc;
1012
use turbo_tasks_testing::{Registration, register, run_without_cache_check};
@@ -257,16 +259,12 @@ fn stats_json() -> serde_json::Value {
257259

258260
// Global task identifiers can contain the crate name, remove it to simplify test assertions
259261
fn remove_crate(mut json: serde_json::Value) -> serde_json::Value {
262+
static HASH_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^[^:@]+@[^:]+:+").unwrap());
260263
match &mut json {
261264
serde_json::Value::Object(map) => {
262265
let old_map = std::mem::take(map);
263266
for (k, v) in old_map {
264-
map.insert(
265-
k.strip_prefix(concat!(module_path!(), "::"))
266-
.unwrap()
267-
.to_string(),
268-
v,
269-
);
267+
map.insert(HASH_RE.replace(&k, "").into_owned(), v);
270268
}
271269
}
272270
_ => unreachable!("expected object"),

turbopack/crates/turbo-tasks-backend/tests/trace_transient.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use turbo_tasks_testing::{Registration, register, run_without_cache_check};
99
static REGISTRATION: Registration = register!();
1010

1111
const EXPECTED_TRACE: &str = "\
12-
Adder::add_method (read cell of type turbo_tasks::primitives::u64)
12+
Adder::add_method (read cell of type turbo-tasks@turbo_tasks::primitives::u64)
1313
self:
14-
Adder::new (read cell of type trace_transient::Adder)
14+
Adder::new (read cell of type turbo-tasks-backend@trace_transient::Adder)
1515
args:
16-
unknown transient task (read cell of type turbo_tasks::primitives::())
16+
unknown transient task (read cell of type turbo-tasks@turbo_tasks::primitives::())
1717
args:
18-
unknown transient task (read cell of type turbo_tasks::primitives::u16)
19-
unknown transient task (read cell of type turbo_tasks::primitives::u32)";
18+
unknown transient task (read cell of type turbo-tasks@turbo_tasks::primitives::u16)
19+
unknown transient task (read cell of type turbo-tasks@turbo_tasks::primitives::u32)";
2020

2121
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2222
async fn test_trace_transient() {

turbopack/crates/turbo-tasks-macros/src/function_macro.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use quote::quote;
33
use syn::{ItemFn, parse_macro_input, parse_quote};
44
use turbo_tasks_macros_shared::{get_native_function_ident, is_self_used};
55

6-
use crate::func::{
7-
DefinitionContext, FunctionArguments, NativeFn, TurboFn, filter_inline_attributes,
6+
use crate::{
7+
func::{DefinitionContext, FunctionArguments, NativeFn, TurboFn, filter_inline_attributes},
8+
global_name::global_name,
89
};
910

1011
/// This macro generates the virtual function that powers turbo tasks.
@@ -58,7 +59,7 @@ pub fn function(args: TokenStream, input: TokenStream) -> TokenStream {
5859
let function_path_string = ident.to_string();
5960

6061
let native_fn = NativeFn {
61-
function_global_name: quote! { concat!(module_path!(), "::", #function_path_string)},
62+
function_global_name: global_name(&function_path_string),
6263
function_path_string,
6364
function_path: parse_quote! { #inline_function_ident },
6465
is_method: turbo_fn.is_method(),

turbopack/crates/turbo-tasks-macros/src/generic_type_macro.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hash::FxHashSet;
44
use syn::{GenericParam, Lifetime, Type, parse_macro_input, spanned::Spanned, visit_mut::VisitMut};
55
use turbo_tasks_macros_shared::{GenericTypeInput, get_type_ident};
66

7-
use crate::value_macro::value_type_and_register;
7+
use crate::{global_name::global_name, value_macro::value_type_and_register};
88

99
pub fn generic_type(input: TokenStream) -> TokenStream {
1010
let mut input = parse_macro_input!(input as GenericTypeInput);
@@ -44,10 +44,10 @@ pub fn generic_type(input: TokenStream) -> TokenStream {
4444

4545
let (impl_generics, _, where_clause) = input.generics.split_for_impl();
4646

47-
let repr = replace_generics_with_unit(input.generics.params.iter(), &input.ty);
47+
let ty = &input.ty;
48+
let repr = replace_generics_with_unit(input.generics.params.iter(), ty);
4849

49-
let ty = input.ty;
50-
let Some(ident) = get_type_ident(&ty) else {
50+
let Some(ident) = get_type_ident(ty) else {
5151
return quote! {
5252
// An error occurred while parsing the ident.
5353
}
@@ -64,6 +64,7 @@ pub fn generic_type(input: TokenStream) -> TokenStream {
6464
}
6565
}
6666

67+
let name = global_name(quote! {stringify!(#repr) });
6768
let value_type_and_register = value_type_and_register(
6869
&ident,
6970
quote! { #ty },
@@ -75,9 +76,7 @@ pub fn generic_type(input: TokenStream) -> TokenStream {
7576
turbo_tasks::VcCellSharedMode<#ty>
7677
},
7778
quote! {
78-
turbo_tasks::ValueType::new_with_any_serialization::<#repr>(
79-
concat!(module_path!(), "::", stringify!(#repr))
80-
)
79+
turbo_tasks::ValueType::new_with_any_serialization::<#repr>(#name)
8180
},
8281
);
8382

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use proc_macro2::TokenStream;
2+
use quote::quote;
3+
4+
/// Composes an expression that will evaluate to a &'static str of the fully qualified name
5+
///
6+
/// The name is prefixed with the current crate name and module path
7+
pub(crate) fn global_name(local_name: impl quote::ToTokens) -> TokenStream {
8+
let crate_name =
9+
std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "unknown_crate".to_string());
10+
quote! { concat!(#crate_name, "@", module_path!(), "::", #local_name)}
11+
}

turbopack/crates/turbo-tasks-macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod derive;
88
mod func;
99
mod function_macro;
1010
mod generic_type_macro;
11+
mod global_name;
1112
mod primitive_macro;
1213
mod value_impl_macro;
1314
mod value_macro;

turbopack/crates/turbo-tasks-macros/src/primitive_macro.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use quote::quote;
33
use syn::parse_macro_input;
44
use turbo_tasks_macros_shared::{PrimitiveInput, get_type_ident};
55

6-
use crate::value_macro::value_type_and_register;
6+
use crate::{global_name::global_name, value_macro::value_type_and_register};
77

88
pub fn primitive(input: TokenStream) -> TokenStream {
99
let input = parse_macro_input!(input as PrimitiveInput);
@@ -32,7 +32,7 @@ pub fn primitive(input: TokenStream) -> TokenStream {
3232
}
3333
}
3434
};
35-
35+
let name = global_name(quote! {stringify!(#ty) });
3636
let value_type_and_register = value_type_and_register(
3737
&ident,
3838
quote! { #ty },
@@ -44,9 +44,7 @@ pub fn primitive(input: TokenStream) -> TokenStream {
4444
turbo_tasks::VcCellSharedMode<#ty>
4545
},
4646
quote! {
47-
turbo_tasks::ValueType::new_with_any_serialization::<#ty>(
48-
concat!(module_path!(), "::", stringify!(#ty))
49-
)
47+
turbo_tasks::ValueType::new_with_any_serialization::<#ty>(#name)
5048
},
5149
);
5250

turbopack/crates/turbo-tasks-macros/src/value_impl_macro.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ use turbo_tasks_macros_shared::{
1313
get_trait_impl_function_ident, get_type_ident, is_self_used,
1414
};
1515

16-
use crate::func::{
17-
DefinitionContext, FunctionArguments, NativeFn, TurboFn, filter_inline_attributes,
18-
split_function_attributes,
16+
use crate::{
17+
func::{
18+
DefinitionContext, FunctionArguments, NativeFn, TurboFn, filter_inline_attributes,
19+
split_function_attributes,
20+
},
21+
global_name::global_name,
1922
};
2023

2124
struct ValueImplArguments {
@@ -110,7 +113,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
110113
let inline_attrs = filter_inline_attributes(attrs.iter().copied());
111114
let function_path_string = format!("{ty}::{ident}", ty = ty.to_token_stream());
112115
let native_fn = NativeFn {
113-
function_global_name: quote! { concat!(module_path!(), "::", #function_path_string)},
116+
function_global_name: global_name(&function_path_string),
114117
function_path_string,
115118
function_path: parse_quote! { <#ty>::#inline_function_ident },
116119
is_method: turbo_fn.is_method(),
@@ -224,10 +227,13 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
224227
let (inline_signature, inline_block) =
225228
turbo_fn.inline_signature_and_block(block, is_self_used);
226229
let inline_attrs = filter_inline_attributes(attrs.iter().copied());
227-
228230
let native_fn = NativeFn {
229231
// This global name breaks the pattern. It isn't clear if it is intentional
230-
function_global_name: quote! { concat!(module_path!(), "::", stringify!(#ty), "::", stringify!(#trait_path), "::", stringify!(#ident))},
232+
function_global_name: global_name(format!(
233+
"{ty}::{trait_path}::{ident}",
234+
ty = ty.to_token_stream(),
235+
trait_path = trait_path.to_token_stream()
236+
)),
231237
function_path_string: format!(
232238
"<{ty} as {trait_path}>::{ident}",
233239
ty = ty.to_token_stream(),
@@ -287,7 +293,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
287293
});
288294
}
289295
}
290-
296+
let value_name = global_name(quote! {stringify!(#ty_ident)});
291297
quote! {
292298
// Register all the function impls so the ValueType can find them
293299
// This means objects resolve as
@@ -297,13 +303,13 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
297303
// 4.VTableRegistries (requires ValueTypeIds)
298304
turbo_tasks::macro_helpers::inventory_submit!{
299305
turbo_tasks::macro_helpers::CollectableTraitMethods(
300-
concat!(module_path!(), "::", stringify!(#ty_ident)),
306+
#value_name,
301307
|| (<::std::boxed::Box<dyn #trait_path> as turbo_tasks::VcValueTrait>::get_trait_type_id(),
302308
vec![#(#trait_methods)*])
303309
)
304310
}
305311

306-
// These can execute later so they can reference value_types during registration
312+
// These can execute later so they can reference trait_types during registration
307313

308314
turbo_tasks::macro_helpers::inventory_submit!{
309315
turbo_tasks::macro_helpers::CollectableTraitCastFunctions(

turbopack/crates/turbo-tasks-macros/src/value_macro.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use syn::{
1313
};
1414
use turbo_tasks_macros_shared::get_value_type_ident;
1515

16+
use crate::global_name::global_name;
17+
1618
enum IntoMode {
1719
None,
1820
New,
@@ -384,17 +386,14 @@ pub fn value(args: TokenStream, input: TokenStream) -> TokenStream {
384386
});
385387
}
386388

389+
let name = global_name(quote! {stringify!(#ident) });
387390
let new_value_type = match serialization_mode {
388391
SerializationMode::None => quote! {
389-
turbo_tasks::ValueType::new::<#ident>(
390-
concat!(module_path!(), "::", stringify!(#ident))
391-
)
392+
turbo_tasks::ValueType::new::<#ident>(#name)
392393
},
393394
SerializationMode::Auto | SerializationMode::Custom => {
394395
quote! {
395-
turbo_tasks::ValueType::new_with_any_serialization::<#ident>(
396-
concat!(module_path!(), "::", stringify!(#ident))
397-
)
396+
turbo_tasks::ValueType::new_with_any_serialization::<#ident>(#name)
398397
}
399398
}
400399
};

turbopack/crates/turbo-tasks-macros/src/value_trait_macro.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ use turbo_tasks_macros_shared::{
88
ValueTraitArguments, get_trait_default_impl_function_ident, get_trait_type_ident, is_self_used,
99
};
1010

11-
use crate::func::{
12-
DefinitionContext, FunctionArguments, NativeFn, TurboFn, filter_inline_attributes,
13-
split_function_attributes,
11+
use crate::{
12+
func::{
13+
DefinitionContext, FunctionArguments, NativeFn, TurboFn, filter_inline_attributes,
14+
split_function_attributes,
15+
},
16+
global_name::global_name,
1417
};
1518

1619
pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
@@ -177,7 +180,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
177180

178181
let function_path_string = format!("{trait_ident}::{ident}");
179182
let native_function = NativeFn {
180-
function_global_name: quote! { concat!(module_path!(), "::", #function_path_string)},
183+
function_global_name: global_name(&function_path_string),
181184
function_path_string,
182185
function_path: parse_quote! {
183186
<Box<dyn #trait_ident> as #inline_extension_trait_ident>::#inline_function_ident
@@ -266,6 +269,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
266269
extended_supertraits.push(quote!(turbo_tasks::debug::ValueDebug));
267270
}
268271

272+
let trait_name = global_name(quote! {stringify!(#trait_ident)});
269273
let expanded = quote! {
270274
#[must_use]
271275
#(#attrs)*
@@ -280,7 +284,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
280284
turbo_tasks::macro_helpers::Lazy::new(|| {
281285
turbo_tasks::TraitType::new(
282286
stringify!(#trait_ident),
283-
concat!(module_path!(), "::", stringify!(#trait_ident)),
287+
#trait_name,
284288
vec![#(#trait_methods)*])
285289
});
286290

0 commit comments

Comments
 (0)