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

Isolated the expression from private imports #11

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,17 +423,12 @@ pub fn export_tokens_macro_ident(ident: &Ident) -> Ident {
/// If the specified [`Path`] doesn't exist or there isn't a valid `#[export_tokens]` attribute
/// on the item at that path, the returned macro path will be invalid.
pub fn export_tokens_macro_path(item_path: &Path) -> Path {
let Some(last_seg) = item_path.segments.last() else { unreachable!("must have at least one segment") };
let mut leading_segs = item_path
.segments
.iter()
.cloned()
.map(|seg| seg.ident)
.collect::<Vec<_>>()[0..item_path.segments.len() - 1]
.to_vec();
let last_seg = export_tokens_macro_ident(&last_seg.ident);
leading_segs.push(last_seg);
parse_quote!(#(#leading_segs)::*)
Copy link
Contributor Author

@gui1117 gui1117 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a path can start with ::, but this will discard it

let mut macro_path = item_path.clone();
let Some(last_seg) = macro_path.segments.pop()
else { unreachable!("must have at least one segment") };
let last_seg = export_tokens_macro_ident(&last_seg.into_value().ident);
macro_path.segments.push(last_seg.into());
macro_path
}

/// Generates a new unique `#[export_tokens]` macro identifier
Expand Down Expand Up @@ -841,6 +836,11 @@ pub fn import_tokens_attr_internal<T1: Into<TokenStream2>, T2: Into<TokenStream2
*
}

// This is to avoid corrupting the scope with imports below
fn isolated_mm_override_path() -> String {
String::from(#mm_override_path)
}

use #mm_path::__private::*;
use #mm_path::__private::quote::ToTokens;
use #mm_path::mm_core::*;
Expand All @@ -863,7 +863,8 @@ pub fn import_tokens_attr_internal<T1: Into<TokenStream2>, T2: Into<TokenStream2
#path_resolver
let path = path.to_token_stream();
let custom_parsed = custom_parsed.to_token_stream();
let resolved_mm_override_path = match syn::parse2::<syn::Path>(String::from(#mm_override_path).parse().unwrap()) {
let mm_override_tokenstream = isolated_mm_override_path().parse().unwrap();
let resolved_mm_override_path = match syn::parse2::<syn::Path>(mm_override_tokenstream) {
Ok(res) => res,
Err(err) => return err.to_compile_error().into()
};
Expand Down Expand Up @@ -926,6 +927,11 @@ pub fn import_tokens_proc_internal<T1: Into<TokenStream2>, T2: Into<TokenStream2
*
}

// This is to avoid corrupting the scope with imports below
fn isolated_mm_override_path() -> String {
String::from(#mm_override_path)
}

use #mm_path::__private::*;
use #mm_path::__private::quote::ToTokens;

Expand All @@ -948,7 +954,8 @@ pub fn import_tokens_proc_internal<T1: Into<TokenStream2>, T2: Into<TokenStream2
Ok(path) => path,
Err(e) => return e.to_compile_error().into(),
};
let resolved_mm_override_path = match syn::parse2::<syn::Path>(String::from(#mm_override_path).parse().unwrap()) {
let mm_override_tokenstream = isolated_mm_override_path().parse().unwrap();
let resolved_mm_override_path = match syn::parse2::<syn::Path>(mm_override_tokenstream) {
Ok(res) => res,
Err(err) => return err.to_compile_error().into()
};
Expand Down