Ensure unique types in codegen (#967)

* Ensure unique types in codegen

* tweak comment

* Test the duplicate type codegen stuff

* appease clippy

* fix import

* add another test for generics being de-duplicated

* cargo fmt

---------

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
James Wilson
2023-05-23 11:54:52 +01:00
committed by GitHub
parent 4444bed627
commit 5960cd2ac8
4 changed files with 188 additions and 3 deletions
+43 -2
View File
@@ -27,7 +27,7 @@ use frame_metadata::{RuntimeMetadata, RuntimeMetadataPrefixed};
use heck::ToSnakeCase as _;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use std::{fs, io::Read, path, string::ToString};
use std::{collections::HashMap, fs, io::Read, path, string::ToString};
use syn::parse_quote;
/// Generates the API for interacting with a Substrate runtime.
@@ -175,15 +175,56 @@ impl RuntimeGenerator {
///
/// Supported versions: v14 and v15.
pub fn new(metadata: RuntimeMetadataPrefixed) -> Self {
let metadata = match metadata.1 {
let mut metadata = match metadata.1 {
RuntimeMetadata::V14(v14) => metadata_v14_to_latest(v14),
RuntimeMetadata::V15(v15) => v15,
_ => panic!("Unsupported metadata version {:?}", metadata.1),
};
Self::ensure_unique_type_paths(&mut metadata);
RuntimeGenerator { metadata }
}
/// Ensure that every unique type we'll be generating or referring to also has a
/// unique path, so that types with matching paths don't end up overwriting each other
/// in the codegen. We ignore any types with generics; Subxt actually endeavours to
/// de-duplicate those into single types with a generic.
fn ensure_unique_type_paths(metadata: &mut RuntimeMetadataV15) {
let mut visited_path_counts = HashMap::<Vec<String>, usize>::new();
for ty in &mut metadata.types.types {
// Ignore types without a path (ie prelude types).
if ty.ty.path.namespace().is_empty() {
continue;
}
let has_valid_type_params = ty.ty.type_params.iter().any(|tp| tp.ty.is_some());
// Ignore types which have generic params that the type generation will use.
// Ordinarily we'd expect that any two types with identical paths must be parameterized
// in order to share the path. However scale-info doesn't understand all forms of generics
// properly I think (eg generics that have associated types that can differ), and so in
// those cases we need to fix the paths for Subxt to generate correct code.
if has_valid_type_params {
continue;
}
// Count how many times we've seen the same path already.
let visited_count = visited_path_counts
.entry(ty.ty.path.segments.clone())
.or_default();
*visited_count += 1;
// alter the type so that if it's been seen more than once, we append a number to
// its name to ensure that every unique type has a unique path, too.
if *visited_count > 1 {
if let Some(name) = ty.ty.path.segments.last_mut() {
*name = format!("{name}{visited_count}");
}
}
}
}
/// Generate the API for interacting with a Substrate runtime.
///
/// # Arguments