sqry-core 11.0.3

Core library for sqry - semantic code search engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! Ordered step trace vocabulary for the Phase 2 binding plane witness.
//!
//! Every transition the resolver makes — scope entry, bucket lookup, alias
//! follow, shadow rejection, candidate consideration, visibility filter,
//! tie-break, rejection, final choice — is represented as one variant of
//! [`ResolutionStep`]. The sequence of steps recorded during a
//! `BindingPlane::resolve()` call is the explainability contract Phases 3-6
//! read when walking witnesses.
//!
//! P2U06 adds `pub steps: Vec<ResolutionStep>` to `SymbolResolutionWitness`
//! so the steps surface on every resolve call. P2U07's `resolve_shared()`
//! helper is the emission point.

use std::fmt;

use serde::{Deserialize, Serialize};
use smallvec::SmallVec;

use super::super::alias::AliasEntryId;
use super::super::scope::ScopeId;
use crate::graph::unified::edge::id::EdgeId;
use crate::graph::unified::file::id::FileId;
use crate::graph::unified::node::id::NodeId;
use crate::graph::unified::resolution::{ResolutionMode, SymbolCandidateBucket};
use crate::graph::unified::string::id::StringId;

/// One step in an ordered resolution trace. Every variant carries only
/// `Eq`-compatible field types so [`SymbolResolutionWitness`]'s `Eq` derive
/// is preserved when `steps: Vec<ResolutionStep>` is added in P2U06.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ResolutionStep {
    /// Resolver entered the file-scope boundary for a query. Distinct from
    /// `EnterScope` because files are **not** arena-addressable scopes —
    /// they are addressed via `FileId`.
    EnterFileScope {
        /// File ID for the scope boundary being entered.
        file: FileId,
    },
    /// Resolver stepped into an arena-addressable scope.
    EnterScope {
        /// The scope being entered.
        scope: ScopeId,
    },
    /// Resolver probed a specific candidate bucket.
    LookupInBucket {
        /// Bucket being probed.
        bucket: SymbolCandidateBucket,
    },
    /// Resolver queried the `AliasTable` for the current scope.
    LookupInAliasTable {
        /// Scope whose alias table was queried.
        scope: ScopeId,
    },
    /// Resolver queried the `ShadowTable` for the current scope.
    LookupInShadowChain {
        /// Scope whose shadow chain was queried.
        scope: ScopeId,
    },
    /// Resolver considered a candidate node with its rank in the ordering.
    ConsiderCandidate {
        /// The candidate node being considered.
        node: NodeId,
        /// Zero-based rank of this candidate within the bucket ordering.
        rank: u16,
    },
    /// Resolver followed an alias edge from `from` to `to`.
    FollowAlias {
        /// Dense alias handle for the alias table entry.
        alias: AliasEntryId,
        /// Original symbol name before alias substitution.
        from: StringId,
        /// Alias-substituted symbol name.
        to: StringId,
    },
    /// Resolver detected that an outer binding is shadowed by an inner one.
    ShadowedBy {
        /// Outer scope whose binding is being shadowed.
        outer: ScopeId,
        /// Inner scope that introduces the shadowing binding.
        inner: ScopeId,
        /// Node in the inner scope that shadows the outer binding.
        by_node: NodeId,
    },
    /// Resolver followed an `EdgeKind::Imports` edge.
    FollowImportEdge {
        /// The import edge being followed.
        edge: EdgeId,
    },
    /// Resolver followed an `EdgeKind::Exports` re-export edge.
    FollowExportEdge {
        /// The export edge being followed.
        edge: EdgeId,
        /// Original exported name.
        from: StringId,
        /// Re-exported name at the destination.
        to: StringId,
    },
    /// Resolver performed an attribute / member lookup on a receiver node.
    /// Used for Python `y.method`, JS `obj.field`, Rust `self::Foo::bar`.
    FollowAttributeLookup {
        /// Node on which the attribute lookup is performed.
        receiver: NodeId,
        /// Name of the attribute or member being looked up.
        attribute: StringId,
    },
    /// Resolver rejected a candidate on visibility grounds.
    FilterByVisibility {
        /// Candidate node that was rejected.
        candidate: NodeId,
        /// Reason for the visibility rejection.
        reason: VisibilityReason,
    },
    /// Resolver applied the caller-supplied resolution mode.
    ApplyResolutionMode {
        /// The resolution mode that was applied.
        mode: ResolutionMode,
    },
    /// Resolver broke a tie between otherwise-equivalent candidates.
    TieBreak {
        /// Rule that broke the tie.
        reason: TieBreakReason,
    },
    /// Resolver rejected a candidate for a reason.
    Rejected {
        /// Node that was rejected.
        node: NodeId,
        /// Reason for rejection.
        reason: RejectionReason,
    },
    /// Resolver chose a final winner.
    Chose {
        /// The winning node.
        node: NodeId,
    },
    /// Resolver returned more than one candidate without a tie-break.
    Ambiguous {
        /// All surviving candidates (inline up to 4 to avoid heap allocation).
        candidates: SmallVec<[NodeId; 4]>,
    },
    /// Resolver gave up without a winner.
    Unresolved {
        /// Symbol that could not be resolved.
        symbol: StringId,
        /// Reason no winner was produced.
        reason: UnresolvedReason,
    },
}

impl fmt::Display for ResolutionStep {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ResolutionStep::EnterFileScope { file } => {
                write!(f, "enter file scope ({file})")
            }
            ResolutionStep::EnterScope { scope } => {
                write!(f, "enter scope ({scope:?})")
            }
            ResolutionStep::LookupInBucket { bucket } => {
                write!(f, "lookup in bucket: {bucket:?}")
            }
            ResolutionStep::LookupInAliasTable { scope } => {
                write!(f, "lookup in alias table (scope {scope:?})")
            }
            ResolutionStep::LookupInShadowChain { scope } => {
                write!(f, "lookup in shadow chain (scope {scope:?})")
            }
            ResolutionStep::ConsiderCandidate { node, rank } => {
                write!(f, "consider candidate {node} at rank {rank}")
            }
            ResolutionStep::FollowAlias { alias, from, to } => {
                write!(f, "follow alias {:?}: {from}{to}", alias)
            }
            ResolutionStep::ShadowedBy {
                outer,
                inner,
                by_node,
            } => {
                write!(
                    f,
                    "shadowed by {by_node} (outer scope {outer:?}, inner scope {inner:?})"
                )
            }
            ResolutionStep::FollowImportEdge { edge } => {
                write!(f, "follow import edge {edge}")
            }
            ResolutionStep::FollowExportEdge { edge, from, to } => {
                write!(f, "follow export edge {edge}: {from}{to}")
            }
            ResolutionStep::FollowAttributeLookup {
                receiver,
                attribute,
            } => {
                write!(f, "follow attribute lookup .{attribute} on {receiver}")
            }
            ResolutionStep::FilterByVisibility { candidate, reason } => {
                write!(f, "filter by visibility: {candidate} rejected ({reason:?})")
            }
            ResolutionStep::ApplyResolutionMode { mode } => {
                write!(f, "apply resolution mode: {mode:?}")
            }
            ResolutionStep::TieBreak { reason } => {
                write!(f, "tie-break: {reason:?}")
            }
            ResolutionStep::Rejected { node, reason } => {
                write!(f, "rejected {node}: {reason:?}")
            }
            ResolutionStep::Chose { node } => {
                write!(f, "chose {node}")
            }
            ResolutionStep::Ambiguous { candidates } => {
                write!(f, "ambiguous: {} candidates", candidates.len())
            }
            ResolutionStep::Unresolved { symbol, reason } => {
                write!(f, "unresolved {symbol}: {reason:?}")
            }
        }
    }
}

/// Reason a candidate was filtered out on visibility grounds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum VisibilityReason {
    /// Symbol has `private` / `pub(self)` visibility.
    Private,
    /// Symbol is `sealed` (e.g., Kotlin sealed class, Java sealed type).
    Sealed,
    /// Symbol has package-internal or module-internal visibility.
    Internal,
    /// Symbol is only visible within the declaring file.
    FileScoped,
    /// Symbol is accessible only via a specific module path.
    ModulePath,
    /// Symbol is not re-exported across a crate boundary.
    CrateBoundary,
    /// Symbol has `protected` visibility (Java, C++, C#, Kotlin, Scala, Ruby,
    /// Swift, PHP, Dart — any language where `protected` is a first-class
    /// access modifier). Appended after `CrateBoundary` so the postcard wire
    /// format remains additive (existing encoded variants are unaffected).
    Protected,
}

/// Reason a tie was broken between otherwise-equivalent candidates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TieBreakReason {
    /// Candidate in a narrower / more-local scope wins.
    NarrowerScope,
    /// Candidate reachable via a shorter qualified path wins.
    ShorterPath,
    /// Candidate introduced by an explicit import wins over wildcard.
    ExplicitImport,
    /// Earlier declaration (lower line number) wins.
    EarlierDeclaration,
    /// Language-specific priority rule applied (e.g., built-in over user def).
    LanguagePriority,
}

/// Reason a candidate was unconditionally rejected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RejectionReason {
    /// Candidate is not reachable from the query scope.
    OutOfScope,
    /// Candidate is shadowed by an inner binding.
    Shadowed,
    /// Candidate's `NodeKind` does not match the expected kind filter.
    WrongKind,
    /// Candidate's visibility is `private` relative to the call site.
    PrivateVisibility,
}

/// Reason the resolver gave up without producing a winner.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UnresolvedReason {
    /// No binding for the queried symbol was found in any scope.
    NotInAnyScope,
    /// Candidates were found but all were rejected by filters.
    AllCandidatesRejected,
    /// Multiple candidates survived and no tie-break rule applied.
    AmbiguousWithoutTieBreak,
    /// The requested file is valid but is not present in the indexed graph.
    FileNotIndexed,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::unified::node::id::NodeId;

    #[test]
    fn chose_round_trips_through_eq() {
        let step = ResolutionStep::Chose {
            node: NodeId::new(42, 1),
        };
        assert_eq!(step.clone(), step);
    }

    #[test]
    fn ambiguous_round_trips_through_postcard() {
        let mut candidates = SmallVec::<[NodeId; 4]>::new();
        candidates.push(NodeId::new(1, 1));
        candidates.push(NodeId::new(2, 1));
        candidates.push(NodeId::new(3, 1));
        let step = ResolutionStep::Ambiguous { candidates };
        let bytes = postcard::to_allocvec(&step).expect("serialize");
        let restored: ResolutionStep = postcard::from_bytes(&bytes).expect("deserialize");
        assert_eq!(restored, step);
    }

    #[test]
    fn smallvec_grows_past_inline_capacity() {
        let mut candidates = SmallVec::<[NodeId; 4]>::new();
        for idx in 0..8 {
            candidates.push(NodeId::new(idx, 1));
        }
        assert_eq!(candidates.len(), 8);
        let step = ResolutionStep::Ambiguous {
            candidates: candidates.clone(),
        };
        let bytes = postcard::to_allocvec(&step).expect("serialize");
        let restored: ResolutionStep = postcard::from_bytes(&bytes).expect("deserialize");
        if let ResolutionStep::Ambiguous { candidates: got } = restored {
            assert_eq!(got.len(), 8);
        } else {
            panic!("expected Ambiguous variant after round-trip");
        }
    }

    #[test]
    fn every_variant_serializes_through_postcard() {
        use super::super::super::alias::AliasEntryId;
        use super::super::super::scope::ScopeId;
        use crate::graph::unified::edge::id::EdgeId;
        use crate::graph::unified::file::id::FileId;
        use crate::graph::unified::resolution::{ResolutionMode, SymbolCandidateBucket};
        use crate::graph::unified::string::id::StringId;

        let steps = vec![
            ResolutionStep::EnterFileScope {
                file: FileId::new(0),
            },
            ResolutionStep::EnterScope {
                scope: ScopeId::new(1, 1),
            },
            ResolutionStep::LookupInBucket {
                bucket: SymbolCandidateBucket::ExactQualified,
            },
            ResolutionStep::LookupInAliasTable {
                scope: ScopeId::new(2, 1),
            },
            ResolutionStep::LookupInShadowChain {
                scope: ScopeId::new(3, 1),
            },
            ResolutionStep::ConsiderCandidate {
                node: NodeId::new(4, 1),
                rank: 0,
            },
            ResolutionStep::FollowAlias {
                alias: AliasEntryId(0),
                from: StringId::new(1),
                to: StringId::new(2),
            },
            ResolutionStep::ShadowedBy {
                outer: ScopeId::new(1, 1),
                inner: ScopeId::new(2, 1),
                by_node: NodeId::new(5, 1),
            },
            ResolutionStep::FollowImportEdge {
                edge: EdgeId::new(0),
            },
            ResolutionStep::FollowExportEdge {
                edge: EdgeId::new(1),
                from: StringId::new(3),
                to: StringId::new(4),
            },
            ResolutionStep::FollowAttributeLookup {
                receiver: NodeId::new(6, 1),
                attribute: StringId::new(5),
            },
            ResolutionStep::FilterByVisibility {
                candidate: NodeId::new(7, 1),
                reason: VisibilityReason::Private,
            },
            ResolutionStep::ApplyResolutionMode {
                mode: ResolutionMode::Strict,
            },
            ResolutionStep::TieBreak {
                reason: TieBreakReason::NarrowerScope,
            },
            ResolutionStep::Rejected {
                node: NodeId::new(8, 1),
                reason: RejectionReason::OutOfScope,
            },
            ResolutionStep::Chose {
                node: NodeId::new(9, 1),
            },
            ResolutionStep::Ambiguous {
                candidates: SmallVec::from_slice(&[NodeId::new(10, 1), NodeId::new(11, 1)]),
            },
            ResolutionStep::Unresolved {
                symbol: StringId::new(6),
                reason: UnresolvedReason::NotInAnyScope,
            },
        ];
        assert_eq!(steps.len(), 18, "must have exactly 18 variants");
        let bytes = postcard::to_allocvec(&steps).expect("serialize");
        let restored: Vec<ResolutionStep> = postcard::from_bytes(&bytes).expect("deserialize");
        assert_eq!(restored, steps);
    }

    /// Every `ResolutionStep` variant's `Display` output must be non-empty and
    /// contain the variant name (case-insensitive keyword match).
    #[test]
    fn all_variants_display_non_empty_and_contains_variant_keyword() {
        use super::super::super::alias::AliasEntryId;
        use super::super::super::scope::ScopeId;
        use crate::graph::unified::edge::id::EdgeId;
        use crate::graph::unified::file::id::FileId;
        use crate::graph::unified::resolution::{ResolutionMode, SymbolCandidateBucket};
        use crate::graph::unified::string::id::StringId;

        let steps: Vec<(ResolutionStep, &str)> = vec![
            (
                ResolutionStep::EnterFileScope {
                    file: FileId::new(0),
                },
                "enter file scope",
            ),
            (
                ResolutionStep::EnterScope {
                    scope: ScopeId::new(1, 1),
                },
                "enter scope",
            ),
            (
                ResolutionStep::LookupInBucket {
                    bucket: SymbolCandidateBucket::ExactQualified,
                },
                "lookup in bucket",
            ),
            (
                ResolutionStep::LookupInAliasTable {
                    scope: ScopeId::new(2, 1),
                },
                "lookup in alias table",
            ),
            (
                ResolutionStep::LookupInShadowChain {
                    scope: ScopeId::new(3, 1),
                },
                "lookup in shadow chain",
            ),
            (
                ResolutionStep::ConsiderCandidate {
                    node: NodeId::new(4, 1),
                    rank: 0,
                },
                "consider candidate",
            ),
            (
                ResolutionStep::FollowAlias {
                    alias: AliasEntryId(0),
                    from: StringId::new(1),
                    to: StringId::new(2),
                },
                "follow alias",
            ),
            (
                ResolutionStep::ShadowedBy {
                    outer: ScopeId::new(1, 1),
                    inner: ScopeId::new(2, 1),
                    by_node: NodeId::new(5, 1),
                },
                "shadowed by",
            ),
            (
                ResolutionStep::FollowImportEdge {
                    edge: EdgeId::new(0),
                },
                "follow import edge",
            ),
            (
                ResolutionStep::FollowExportEdge {
                    edge: EdgeId::new(1),
                    from: StringId::new(3),
                    to: StringId::new(4),
                },
                "follow export edge",
            ),
            (
                ResolutionStep::FollowAttributeLookup {
                    receiver: NodeId::new(6, 1),
                    attribute: StringId::new(5),
                },
                "follow attribute lookup",
            ),
            (
                ResolutionStep::FilterByVisibility {
                    candidate: NodeId::new(7, 1),
                    reason: VisibilityReason::Private,
                },
                "filter by visibility",
            ),
            (
                ResolutionStep::ApplyResolutionMode {
                    mode: ResolutionMode::Strict,
                },
                "apply resolution mode",
            ),
            (
                ResolutionStep::TieBreak {
                    reason: TieBreakReason::NarrowerScope,
                },
                "tie-break",
            ),
            (
                ResolutionStep::Rejected {
                    node: NodeId::new(8, 1),
                    reason: RejectionReason::OutOfScope,
                },
                "rejected",
            ),
            (
                ResolutionStep::Chose {
                    node: NodeId::new(9, 1),
                },
                "chose",
            ),
            (
                ResolutionStep::Ambiguous {
                    candidates: SmallVec::from_slice(&[NodeId::new(10, 1), NodeId::new(11, 1)]),
                },
                "ambiguous",
            ),
            (
                ResolutionStep::Unresolved {
                    symbol: StringId::new(6),
                    reason: UnresolvedReason::NotInAnyScope,
                },
                "unresolved",
            ),
        ];

        assert_eq!(steps.len(), 18, "must cover all 18 ResolutionStep variants");

        for (step, keyword) in &steps {
            let text = format!("{step}");
            assert!(
                !text.is_empty(),
                "Display output for step {step:?} must be non-empty"
            );
            assert!(
                text.to_lowercase().contains(keyword),
                "Display output for step {step:?} must contain keyword {:?}, got: {:?}",
                keyword,
                text
            );
        }
    }

    /// Round-trip every variant of every sub-enum through postcard.  Cheap
    /// insurance that the serde representation is stable and complete — if a
    /// variant is accidentally omitted from the `every_variant_serializes_through_postcard`
    /// test above, this test will still catch it.
    #[test]
    fn all_subenum_variants_round_trip() {
        macro_rules! round_trip {
            ($val:expr) => {{
                let bytes = postcard::to_allocvec(&$val).expect("serialize");
                let restored = postcard::from_bytes(&bytes).expect("deserialize");
                assert_eq!($val, restored);
            }};
        }

        // VisibilityReason — 7 variants (CrateBoundary was last; Protected is
        // appended to preserve the additive wire format).
        round_trip!(VisibilityReason::Private);
        round_trip!(VisibilityReason::Sealed);
        round_trip!(VisibilityReason::Internal);
        round_trip!(VisibilityReason::FileScoped);
        round_trip!(VisibilityReason::ModulePath);
        round_trip!(VisibilityReason::CrateBoundary);
        round_trip!(VisibilityReason::Protected);

        // TieBreakReason — 5 variants
        round_trip!(TieBreakReason::NarrowerScope);
        round_trip!(TieBreakReason::ShorterPath);
        round_trip!(TieBreakReason::ExplicitImport);
        round_trip!(TieBreakReason::EarlierDeclaration);
        round_trip!(TieBreakReason::LanguagePriority);

        // RejectionReason — 4 variants
        round_trip!(RejectionReason::OutOfScope);
        round_trip!(RejectionReason::Shadowed);
        round_trip!(RejectionReason::WrongKind);
        round_trip!(RejectionReason::PrivateVisibility);

        // UnresolvedReason — 4 variants
        round_trip!(UnresolvedReason::NotInAnyScope);
        round_trip!(UnresolvedReason::AllCandidatesRejected);
        round_trip!(UnresolvedReason::AmbiguousWithoutTieBreak);
        round_trip!(UnresolvedReason::FileNotIndexed);
    }
}