Skip to content

Commit d5bc2d0

Browse files
authored
refactor(compiler): remove duplicate named type index (#613)
1 parent 9acc2db commit d5bc2d0

6 files changed

Lines changed: 20 additions & 31 deletions

File tree

‎crates/plotnik-lib/src/compiler/analyze/result.rs‎

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ impl PublicResultGroup {
379379
pub(crate) struct ResultModel {
380380
reachable_defs: DefinitionReachability,
381381
type_layout: ResultTypeLayout,
382-
named_types: BTreeMap<TypeId, Symbol>,
383382
type_name_bindings: Vec<TypeNameBinding>,
384383
entry_point_items: Vec<ResultItem>,
385384
capture_layout: CaptureLayout,
@@ -421,10 +420,9 @@ impl ResultModel {
421420
&collected_types.builtins,
422421
collected_types.value_types,
423422
);
424-
let named_types: BTreeMap<TypeId, Symbol> = types.iter_named_types().collect();
425-
let mut type_name_bindings = named_types
426-
.iter()
427-
.map(|(&type_id, &name)| TypeNameBinding { name, type_id })
423+
let mut type_name_bindings = types
424+
.iter_named_types()
425+
.map(|(type_id, name)| TypeNameBinding { name, type_id })
428426
.collect::<Vec<_>>();
429427
for def_id in reachable_defs.iter() {
430428
let Some(body) = types.expect_def_output(def_id).value() else {
@@ -437,8 +435,8 @@ impl ResultModel {
437435
}
438436
type_name_bindings.sort_by_key(|binding| (binding.type_id, binding.name));
439437
type_name_bindings.dedup();
440-
let entry_point_items = ItemCollector::new(types, definitions, &named_types)
441-
.collect(artifacts.iter_entry_point_outputs());
438+
let entry_point_items =
439+
ItemCollector::new(types, definitions).collect(artifacts.iter_entry_point_outputs());
442440
let mut public_result_groups = BTreeMap::new();
443441
for item in entry_point_items
444442
.iter()
@@ -451,7 +449,7 @@ impl ResultModel {
451449
);
452450
assert!(previous.is_none(), "public result item names are unique");
453451
}
454-
for (&type_id, &name) in &named_types {
452+
for (type_id, name) in types.iter_named_types() {
455453
let Some(group) = public_result_groups.get_mut(&name) else {
456454
continue;
457455
};
@@ -466,7 +464,6 @@ impl ResultModel {
466464
Ok(Self {
467465
reachable_defs,
468466
type_layout,
469-
named_types,
470467
type_name_bindings,
471468
entry_point_items,
472469
capture_layout,
@@ -509,10 +506,6 @@ impl ResultModel {
509506
);
510507
group
511508
}
512-
513-
pub(crate) fn type_name_of(&self, type_id: TypeId) -> Option<Symbol> {
514-
self.named_types.get(&type_id).copied()
515-
}
516509
}
517510

518511
impl<'a> ResultSchema<'a> {
@@ -534,10 +527,6 @@ impl<'a> ResultSchema<'a> {
534527
&self.model.type_layout
535528
}
536529

537-
pub(crate) fn type_name_of(&self, type_id: TypeId) -> Option<Symbol> {
538-
self.model.type_name_of(type_id)
539-
}
540-
541530
pub(crate) fn iter_type_name_bindings(&self) -> impl Iterator<Item = TypeNameBinding> + '_ {
542531
self.model.type_name_bindings.iter().copied()
543532
}
@@ -572,22 +561,16 @@ impl<'a> ResultSchema<'a> {
572561
struct ItemCollector<'a> {
573562
types: &'a TypeAnalysis,
574563
definitions: &'a DefinitionGraph,
575-
named_types: &'a BTreeMap<TypeId, Symbol>,
576564
declared_names: HashSet<Symbol>,
577565
walked_types: HashSet<TypeId>,
578566
items: Vec<ResultItem>,
579567
}
580568

581569
impl<'a> ItemCollector<'a> {
582-
fn new(
583-
types: &'a TypeAnalysis,
584-
definitions: &'a DefinitionGraph,
585-
named_types: &'a BTreeMap<TypeId, Symbol>,
586-
) -> Self {
570+
fn new(types: &'a TypeAnalysis, definitions: &'a DefinitionGraph) -> Self {
587571
Self {
588572
types,
589573
definitions,
590-
named_types,
591574
declared_names: HashSet::new(),
592575
walked_types: HashSet::new(),
593576
items: Vec::new(),
@@ -655,14 +638,14 @@ impl<'a> ItemCollector<'a> {
655638
fn collect_position(&mut self, ty: TypeId) {
656639
match self.types.expect_type_shape(ty) {
657640
TypeShape::Record(_) | TypeShape::Variant(_) => {
658-
let name = *self
659-
.named_types
660-
.get(&ty)
641+
let name = self
642+
.types
643+
.type_name_of(ty)
661644
.expect("naming pass names every non-payload composite");
662645
self.add_item(name, ty);
663646
}
664647
TypeShape::List { .. } | TypeShape::Option(_) => {
665-
let Some(&name) = self.named_types.get(&ty) else {
648+
let Some(name) = self.types.type_name_of(ty) else {
666649
self.walk(ty);
667650
return;
668651
};

‎crates/plotnik-lib/src/compiler/analyze/types/type_analysis.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,10 @@ impl TypeAnalysis {
556556
})
557557
}
558558

559+
pub(crate) fn type_name_of(&self, type_id: TypeId) -> Option<Symbol> {
560+
self.named_types.get(&type_id).copied()
561+
}
562+
559563
/// Iterate generated and explicitly named structural bodies in `TypeId`
560564
/// order. Definition declarations are exposed separately through their
561565
/// `DefId` and output body.

‎crates/plotnik-lib/src/compiler/emit/decode.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ impl DecodePlanBuilder<'_, '_> {
210210
TypeShape::Record(_) | TypeShape::Variant(_) => DecodeValue::Nested {
211211
item: self
212212
.schema
213+
.types
213214
.type_name_of(ty)
214215
.expect("naming pass names every non-payload composite"),
215216
source_type: ty,
@@ -244,7 +245,7 @@ impl DecodePlanBuilder<'_, '_> {
244245
if !seen.insert(ty) {
245246
return false;
246247
}
247-
if let Some(name) = self.schema.type_name_of(ty)
248+
if let Some(name) = self.schema.types.type_name_of(ty)
248249
&& self.item_enters_depth(name)
249250
{
250251
return true;

‎crates/plotnik-lib/src/compiler/emit/targets/rust/types.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ impl<'m, 'a> Emitter<'m, 'a> {
244244
TypeShape::Record(_) | TypeShape::Variant(_) => {
245245
let name = self
246246
.schema
247+
.types
247248
.type_name_of(ty)
248249
.expect("naming pass names every composite outside enum-variant payloads");
249250
self.named_type(name, ty)

‎crates/plotnik-lib/src/compiler/emit/targets/typescript/types.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<'a> SchemaEmitter<'a> {
232232

233233
fn render_ty(&self, ty: TypeId) -> Sink<SemanticTag> {
234234
if !ty.is_builtin()
235-
&& let Some(symbol) = self.schema.type_name_of(ty)
235+
&& let Some(symbol) = self.schema.types.type_name_of(ty)
236236
&& self.items_by_name.contains_key(&symbol)
237237
{
238238
let mut out = Sink::new();

‎crates/plotnik-lib/src/compiler/lower/spans.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ fn custom_capture_type_binding(
358358
.final_type;
359359

360360
loop {
361-
if input.result.type_name_of(type_id) == Some(written_name) {
361+
if input.analysis.type_analysis.type_name_of(type_id) == Some(written_name) {
362362
return Some(SpanBindingIR::Type(type_id));
363363
}
364364
match input.analysis.type_analysis.type_shape(type_id) {

0 commit comments

Comments
 (0)