sqry_core/graph/unified/bind/alias.rs
1//! Alias derivation for the Phase 2 binding plane.
2//!
3//! An `AliasEntry` records a single alias declaration (`from_symbol` in the
4//! scope resolves to the declaration at `to_symbol`). Wildcard imports
5//! (`from module import *`) are represented with `is_wildcard = true` and a
6//! sentinel `to_symbol` — the resolver forwards these through the target
7//! module's exports rather than the alias table directly.
8
9use std::collections::HashMap;
10
11use serde::{Deserialize, Serialize};
12
13use crate::graph::unified::build::phase4e_binding::BindingEdgeIndex;
14use crate::graph::unified::edge::kind::EdgeKind;
15use crate::graph::unified::mutation_target::GraphMutationTarget;
16use crate::graph::unified::node::id::NodeId;
17use crate::graph::unified::node::kind::NodeKind;
18use crate::graph::unified::string::id::StringId;
19
20use super::scope::{ScopeArena, ScopeId};
21
22/// Dense handle into an `AliasTable`.
23///
24/// The id is the position of the entry in the sorted `entries` slice, so
25/// `AliasEntryId(k)` is valid as long as the table hasn't been replaced.
26/// `AliasTable::get(id)` performs a single bounds-checked slice index.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
28pub struct AliasEntryId(pub u32);
29
30/// One alias declaration.
31///
32/// Records the scope where the alias is declared, the local name the alias
33/// introduces (`from_symbol`), the target name the alias points to
34/// (`to_symbol`), the `EdgeId` of the `Imports` edge that produced this
35/// entry, and whether this was a wildcard import.
36///
37/// The `id` field is the position of this entry in the owning `AliasTable`'s
38/// sorted slice after `derive_aliases` has run. It is assigned by the sort
39/// and must not be mutated by callers.
40#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
41pub struct AliasEntry {
42 /// Stable dense handle for this entry within its owning `AliasTable`.
43 /// Assigned after sorting; equals `AliasEntryId(idx)` where `idx` is the
44 /// position of this entry in the sorted entries slice.
45 pub id: AliasEntryId,
46 /// Scope that contains this alias declaration.
47 pub scope: ScopeId,
48 /// The local symbol name introduced by the alias (e.g., `baz` from
49 /// `use foo::bar as baz`).
50 pub from_symbol: StringId,
51 /// The qualified target name the alias resolves to (e.g., `foo::bar`).
52 ///
53 /// Falls back to the local name if `qualified_name` is unset on the
54 /// import node. In that case `to_symbol == from_symbol`.
55 pub to_symbol: StringId,
56 /// `NodeId` of the `Import` node that this entry was derived from.
57 pub import_node: NodeId,
58 /// Whether this is a wildcard import (`from module import *`).
59 pub is_wildcard: bool,
60}
61
62/// Content-addressed alias lookup table.
63///
64/// Entries are sorted by `(scope, from_symbol)` for `O(log n)` binary-search
65/// lookup via [`AliasTable::resolve_alias`]. Wildcard entries are included in
66/// [`AliasTable::aliases_in`] but are skipped by `resolve_alias` — the
67/// resolver dispatches wildcards through the target module's exports.
68///
69/// # Serialization
70///
71/// Only `entries` is persisted. The `by_scope` index is derived from
72/// `entries` on deserialization and cannot drift out of sync with the entries
73/// slice. This keeps the V9 snapshot footprint minimal and eliminates the
74/// dual-write hazard.
75#[derive(Debug, Clone, Default)]
76pub struct AliasTable {
77 /// All entries, sorted by `(scope, from_symbol)`.
78 entries: Vec<AliasEntry>,
79 /// Per-scope range index mapping a `ScopeId` to its `[start, end)` slice
80 /// in `entries`. Populated after the entries are sorted; **not** persisted.
81 by_scope: HashMap<ScopeId, (u32, u32)>,
82}
83
84impl AliasTable {
85 /// Creates an empty `AliasTable`.
86 #[must_use]
87 pub fn new() -> Self {
88 Self::default()
89 }
90
91 /// Returns the total number of alias entries.
92 #[must_use]
93 pub fn len(&self) -> usize {
94 self.entries.len()
95 }
96
97 /// Returns `true` if the table contains no entries.
98 #[must_use]
99 pub fn is_empty(&self) -> bool {
100 self.entries.is_empty()
101 }
102
103 /// Returns the full sorted entries slice.
104 ///
105 /// Useful for V9 persistence walkers, debug dumps, stats collection, and
106 /// any future consumer that needs to iterate the entire table.
107 #[must_use]
108 pub fn entries(&self) -> &[AliasEntry] {
109 &self.entries
110 }
111
112 /// Looks up an alias entry by its dense `AliasEntryId`.
113 ///
114 /// Returns `None` if `id.0 >= self.len()`. The id is the position of the
115 /// entry in the sorted slice, assigned during `derive_aliases`. It is
116 /// stable for the lifetime of this table instance.
117 ///
118 /// Used by `ResolutionStep::FollowAlias { alias: AliasEntryId }` in the
119 /// P2U07 resolver to recover the full `AliasEntry` for a recorded step.
120 #[must_use]
121 pub fn get(&self, id: AliasEntryId) -> Option<&AliasEntry> {
122 self.entries.get(id.0 as usize)
123 }
124
125 /// Returns all alias entries declared in `scope`, including wildcard
126 /// entries. Returns an empty slice when no aliases belong to `scope`.
127 #[must_use]
128 pub fn aliases_in(&self, scope: ScopeId) -> &[AliasEntry] {
129 match self.by_scope.get(&scope) {
130 Some(&(start, end)) => &self.entries[start as usize..end as usize],
131 None => &[],
132 }
133 }
134
135 /// Resolves `symbol` within `scope` to its target name.
136 ///
137 /// Returns `None` when no matching non-wildcard alias exists. Wildcard
138 /// entries are deliberately skipped — the resolver dispatches wildcards
139 /// through the target module's exports at query time.
140 ///
141 /// The lookup uses binary search on the sorted `(scope, from_symbol)`
142 /// order, so it is `O(log n)` in the number of aliases in `scope`.
143 #[must_use]
144 pub fn resolve_alias(&self, scope: ScopeId, symbol: StringId) -> Option<StringId> {
145 let slice = self.aliases_in(scope);
146 // slice is already scope-filtered, so sorting by from_symbol alone is valid.
147 slice
148 .binary_search_by(|entry| entry.from_symbol.cmp(&symbol))
149 .ok()
150 .map(|idx| &slice[idx])
151 .filter(|entry| !entry.is_wildcard)
152 .map(|entry| entry.to_symbol)
153 }
154
155 /// Applies `keep` to every entry's `import_node`, dropping entries whose
156 /// import-node fails the predicate.
157 ///
158 /// After filtering, the `id` of each surviving entry is reassigned to its
159 /// new position in the sorted `entries` slice, and the `by_scope` range
160 /// index is rebuilt from scratch so `resolve_alias` / `aliases_in` stay
161 /// consistent. The relative order of surviving entries is preserved, so
162 /// the table remains sorted by `(scope, from_symbol)`.
163 ///
164 /// This is the mutation entry point used by the Gate 0c `NodeIdBearing`
165 /// impl (A2 §K row K.A12). Callers that hold the table behind an `Arc`
166 /// must reach it through `Arc::make_mut` before invoking this method.
167 ///
168 /// `#[allow(dead_code)]` mirrors the `NodeIdBearing` trait itself: Gate 0b
169 /// lands the scaffolding and unit tests, Gate 0c adds the production
170 /// call site in `RebuildGraph::finalize()`.
171 #[allow(dead_code)]
172 pub(crate) fn retain_by_node(&mut self, keep: &dyn Fn(NodeId) -> bool) {
173 self.entries.retain(|entry| keep(entry.import_node));
174 for (idx, entry) in self.entries.iter_mut().enumerate() {
175 entry.id = AliasEntryId(u32::try_from(idx).expect("alias count fits u32"));
176 }
177 self.rebuild_index();
178 }
179
180 /// Rewrite every `StringId` stored on every entry through `remap`,
181 /// replacing any ID that appears as a key with its canonical value.
182 ///
183 /// This is the mutation entry point used by Gate 0c's finalize step 1
184 /// (`StringId` canonicalisation). `from_symbol` is part of the
185 /// `(scope, from_symbol)` sort key, so after rewrite the entries are
186 /// re-sorted by `(scope, from_symbol)` and `by_scope` is rebuilt so
187 /// `resolve_alias` / `aliases_in` keep returning consistent results.
188 ///
189 /// The relative order within a `(scope, from_symbol)` group is
190 /// preserved by using a stable sort. Empty `remap` is a no-op.
191 ///
192 /// Callers that hold the table behind an `Arc` must reach it through
193 /// `Arc::make_mut` before invoking this method.
194 #[allow(dead_code)]
195 pub(crate) fn rewrite_string_ids_through_remap(
196 &mut self,
197 remap: &std::collections::HashMap<StringId, StringId>,
198 ) {
199 if remap.is_empty() {
200 return;
201 }
202 let mut changed = false;
203 for entry in &mut self.entries {
204 if let Some(&canon) = remap.get(&entry.from_symbol) {
205 entry.from_symbol = canon;
206 changed = true;
207 }
208 if let Some(&canon) = remap.get(&entry.to_symbol) {
209 entry.to_symbol = canon;
210 changed = true;
211 }
212 }
213 if !changed {
214 return;
215 }
216 // `from_symbol` may have collapsed onto a different canonical ID,
217 // so re-establish the (scope, from_symbol) sort order and rebuild
218 // the scope range index. Use a stable sort to preserve the prior
219 // order within each (scope, from_symbol) group.
220 self.entries
221 .sort_by(|a, b| (a.scope, a.from_symbol).cmp(&(b.scope, b.from_symbol)));
222 for (idx, entry) in self.entries.iter_mut().enumerate() {
223 entry.id = AliasEntryId(u32::try_from(idx).expect("alias count fits u32"));
224 }
225 self.rebuild_index();
226 }
227
228 /// Rebuilds the `by_scope` range index from `self.entries`.
229 ///
230 /// Called after deserialization (where `by_scope` is not persisted) and
231 /// after constructing an `AliasTable` from a raw `Vec<AliasEntry>`. The
232 /// entries must already be sorted by `(scope, from_symbol)`.
233 fn rebuild_index(&mut self) {
234 debug_assert!(
235 self.entries.windows(2).all(|w| w[0].scope <= w[1].scope),
236 "rebuild_index requires entries sorted by scope ascending"
237 );
238 self.by_scope.clear();
239 let mut cursor = 0u32;
240 while (cursor as usize) < self.entries.len() {
241 let scope = self.entries[cursor as usize].scope;
242 let start = cursor;
243 while (cursor as usize) < self.entries.len()
244 && self.entries[cursor as usize].scope == scope
245 {
246 cursor += 1;
247 }
248 self.by_scope.insert(scope, (start, cursor));
249 }
250 }
251}
252
253// ---------------------------------------------------------------------------
254// Serde: persist only `entries`; rebuild `by_scope` on deserialize.
255// ---------------------------------------------------------------------------
256
257impl Serialize for AliasTable {
258 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
259 // Serialize as a newtype wrapping just the entries Vec.
260 self.entries.serialize(serializer)
261 }
262}
263
264impl<'de> Deserialize<'de> for AliasTable {
265 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
266 let entries = Vec::<AliasEntry>::deserialize(deserializer)?;
267 let mut table = AliasTable {
268 entries,
269 by_scope: HashMap::new(),
270 };
271 table.rebuild_index();
272 Ok(table)
273 }
274}
275
276/// Iterates every `Import` node via the `by_kind` index and, for each one,
277/// looks up its incoming `Imports` edges from the pre-built
278/// `BindingEdgeIndex::imports_by_target` map (O(1) per node). The source of
279/// each such edge is the importing scope's node; the enclosing scope is
280/// resolved via the precomputed `node_to_scope` map built from
281/// `ScopeArena::iter()`.
282///
283/// For `use foo::bar as baz;` the `alias` field on the edge carries `baz` and
284/// the import node's `qualified_name` carries `foo::bar`. For
285/// `from module import *`, `alias` is `None`, `is_wildcard` is `true`, and the
286/// import node's name is the sentinel `*`.
287///
288/// When the import node's `qualified_name` is `None`, `to_symbol` falls back
289/// to the plain `name` field — i.e., `to_symbol == from_symbol`.
290///
291/// Returns `(AliasTable, aliases_with_invalid_scope)` where the second value
292/// is the number of `Import` nodes whose enclosing scope could not be resolved.
293///
294/// # Performance
295///
296/// The `edge_index` parameter replaces the per-import `edges_to()` calls that
297/// previously triggered O(E) delta scans, turning per-node edge lookup from
298/// O(E) to O(1).
299pub(crate) fn derive_aliases<G: GraphMutationTarget>(
300 graph: &G,
301 scopes: &ScopeArena,
302 edge_index: &BindingEdgeIndex,
303) -> (AliasTable, u64) {
304 // Build a NodeId → ScopeId map for O(1) enclosing-scope lookup.
305 // Uses ScopeArena::iter() so each slot's real generation is used; avoids
306 // the generation=1 hardcode that a manual 0..slot_count loop would require.
307 let node_to_scope = build_node_to_scope_map(scopes);
308
309 let mut raw: Vec<AliasEntry> = Vec::new();
310 let mut invalid_scope_count: u64 = 0;
311
312 // Iterate every Import node and read its incoming Imports edges from
313 // the pre-built index (O(1) per node). Imports edges point FROM the
314 // importing scope/module TO the import node.
315 for &import_node_id in graph.indices().by_kind(NodeKind::Import) {
316 let Some(import_entry) = graph.nodes().get(import_node_id) else {
317 continue;
318 };
319
320 // Look up pre-indexed incoming Imports edges for this import node.
321 let Some(incoming) = edge_index.imports_by_target.get(&import_node_id) else {
322 continue;
323 };
324
325 for (source_node_id, edge_kind) in incoming {
326 let EdgeKind::Imports { alias, is_wildcard } = edge_kind else {
327 continue;
328 };
329
330 // Resolve the enclosing scope.
331 let scope = if let Some(id) = node_to_scope.get(source_node_id).copied() {
332 id
333 } else {
334 // The source node has no corresponding scope. Emit the
335 // entry with INVALID scope and count it for observability.
336 invalid_scope_count += 1;
337 ScopeId::INVALID
338 };
339
340 // `alias` holds the local name (e.g., `baz` in `use foo::bar as baz`).
341 // When no alias is given, use the import node's own name.
342 let from_symbol = alias.unwrap_or(import_entry.name);
343
344 // The target qualified name comes from the import node's qualified_name
345 // field, falling back to the plain name if not set — i.e., to_symbol
346 // equals from_symbol in the no-qualified-name case.
347 let to_symbol = import_entry.qualified_name.unwrap_or(import_entry.name);
348
349 // Placeholder id; will be overwritten after sorting.
350 raw.push(AliasEntry {
351 id: AliasEntryId(0),
352 scope,
353 from_symbol,
354 to_symbol,
355 import_node: import_node_id,
356 is_wildcard: *is_wildcard,
357 });
358 }
359 }
360
361 // Sort by (scope, from_symbol) to enable binary-search lookup.
362 raw.sort_by(|a, b| (a.scope, a.from_symbol).cmp(&(b.scope, b.from_symbol)));
363
364 // Assign stable ids: the position in the sorted slice IS the AliasEntryId.
365 for (idx, entry) in raw.iter_mut().enumerate() {
366 entry.id = AliasEntryId(u32::try_from(idx).expect("alias count fits u32"));
367 }
368
369 // Build the per-scope range index via the shared helper so the logic lives
370 // in exactly one place (deserialization also calls rebuild_index).
371 let mut table = AliasTable {
372 entries: raw,
373 by_scope: HashMap::new(),
374 };
375 table.rebuild_index();
376
377 (table, invalid_scope_count)
378}
379
380/// Builds a `HashMap<NodeId, ScopeId>` from the scope arena so that
381/// `derive_aliases` can resolve the enclosing scope for any node in O(1).
382///
383/// Uses `ScopeArena::iter()` so each slot's real generation is used and the
384/// generation=1 hardcode that a manual `0..slot_count` loop would require is
385/// avoided.
386fn build_node_to_scope_map(scopes: &ScopeArena) -> HashMap<NodeId, ScopeId> {
387 scopes.iter().map(|(id, scope)| (scope.node, id)).collect()
388}
389
390#[cfg(test)]
391mod tests {
392 use super::*;
393 use crate::graph::unified::node::id::NodeId;
394 use crate::graph::unified::string::id::StringId;
395
396 fn make_scope(index: u32) -> ScopeId {
397 ScopeId::new(index, 1)
398 }
399
400 fn make_node() -> NodeId {
401 NodeId::new(0, 1)
402 }
403
404 fn make_entry(scope: ScopeId, from: u32, to: u32, wildcard: bool) -> AliasEntry {
405 AliasEntry {
406 id: AliasEntryId(0), // patched by derive_aliases; set manually in unit tests
407 scope,
408 from_symbol: StringId::new(from),
409 to_symbol: StringId::new(to),
410 import_node: make_node(),
411 is_wildcard: wildcard,
412 }
413 }
414
415 /// Build an `AliasTable` directly from a pre-sorted entries slice (for
416 /// unit tests that don't want to go through `derive_aliases`).
417 fn table_from_sorted(mut entries: Vec<AliasEntry>) -> AliasTable {
418 for (idx, e) in entries.iter_mut().enumerate() {
419 e.id = AliasEntryId(u32::try_from(idx).unwrap());
420 }
421 let mut table = AliasTable {
422 entries,
423 by_scope: HashMap::new(),
424 };
425 table.rebuild_index();
426 table
427 }
428
429 #[test]
430 fn empty_table_lookup_returns_none() {
431 let table = AliasTable::new();
432 assert!(table.is_empty());
433 let scope = make_scope(0);
434 assert!(table.aliases_in(scope).is_empty());
435 assert_eq!(table.resolve_alias(scope, StringId::new(0)), None);
436 }
437
438 #[test]
439 fn binary_search_resolve_alias() {
440 let scope = make_scope(0);
441 let entries = vec![
442 make_entry(scope, 10, 100, false),
443 make_entry(scope, 20, 200, false),
444 ];
445 let table = table_from_sorted(entries);
446 assert_eq!(
447 table.resolve_alias(scope, StringId::new(10)),
448 Some(StringId::new(100))
449 );
450 assert_eq!(
451 table.resolve_alias(scope, StringId::new(20)),
452 Some(StringId::new(200))
453 );
454 assert_eq!(table.resolve_alias(scope, StringId::new(30)), None);
455 }
456
457 #[test]
458 fn wildcard_skipped_by_resolve() {
459 let scope = make_scope(0);
460 let entries = vec![make_entry(scope, 5, 99, true)];
461 let table = table_from_sorted(entries);
462 assert_eq!(table.resolve_alias(scope, StringId::new(5)), None);
463 assert!(
464 table.aliases_in(scope).iter().any(|e| e.is_wildcard),
465 "wildcard entry visible via aliases_in"
466 );
467 }
468
469 #[test]
470 fn get_by_alias_entry_id_round_trips() {
471 let scope = make_scope(0);
472 let entries = vec![
473 make_entry(scope, 10, 100, false),
474 make_entry(scope, 20, 200, false),
475 ];
476 let table = table_from_sorted(entries);
477
478 // Ids are assigned by position after sort.
479 let e0 = table.get(AliasEntryId(0)).expect("id 0 must exist");
480 let e1 = table.get(AliasEntryId(1)).expect("id 1 must exist");
481
482 assert_eq!(e0.from_symbol, StringId::new(10));
483 assert_eq!(e0.to_symbol, StringId::new(100));
484 assert_eq!(e0.id, AliasEntryId(0));
485
486 assert_eq!(e1.from_symbol, StringId::new(20));
487 assert_eq!(e1.to_symbol, StringId::new(200));
488 assert_eq!(e1.id, AliasEntryId(1));
489
490 assert!(table.get(AliasEntryId(2)).is_none());
491 }
492
493 #[test]
494 fn entries_accessor_returns_full_slice() {
495 let scope = make_scope(0);
496 let entries = vec![
497 make_entry(scope, 1, 10, false),
498 make_entry(scope, 2, 20, false),
499 make_entry(scope, 3, 30, true),
500 ];
501 let table = table_from_sorted(entries);
502 assert_eq!(table.entries().len(), 3);
503 }
504
505 #[test]
506 fn postcard_round_trip_preserves_entries_and_rebuilds_by_scope() {
507 let s_a = make_scope(1);
508 let s_b = make_scope(2);
509 let entries = vec![
510 make_entry(s_a, 10, 100, false),
511 make_entry(s_a, 11, 101, false),
512 make_entry(s_b, 20, 200, true),
513 ];
514 let original = table_from_sorted(entries);
515
516 let bytes = postcard::to_allocvec(&original).expect("serialize");
517 let restored: AliasTable = postcard::from_bytes(&bytes).expect("deserialize");
518
519 assert_eq!(
520 restored.entries().len(),
521 original.entries().len(),
522 "entry count must survive round-trip"
523 );
524 assert_eq!(
525 restored.entries(),
526 original.entries(),
527 "entries must be byte-equal after round-trip"
528 );
529
530 // Verify by_scope was rebuilt correctly: resolve_alias must work.
531 assert_eq!(
532 restored.resolve_alias(s_a, StringId::new(10)),
533 Some(StringId::new(100)),
534 "resolve_alias on restored table must return correct target for s_a entry 0"
535 );
536 assert_eq!(
537 restored.resolve_alias(s_a, StringId::new(11)),
538 Some(StringId::new(101)),
539 "resolve_alias on restored table must return correct target for s_a entry 1"
540 );
541 // Wildcard entry in s_b is not resolved by resolve_alias.
542 assert_eq!(
543 restored.resolve_alias(s_b, StringId::new(20)),
544 None,
545 "wildcard entry must not be returned by resolve_alias after round-trip"
546 );
547 assert!(
548 restored.aliases_in(s_b).iter().any(|e| e.is_wildcard),
549 "wildcard entry in s_b must survive round-trip"
550 );
551 }
552
553 #[test]
554 fn aliases_in_partition_by_scope() {
555 let s_a = make_scope(1);
556 let s_b = make_scope(2);
557 let s_c = make_scope(3);
558
559 let from_x = StringId::new(10);
560 let from_y = StringId::new(11);
561 let to_1 = StringId::new(20);
562 let to_2 = StringId::new(21);
563
564 // Six entries across three scopes, two entries each.
565 // Build unsorted and let table_from_sorted sort them so the sort matters.
566 let mut entries = vec![
567 // s_b entries (will sort after s_a, before s_c by ScopeId ordering)
568 AliasEntry {
569 id: AliasEntryId(0),
570 scope: s_b,
571 from_symbol: from_x,
572 to_symbol: to_1,
573 import_node: make_node(),
574 is_wildcard: false,
575 },
576 AliasEntry {
577 id: AliasEntryId(0),
578 scope: s_b,
579 from_symbol: from_y,
580 to_symbol: to_2,
581 import_node: make_node(),
582 is_wildcard: false,
583 },
584 // s_c entries
585 AliasEntry {
586 id: AliasEntryId(0),
587 scope: s_c,
588 from_symbol: from_x,
589 to_symbol: to_2,
590 import_node: make_node(),
591 is_wildcard: false,
592 },
593 AliasEntry {
594 id: AliasEntryId(0),
595 scope: s_c,
596 from_symbol: from_y,
597 to_symbol: to_1,
598 import_node: make_node(),
599 is_wildcard: false,
600 },
601 // s_a entries (will sort first)
602 AliasEntry {
603 id: AliasEntryId(0),
604 scope: s_a,
605 from_symbol: from_x,
606 to_symbol: to_2,
607 import_node: make_node(),
608 is_wildcard: false,
609 },
610 AliasEntry {
611 id: AliasEntryId(0),
612 scope: s_a,
613 from_symbol: from_y,
614 to_symbol: to_1,
615 import_node: make_node(),
616 is_wildcard: false,
617 },
618 ];
619 entries.sort_by(|a, b| (a.scope, a.from_symbol).cmp(&(b.scope, b.from_symbol)));
620 let table = table_from_sorted(entries);
621
622 // Each scope must see exactly its own 2 entries.
623 let b_entries = table.aliases_in(s_b);
624 assert_eq!(b_entries.len(), 2, "s_b must have exactly 2 entries");
625 assert!(
626 b_entries.iter().all(|e| e.scope == s_b),
627 "s_b slice must only contain s_b entries"
628 );
629
630 let a_entries = table.aliases_in(s_a);
631 assert_eq!(a_entries.len(), 2, "s_a must have exactly 2 entries");
632 assert!(
633 a_entries.iter().all(|e| e.scope == s_a),
634 "s_a slice must only contain s_a entries"
635 );
636
637 let c_entries = table.aliases_in(s_c);
638 assert_eq!(c_entries.len(), 2, "s_c must have exactly 2 entries");
639 assert!(
640 c_entries.iter().all(|e| e.scope == s_c),
641 "s_c slice must only contain s_c entries"
642 );
643
644 // Scope isolation: same from_symbol in different scopes resolves to different targets.
645 let resolved_a = table.resolve_alias(s_a, from_x);
646 let resolved_b = table.resolve_alias(s_b, from_x);
647 assert!(resolved_a.is_some(), "s_a must resolve from_x");
648 assert!(resolved_b.is_some(), "s_b must resolve from_x");
649 assert_ne!(
650 resolved_a, resolved_b,
651 "same from_symbol in different scopes must resolve to different targets"
652 );
653 }
654}