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

Shorter type names in recursive types #33

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
82 changes: 82 additions & 0 deletions typegen/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,3 +1774,85 @@ fn alloc_crate_path_replacement() {
assert_eq!(std_code.to_string(), expected_std_code.to_string());
assert_eq!(no_std_code.to_string(), expected_no_std_code.to_string());
}
#[test]
fn ensure_unique_names_recursion() {
use scale_info::PortableRegistry;

#[allow(unused)]
#[derive(TypeInfo)]
#[scale_info(skip_type_params(A))]
struct Example<A, B> {
id: A,
rest: B,
inner: Vec<Example<A, B>>,
}

let mut registry = Testgen::new()
.with::<Example<String, u32>>()
.with::<Example<u8, u32>>()
.with::<Example<u16, u32>>()
.into_portable_registry();

let sorted_type_paths = |registry: &PortableRegistry| -> Vec<String> {
let mut lines = registry
.types
.iter()
.map(|t| t.ty.path.segments.clone().join("::"))
.filter(|e| !e.is_empty())
.collect::<Vec<String>>();
lines.sort();
lines
};

let e1 = sorted_type_paths(&registry);
assert_eq!(
e1,
vec![
"scale_typegen::tests::Example",
"scale_typegen::tests::Example",
"scale_typegen::tests::Example",
]
);

ensure_unique_type_paths(&mut registry);

let e2 = sorted_type_paths(&registry);
assert_eq!(
e2,
vec![
"scale_typegen::tests::Example1",
"scale_typegen::tests::Example2",
"scale_typegen::tests::Example3",
]
);

let generated = Testgen::new()
.with::<Example<String, u32>>()
.with::<Example<u8, u32>>()
.with::<Example<u16, u32>>()
.try_gen_tests_mod(Default::default(), true)
.unwrap();

let expected = quote! {
pub mod tests {
use super::types;
pub struct Example1<_1> {
pub id: ::std::string::String,
pub rest: _1,
pub inner: ::std::vec::Vec<Example1<_1> >,
}
pub struct Example2<_1> {
pub id: ::core::primitive::u8,
pub rest: _1,
pub inner: ::std::vec::Vec<Example2<_1> >,
}
pub struct Example3<_1> {
pub id: ::core::primitive::u16,
pub rest: _1,
pub inner: ::std::vec::Vec<Example3<_1> >,
}
}
};

assert_eq!(expected.to_string(), generated.to_string())
}
1 change: 1 addition & 0 deletions typegen/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub(super) fn subxt_settings() -> TypeGeneratorSettings {
compact_as_type_path: Some(parse_quote!(::subxt_path::ext::codec::CompactAs)),
compact_type_path: Some(parse_quote!(::subxt_path::ext::codec::Compact)),
alloc_crate_path: Default::default(),
parent_path: std::cell::RefCell::default(),
}
}
/// Derives mirroring the subxt default derives
Expand Down
23 changes: 21 additions & 2 deletions typegen/src/typegen/ir/module_ir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;

use crate::typegen::settings::substitutes::TryIntoSynPath;
use crate::TypeGeneratorSettings;

use super::type_ir::TypeIR;
Expand Down Expand Up @@ -32,8 +33,26 @@ impl ToTokensWithSettings for ModuleIR {
.map(|ir| ir.to_token_stream(settings));
let types = self
.types
.values()
.map(|(_, ir)| ir.to_token_stream(settings))
.iter()
.map(|(path, (_, ir))| {
let parent_path = path.syn_path().map(|mut path| {
// add the root module to the parent_path
let extension = syn::PathSegment {
ident: settings.types_mod_ident.clone(),
arguments: syn::PathArguments::None,
};
let punctuated = {
let mut buf = syn::punctuated::Punctuated::new();
buf.push_value(extension);
buf.push_punct(syn::token::PathSep::default());
buf.extend(path.segments);
buf
};
path.segments = punctuated;
path
});
settings.with_parent_path(parent_path, |settings| ir.to_token_stream(settings))
})
.clone();

tokens.extend(quote! {
Expand Down
2 changes: 1 addition & 1 deletion typegen/src/typegen/ir/type_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl CompositeIR {

impl ToTokensWithSettings for CompositeFieldIR {
fn to_tokens(&self, tokens: &mut TokenStream, settings: &TypeGeneratorSettings) {
let ty_path = &self.type_path.to_syn_type(&settings.alloc_crate_path);
let ty_path = &self.type_path.to_syn_type(settings);
if self.is_boxed {
let alloc_path = &settings.alloc_crate_path;
tokens.extend(quote! { #alloc_path::boxed::Box<#ty_path> })
Expand Down
16 changes: 16 additions & 0 deletions typegen/src/typegen/settings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::RefCell;

use derives::DerivesRegistry;
use proc_macro2::Ident;
use quote::{quote, ToTokens};
Expand Down Expand Up @@ -41,6 +43,8 @@ pub struct TypeGeneratorSettings {
/// `alloc::string::String`, `alloc::vec::Vec` and `alloc::boxed::Box`. The default is `AllocCratePath::Std` which
/// uses the types from the `std` library instead.
pub alloc_crate_path: AllocCratePath,
/// path of the parent type
pub parent_path: RefCell<Option<syn::Path>>,
}

/// Information about how to construct the type paths for types that need allocation, e.g.
Expand Down Expand Up @@ -78,6 +82,7 @@ impl Default for TypeGeneratorSettings {
compact_type_path: None,
insert_codec_attributes: false,
alloc_crate_path: Default::default(),
parent_path: RefCell::default(),
}
}
}
Expand All @@ -88,6 +93,17 @@ impl TypeGeneratorSettings {
Self::default()
}

/// Run function with modified parent_path
pub fn with_parent_path<T>(&self, path: Option<syn::Path>, f: T) -> proc_macro2::TokenStream
where
T: FnOnce(&Self) -> proc_macro2::TokenStream,
{
let old_path = self.parent_path.replace(path);
let result = f(self);
self.parent_path.replace(old_path);
result
}

/// Sets the `type_mod_name` field.
pub fn type_mod_name(mut self, type_mod_name: &str) -> Self {
self.types_mod_ident =
Expand Down
2 changes: 1 addition & 1 deletion typegen/src/typegen/settings/substitutes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn replace_path_params_recursively<I: Borrow<syn::Ident>, P: Borrow<TypePath>>(
};
if let Some(ident) = get_ident_from_type_path(path) {
if let Some((_, replacement)) = params.iter().find(|(i, _)| ident == i.borrow()) {
*ty = replacement.borrow().to_syn_type(&settings.alloc_crate_path);
*ty = replacement.borrow().to_syn_type(settings);
continue;
}
}
Expand Down
46 changes: 35 additions & 11 deletions typegen/src/typegen/type_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum TypePathInner {

impl ToTokensWithSettings for TypePath {
fn to_tokens(&self, tokens: &mut TokenStream, settings: &TypeGeneratorSettings) {
let syn_type = self.to_syn_type(&settings.alloc_crate_path);
let syn_type = self.to_syn_type(settings);
syn_type.to_tokens(tokens)
}
}
Expand All @@ -56,10 +56,10 @@ impl TypePath {
}))
}

pub(crate) fn to_syn_type(&self, alloc_crate_path: &AllocCratePath) -> syn::Type {
pub(crate) fn to_syn_type(&self, settings: &TypeGeneratorSettings) -> syn::Type {
match &self.0 {
TypePathInner::Parameter(ty_param) => syn::Type::Path(parse_quote! { #ty_param }),
TypePathInner::Type(ty) => ty.to_syn_type(alloc_crate_path),
TypePathInner::Type(ty) => ty.to_syn_type(settings),
}
}

Expand Down Expand Up @@ -283,29 +283,53 @@ impl TypePathType {
)
}

fn to_syn_type(&self, alloc_crate_path: &AllocCratePath) -> syn::Type {
fn to_syn_type(&self, settings: &TypeGeneratorSettings) -> syn::Type {
let alloc_crate_path = &settings.alloc_crate_path;
match &self {
TypePathType::Path { path, params } => {
let path = if params.is_empty() {
parse_quote! { #path }
} else {
let params = params.iter().map(|e| e.to_syn_type(alloc_crate_path));
let params = params.iter().map(|e| e.to_syn_type(settings));
let parent_path = settings
.parent_path
.borrow()
.as_ref()
.filter(|x| path == *x)
.map(|x| {
use syn::Path;
let Path {
segments,
leading_colon,
} = x;
let segments = {
let mut punctuated = syn::punctuated::Punctuated::new();
let self_ident = segments.last().expect("this should not happen");
punctuated.push_value(self_ident.clone());
punctuated
};
Path {
segments,
leading_colon: *leading_colon,
}
});
let path = parent_path.as_ref().unwrap_or(path);
parse_quote! { #path< #( #params ),* > }
};
syn::Type::Path(path)
}
TypePathType::Vec { of } => {
let of = of.to_syn_type(alloc_crate_path);
let of = of.to_syn_type(settings);
let type_path = parse_quote! { #alloc_crate_path::vec::Vec<#of> };
syn::Type::Path(type_path)
}
TypePathType::Array { len, of } => {
let of = of.to_syn_type(alloc_crate_path);
let of = of.to_syn_type(settings);
let array = parse_quote! { [#of; #len] };
syn::Type::Array(array)
}
TypePathType::Tuple { elements } => {
let elements = elements.iter().map(|e| e.to_syn_type(alloc_crate_path));
let elements = elements.iter().map(|e| e.to_syn_type(settings));
let tuple = parse_quote! { (#( # elements, )* ) };
syn::Type::Tuple(tuple)
}
Expand All @@ -331,7 +355,7 @@ impl TypePathType {
is_field,
compact_type_path,
} => {
let inner = inner.to_syn_type(alloc_crate_path);
let inner = inner.to_syn_type(settings);
let path = if *is_field {
// compact fields can use the inner compact type directly and be annotated with
// the `compact` attribute e.g. `#[codec(compact)] my_compact_field: u128`
Expand All @@ -346,8 +370,8 @@ impl TypePathType {
bit_store_type,
decoded_bits_type_path,
} => {
let bit_order_type = bit_order_type.to_syn_type(alloc_crate_path);
let bit_store_type = bit_store_type.to_syn_type(alloc_crate_path);
let bit_order_type = bit_order_type.to_syn_type(settings);
let bit_store_type = bit_store_type.to_syn_type(settings);
let type_path =
parse_quote! { #decoded_bits_type_path<#bit_store_type, #bit_order_type> };
syn::Type::Path(type_path)
Expand Down
Loading