-
Notifications
You must be signed in to change notification settings - Fork 248
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
[Bugfix] Invalid generation of types with >1 generic parameters #1023
Merged
tadeohepperle
merged 3 commits into
master
from
tadeo-hepperle-invalid-generation-types-multiple-generic-parameters
Jun 23, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,9 @@ use frame_metadata::{ | |
v15::{ExtrinsicMetadata, RuntimeMetadataV15}, | ||
RuntimeMetadataPrefixed, | ||
}; | ||
use scale_info::{meta_type, IntoPortable, TypeInfo}; | ||
use scale_info::{meta_type, IntoPortable, PortableRegistry, Registry, TypeInfo}; | ||
use subxt_codegen::{CratePath, DerivesRegistry, RuntimeGenerator, TypeSubstitutes}; | ||
use syn::__private::quote; | ||
|
||
fn generate_runtime_interface_from_metadata(metadata: RuntimeMetadataPrefixed) -> String { | ||
// Generate a runtime interface from the provided metadata. | ||
|
@@ -144,3 +145,75 @@ fn generic_types_overwrite_each_other() { | |
// We do _not_ expect this to exist, since a generic is present on the type: | ||
assert!(!interface.contains("DuplicateType2")); | ||
} | ||
|
||
#[test] | ||
fn more_than_1_generic_parameters_work() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
#[allow(unused)] | ||
#[derive(TypeInfo)] | ||
struct Foo<T, U, V, W> { | ||
a: T, | ||
b: U, | ||
c: V, | ||
d: W, | ||
} | ||
|
||
#[allow(unused)] | ||
#[derive(TypeInfo)] | ||
struct Bar { | ||
p: Foo<u32, u32, u64, u128>, | ||
q: Foo<u8, u8, u8, u8>, | ||
} | ||
|
||
let mut registry = Registry::new(); | ||
registry.register_type(&meta_type::<Bar>()); | ||
let portable_types: PortableRegistry = registry.into(); | ||
|
||
let type_gen = subxt_codegen::TypeGenerator::new( | ||
&portable_types, | ||
"root", | ||
Default::default(), | ||
Default::default(), | ||
CratePath::default(), | ||
false, | ||
); | ||
|
||
let types = type_gen.generate_types_mod().unwrap(); | ||
let generated_mod = quote::quote!( #types); | ||
|
||
let expected_mod = quote::quote! { | ||
pub mod root { | ||
use super::root; | ||
pub mod integration_tests { | ||
use super::root; | ||
pub mod codegen { | ||
use super::root; | ||
pub mod codegen_tests { | ||
use super::root; | ||
pub struct Bar { | ||
pub p: root::integration_tests::codegen::codegen_tests::Foo< | ||
::core::primitive::u32, | ||
::core::primitive::u32, | ||
::core::primitive::u64, | ||
::core::primitive::u128 | ||
>, | ||
pub q: root::integration_tests::codegen::codegen_tests::Foo< | ||
::core::primitive::u8, | ||
::core::primitive::u8, | ||
::core::primitive::u8, | ||
::core::primitive::u8 | ||
>, | ||
} | ||
pub struct Foo<_0, _1, _2, _3> { | ||
pub a: _0, | ||
pub b: _1, | ||
pub c: _2, | ||
pub d: _3, | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
assert_eq!(generated_mod.to_string(), expected_mod.to_string()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hrm, does that compile? :)
It would be better to bring in
quote
in dependency tree as this might break between releases...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could do that, good idea.