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

Use type index for now, and prep 0.4.2 release #24

Merged
merged 1 commit into from
Mar 26, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [0.4.2] - 2024-03-26

- Revert some code simplification in 0.4.1 so that older metadata (built with scale-info versions prior to 2.11.1 and using the `retain()` method) is more likely to work.

# [0.4.1] - 2024-03-22

- Bump `scale-info` to 2.11.1 to fix an issue in `scale_typegen::utils::ensure_unique_type_paths` for cases where a type's index and id do not line up, and simplify code a touch.
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ resolver = "2"
[workspace.package]
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
version = "0.4.1"
version = "0.4.2"
rust-version = "1.70.0"
license = "Apache-2.0 OR GPL-3.0"
repository = "https://github.com/paritytech/scale-typegen"
homepage = "https://www.parity.io/"

[workspace.dependencies]
scale-typegen-description = { version = "0.4.1", path = "description" }
scale-typegen = { version = "0.4.1", path = "typegen" }
scale-typegen-description = { version = "0.4.2", path = "description" }
scale-typegen = { version = "0.4.2", path = "typegen" }

# external dependencies
parity-scale-codec = { version = "3.6.5", features = ["derive"] }
Expand Down
23 changes: 15 additions & 8 deletions typegen/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ pub fn ensure_unique_type_paths(types: &mut PortableRegistry) {
let mut types_with_same_type_path_grouped_by_shape = HashMap::<&[String], Vec<Vec<u32>>>::new();

// First, group types if they are similar (same path, same shape).
for ty in types.types.iter() {
for (ty_idx, ty) in types.types.iter().enumerate() {
// We use the index of the type in the types registry instead of `ty.id`. The two
// _should_ be identical, but prior to `scale-info` 2.11.1 they sometimes weren't
// when `registry.retain()` was used, and so to avoid older metadata files breaking
// things, let's stick to using the index for a while:
let ty_idx = ty_idx as u32;
let ty = &ty.ty;

// Ignore types without a path (i.e prelude types).
if ty.ty.path.namespace().is_empty() {
if ty.path.namespace().is_empty() {
continue;
};

// get groups that share this path already, if any.
let groups_with_same_path = types_with_same_type_path_grouped_by_shape
.entry(&ty.ty.path.segments)
.entry(&ty.path.segments)
.or_default();

// Compare existing groups to check which to add our type ID to.
Expand All @@ -33,17 +40,17 @@ pub fn ensure_unique_type_paths(types: &mut PortableRegistry) {
let other_ty_in_group_idx = group[0]; // all types in group are same shape; just check any one of them.
let other_ty_in_group = types
.resolve(other_ty_in_group_idx)
.expect("type is present; qed;");
if types_equal_extended_to_params(&ty.ty, other_ty_in_group) {
group.push(ty.id);
.expect("type is present (1); qed;");
if types_equal_extended_to_params(ty, other_ty_in_group) {
group.push(ty_idx);
added_to_existing_group = true;
break;
}
}

// We didn't find a matching group, so add it to a new one.
if !added_to_existing_group {
groups_with_same_path.push(vec![ty.id])
groups_with_same_path.push(vec![ty_idx])
}
}

Expand All @@ -60,7 +67,7 @@ pub fn ensure_unique_type_paths(types: &mut PortableRegistry) {
let ty = types
.types
.get_mut(ty_id as usize)
.expect("type is present; qed;");
.expect("type is present (2); qed;");
let name = ty.ty.path.segments.last_mut().expect("This is only empty for builtin types, that are filtered out with namespace().is_empty() above; qed;");
*name = format!("{name}{n}"); // e.g. Header1, Header2, Header3, ...
}
Expand Down
Loading