Insert $crate into type_id macro expansion

This commit is contained in:
David Tolnay 2021-02-11 22:37:15 -08:00
parent 3403ae8500
commit c28f77e2e4
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 16 additions and 6 deletions

View File

@ -10,6 +10,7 @@ use crate::syntax::{
self, check, mangle, Api, Doc, Enum, ExternFn, ExternType, Impl, Lifetimes, Pair, Signature,
Struct, Trait, Type, TypeAlias, Types,
};
use crate::type_id::Crate;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::mem;
@ -1095,7 +1096,7 @@ fn type_id(name: &Pair) -> TokenStream {
segments.extend(namespace_segments.cloned());
segments.push(Ident::new(&name.cxx.to_string(), Span::call_site()));
let qualified = QualifiedName { segments };
crate::type_id::expand(qualified)
crate::type_id::expand(Crate::Cxx, qualified)
}
fn expand_rust_box(ident: &Ident, types: &Types, explicit_impl: Option<&Impl>) -> TokenStream {

View File

@ -90,5 +90,5 @@ pub fn type_id(input: TokenStream) -> TokenStream {
}
let arg = parse_macro_input!(input as TypeId);
type_id::expand(arg.path).into()
type_id::expand(arg.krate, arg.path).into()
}

View File

@ -1,14 +1,23 @@
use crate::syntax::qualified::QualifiedName;
use proc_macro2::{TokenStream, TokenTree};
use quote::{format_ident, quote};
use quote::{format_ident, quote, ToTokens};
pub enum Crate {
Cxx,
DollarCrate(TokenTree),
}
impl ToTokens for Crate {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Crate::Cxx => tokens.extend(quote!(::cxx)),
Crate::DollarCrate(krate) => krate.to_tokens(tokens),
}
}
}
// "folly::File" => `(f, o, l, l, y, (), F, i, l, e)`
pub fn expand(arg: QualifiedName) -> TokenStream {
pub fn expand(krate: Crate, arg: QualifiedName) -> TokenStream {
let mut ids = Vec::new();
for word in arg.segments {
@ -19,11 +28,11 @@ pub fn expand(arg: QualifiedName) -> TokenStream {
ids.push(match ch {
'A'..='Z' | 'a'..='z' => {
let t = format_ident!("{}", ch);
quote!(::cxx::#t)
quote!(#krate::#t)
}
'0'..='9' | '_' => {
let t = format_ident!("_{}", ch);
quote!(::cxx::#t)
quote!(#krate::#t)
}
_ => quote!([(); #ch as _]),
});