Skip to content

Commit 39cd181

Browse files
wit-parser: Remove duplicate spans from UnresolvedPackage (#2441)
* store spans directly on WIT items instead of parallel vecs encapsulate world item span adjustment simplify highlighting update other crates to build/test correctly * use simple custom serializer to maintain existing json representation * use u32::MAX sentinal instead of Unknown enum variant
1 parent 9b285cc commit 39cd181

23 files changed

Lines changed: 542 additions & 557 deletions

File tree

crates/wit-component/src/dummy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn dummy_module(resolve: &Resolve, world: WorldId, mangling: ManglingAndAbi)
2222
push_imported_type_intrinsics(&mut wat, resolve, Some(name), *ty, mangling);
2323
}
2424
}
25-
WorldItem::Type(id) => {
25+
WorldItem::Type { id, .. } => {
2626
push_imported_type_intrinsics(&mut wat, resolve, None, *id, mangling);
2727
}
2828
}
@@ -47,7 +47,7 @@ pub fn dummy_module(resolve: &Resolve, world: WorldId, mangling: ManglingAndAbi)
4747
push_exported_type_intrinsics(&mut wat, resolve, Some(name), *ty, mangling);
4848
}
4949
}
50-
WorldItem::Type(_) => {}
50+
WorldItem::Type { .. } => {}
5151
}
5252
}
5353

@@ -64,7 +64,7 @@ pub fn dummy_module(resolve: &Resolve, world: WorldId, mangling: ManglingAndAbi)
6464
push_exported_resource_functions(&mut wat, resolve, name, *ty, mangling);
6565
}
6666
}
67-
WorldItem::Type(_) => {}
67+
WorldItem::Type { .. } => {}
6868
}
6969
}
7070

crates/wit-component/src/encoding.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@ impl<'a> EncodingState<'a> {
504504
// FIXME: ideally this would use the liveness analysis from
505505
// world-building to only encode live types, not all type in a world.
506506
for (_name, item) in world.imports.iter() {
507-
if let WorldItem::Type(ty) = item {
507+
if let WorldItem::Type { id, .. } = item {
508508
self.root_import_type_encoder(None)
509-
.encode_valtype(resolve, &Type::Id(*ty))?;
509+
.encode_valtype(resolve, &Type::Id(*id))?;
510510
}
511511
}
512512

@@ -577,7 +577,7 @@ impl<'a> EncodingState<'a> {
577577
for (name, item) in resolve.worlds[world].imports.iter() {
578578
let func = match item {
579579
WorldItem::Function(f) => f,
580-
WorldItem::Interface { .. } | WorldItem::Type(_) => continue,
580+
WorldItem::Interface { .. } | WorldItem::Type { .. } => continue,
581581
};
582582
let name = resolve.name_world_key(name);
583583
if !(info
@@ -752,7 +752,7 @@ impl<'a> EncodingState<'a> {
752752
core_names,
753753
)?;
754754
}
755-
WorldItem::Type(_) => unreachable!(),
755+
WorldItem::Type { .. } => unreachable!(),
756756
}
757757
}
758758

@@ -2932,7 +2932,7 @@ impl<'a> Shims<'a> {
29322932
result: wit_result,
29332933
docs: Default::default(),
29342934
stability: Stability::Unknown,
2935-
span: None,
2935+
span: Default::default(),
29362936
},
29372937
if async_ {
29382938
AbiVariant::GuestImportAsync
@@ -3030,7 +3030,7 @@ fn task_return_options_and_type(
30303030
result: None,
30313031
docs: Default::default(),
30323032
stability: Stability::Unknown,
3033-
span: None,
3033+
span: Default::default(),
30343034
};
30353035
let abi = AbiVariant::GuestImport;
30363036
let options = RequiredOptions::for_import(resolve, &func_tmp, abi);

crates/wit-component/src/encoding/wit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ pub fn encode_world(resolve: &Resolve, world_id: WorldId) -> Result<ComponentTyp
8686
let idx = component.encode_func_type(resolve, f)?;
8787
ComponentTypeRef::Func(idx)
8888
}
89-
WorldItem::Type(t) => {
89+
WorldItem::Type { id, .. } => {
9090
component.interface = None;
9191
component.import_types = true;
92-
component.encode_valtype(resolve, &Type::Id(*t))?;
92+
component.encode_valtype(resolve, &Type::Id(*id))?;
9393
component.import_types = false;
9494
continue;
9595
}
@@ -111,7 +111,7 @@ pub fn encode_world(resolve: &Resolve, world_id: WorldId) -> Result<ComponentTyp
111111
let idx = component.encode_func_type(resolve, f)?;
112112
ComponentTypeRef::Func(idx)
113113
}
114-
WorldItem::Type(_) => unreachable!(),
114+
WorldItem::Type { .. } => unreachable!(),
115115
};
116116
component.outer.export(&name, ty);
117117
}

crates/wit-component/src/encoding/world.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a> ComponentWorld<'a> {
110110
WorldItem::Interface { id, .. } => {
111111
resolve.interfaces[*id].functions.is_empty()
112112
}
113-
WorldItem::Type(_) => true,
113+
WorldItem::Type { .. } => true,
114114
})
115115
};
116116
if no_required_by_import() && no_required_exports() && library_info.is_none() {
@@ -251,11 +251,11 @@ impl<'a> ComponentWorld<'a> {
251251
let name = resolve.name_world_key(name);
252252
log::trace!("register import `{name}`");
253253
let import_map_key = match item {
254-
WorldItem::Function(_) | WorldItem::Type(_) => None,
254+
WorldItem::Function(_) | WorldItem::Type { .. } => None,
255255
WorldItem::Interface { .. } => Some(name),
256256
};
257257
let interface_id = match item {
258-
WorldItem::Function(_) | WorldItem::Type(_) => None,
258+
WorldItem::Function(_) | WorldItem::Type { .. } => None,
259259
WorldItem::Interface { id, .. } => Some(*id),
260260
};
261261
let interface = import_map
@@ -269,8 +269,8 @@ impl<'a> ComponentWorld<'a> {
269269
WorldItem::Function(func) => {
270270
interface.add_func(required, resolve, func);
271271
}
272-
WorldItem::Type(ty) => {
273-
interface.add_type(required, resolve, *ty);
272+
WorldItem::Type { id, .. } => {
273+
interface.add_type(required, resolve, *id);
274274
}
275275
WorldItem::Interface { id, .. } => {
276276
for (_name, ty) in resolve.interfaces[*id].types.iter() {
@@ -317,7 +317,7 @@ impl<'a> ComponentWorld<'a> {
317317
log::trace!("add live world export `{}`", resolve.name_world_key(name));
318318
let id = match item {
319319
WorldItem::Interface { id, .. } => id,
320-
WorldItem::Function(_) | WorldItem::Type(_) => {
320+
WorldItem::Function(_) | WorldItem::Type { .. } => {
321321
live.add_world_item(resolve, item);
322322
continue;
323323
}
@@ -359,7 +359,7 @@ impl<'a> ComponentWorld<'a> {
359359
// encoded and therefore unconditionally live here. Once encoding is
360360
// based on conditionally-live things then this should be removed.
361361
for (_, item) in world.imports.iter() {
362-
if let WorldItem::Type(id) = item {
362+
if let WorldItem::Type { id, .. } = item {
363363
live.add_type_id(resolve, *id);
364364
}
365365
}
@@ -450,7 +450,7 @@ impl<'a> ComponentWorld<'a> {
450450
let id = match item {
451451
WorldItem::Function(_) => continue,
452452
WorldItem::Interface { id, .. } => *id,
453-
WorldItem::Type(_) => unreachable!(),
453+
WorldItem::Type { .. } => unreachable!(),
454454
};
455455
let mut set = HashSet::new();
456456

crates/wit-component/src/metadata.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ impl Default for Bindgen {
8989
imports: Default::default(),
9090
exports: Default::default(),
9191
includes: Default::default(),
92-
include_names: Default::default(),
9392
package: Some(package),
9493
stability: Default::default(),
95-
span: None,
94+
span: Default::default(),
9695
});
9796
resolve.packages[package]
9897
.worlds
@@ -170,7 +169,7 @@ impl EncodingMap {
170169
self.encodings.insert(key, encoding);
171170
}
172171
}
173-
WorldItem::Type(_) => {}
172+
WorldItem::Type { .. } => {}
174173
}
175174
}
176175
}
@@ -411,7 +410,7 @@ impl Bindgen {
411410
.resolve
412411
.merge(resolve)
413412
.context("failed to merge WIT package sets together")?;
414-
let world = remap.map_world(world, None)?;
413+
let world = remap.map_world(world, Default::default())?;
415414
let exports = self.resolve.worlds[world].exports.keys().cloned().collect();
416415
self.resolve
417416
.merge_worlds(world, self.world, &mut CloneMaps::default())

crates/wit-component/src/printing.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ impl<O: Output> WitPrinter<O> {
402402
let mut function_imports_to_print = Vec::new();
403403
for (name, import) in world.imports.iter() {
404404
match import {
405-
WorldItem::Type(t) => match name {
406-
WorldKey::Name(s) => types.push((s.as_str(), *t)),
405+
WorldItem::Type { id, .. } => match name {
406+
WorldKey::Name(s) => types.push((s.as_str(), *id)),
407407
WorldKey::Interface(_) => unreachable!(),
408408
},
409409
_ => {
@@ -458,7 +458,7 @@ impl<O: Output> WitPrinter<O> {
458458
WorldItem::Interface { id, .. } => &resolve.interfaces[*id].docs,
459459
WorldItem::Function(f) => &f.docs,
460460
// Types are handled separately
461-
WorldItem::Type(_) => unreachable!(),
461+
WorldItem::Type { .. } => unreachable!(),
462462
});
463463
}
464464

@@ -484,7 +484,7 @@ impl<O: Output> WitPrinter<O> {
484484
self.output.semicolon();
485485
}
486486
// Types are handled separately
487-
WorldItem::Type(_) => unreachable!(),
487+
WorldItem::Type { .. } => unreachable!(),
488488
}
489489
}
490490
WorldKey::Interface(id) => {

crates/wit-component/src/validation.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,7 @@ impl ExportMap {
14931493
WorldItem::Function(f) => {
14941494
require_world_func(&f.name)?;
14951495
}
1496-
WorldItem::Type(_) => unreachable!(),
1496+
WorldItem::Type { .. } => unreachable!(),
14971497
}
14981498
}
14991499

@@ -1978,7 +1978,7 @@ impl Standard {
19781978
let id = match &world.exports[export] {
19791979
WorldItem::Interface { id, .. } => *id,
19801980
WorldItem::Function(_) => continue,
1981-
WorldItem::Type(_) => unreachable!(),
1981+
WorldItem::Type { .. } => unreachable!(),
19821982
};
19831983
let remaining = match export {
19841984
WorldKey::Name(name) => export_name.strip_prefix(name),
@@ -2449,7 +2449,7 @@ impl NameMangling for Legacy {
24492449
}
24502450
}
24512451

2452-
WorldItem::Type(_) => unreachable!(),
2452+
WorldItem::Type { .. } => unreachable!(),
24532453
}
24542454
}
24552455

@@ -2468,7 +2468,7 @@ impl NameMangling for Legacy {
24682468
let id = match &world.exports[name] {
24692469
WorldItem::Interface { id, .. } => *id,
24702470
WorldItem::Function(_) => continue,
2471-
WorldItem::Type(_) => unreachable!(),
2471+
WorldItem::Type { .. } => unreachable!(),
24722472
};
24732473
let name = resolve.name_world_key(name);
24742474
let resource = match export_name
@@ -2618,9 +2618,9 @@ fn resource_test_for_world<'a>(
26182618
) -> impl Fn(&str) -> Option<TypeId> + 'a {
26192619
let world = &resolve.worlds[id];
26202620
move |name: &str| match world.imports.get(&WorldKey::Name(name.to_string()))? {
2621-
WorldItem::Type(r) => {
2622-
if matches!(resolve.types[*r].kind, TypeDefKind::Resource) {
2623-
Some(*r)
2621+
WorldItem::Type { id, .. } => {
2622+
if matches!(resolve.types[*id].kind, TypeDefKind::Resource) {
2623+
Some(*id)
26242624
} else {
26252625
None
26262626
}

crates/wit-dylib/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl Adapter {
305305
self.collect_payload_data_for_func(resolve, Some(key), func, "", data);
306306
}
307307
}
308-
WorldItem::Type(_) => {}
308+
WorldItem::Type { .. } => {}
309309
WorldItem::Function(func) => {
310310
self.collect_payload_data_for_func(resolve, None, func, "", data);
311311
}
@@ -325,7 +325,7 @@ impl Adapter {
325325
);
326326
}
327327
}
328-
WorldItem::Type(_) => unreachable!(),
328+
WorldItem::Type { .. } => unreachable!(),
329329
WorldItem::Function(func) => {
330330
self.collect_payload_data_for_func(resolve, None, func, "[export]", data);
331331
}
@@ -355,8 +355,8 @@ impl Adapter {
355355
self.add_imported_type_intrinsics(resolve, Some(interface), *ty);
356356
}
357357
}
358-
WorldItem::Type(ty) => {
359-
self.add_imported_type_intrinsics(resolve, None, *ty);
358+
WorldItem::Type { id, .. } => {
359+
self.add_imported_type_intrinsics(resolve, None, *id);
360360
}
361361
WorldItem::Function(func) => {
362362
self.add_imported_func(resolve, None, func, &mut ret);
@@ -384,7 +384,7 @@ impl Adapter {
384384
);
385385
}
386386
}
387-
WorldItem::Type(_) => unreachable!(),
387+
WorldItem::Type { .. } => unreachable!(),
388388
}
389389
}
390390

@@ -596,7 +596,7 @@ impl Adapter {
596596
match export {
597597
WorldItem::Function(func) => import_types.add_func(resolve, func),
598598
WorldItem::Interface { .. } => {}
599-
WorldItem::Type(_) => unreachable!(),
599+
WorldItem::Type { .. } => unreachable!(),
600600
}
601601
}
602602
for ty in import_types.iter() {
@@ -1385,7 +1385,7 @@ fn imported_types_used_by_exported_interfaces(resolve: &Resolve, world: WorldId)
13851385
exported_interfaces.insert(*id);
13861386
live_export_types.add_interface(resolve, *id)
13871387
}
1388-
WorldItem::Type(_) => unreachable!(),
1388+
WorldItem::Type { .. } => unreachable!(),
13891389
}
13901390
}
13911391

0 commit comments

Comments
 (0)