From a22fba110d6490829b2543ffbe4841c9c2f153ac Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 15 Nov 2024 06:09:40 -0800 Subject: [PATCH] Don't use `MutableKeys` to set the new world name. The [`MutableKeys` trait] requires that the new key have the same hash value as the old key. Since that's not true when we're changing a world name, rebuild a new `IndexMap` instead, to avoid perterbing the key order. [`MutableKeys` trait]: https://docs.rs/indexmap/latest/indexmap/map/trait.MutableKeys.html --- src/bindings.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/bindings.rs b/src/bindings.rs index 5ebea30..90c217e 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -8,7 +8,7 @@ use std::{ use anyhow::{bail, Context, Result}; use cargo_component_core::registry::DecodedDependency; use heck::ToKebabCase; -use indexmap::{map::MutableKeys, IndexMap, IndexSet}; +use indexmap::{IndexMap, IndexSet}; use semver::Version; use wasm_pkg_client::PackageRef; use wit_bindgen_core::Files; @@ -193,9 +193,19 @@ impl<'a> BindingsGenerator<'a> { pkg.name.namespace = id.namespace().to_string(); pkg.name.name = id.name().to_string(); - // Update the world name in the `pkg.worlds` map too. - let (_index, key, _value) = pkg.worlds.get_full_mut2(&old_name).unwrap(); - *key = id.name().to_string(); + // Update the world name in the `pkg.worlds` map too. Don't use + // `MutableKeys` because the new world name may not have the same + // hash as the old world name. + let mut new_worlds = IndexMap::new(); + for (name, world) in pkg.worlds.iter() { + if name == &old_name { + new_worlds.insert(id.name().to_string(), *world); + } else { + new_worlds.insert(name.clone(), *world); + } + } + assert_eq!(pkg.worlds.len(), new_worlds.len()); + pkg.worlds = new_worlds; let source = merged .merge(resolve)