|
| 1 | +//! Multi-survivor instance-receiver disambiguation fails closed. |
| 2 | +//! |
| 3 | +//! Regression for the three.js mis-pick class: when the inferred receiver |
| 4 | +//! type matches multiple same-name members (duplicate class copies in the |
| 5 | +//! corpus), resolution must not fall through to the name-keyed priority |
| 6 | +//! ladder, whose terminal arms are first-pick and proximity — not identity |
| 7 | +//! evidence. Exactly one survivor resolves; zero or many fail closed. |
| 8 | +
|
| 9 | +use codanna::config::Settings; |
| 10 | +use codanna::indexing::pipeline::types::{ |
| 11 | + ResolutionContext, ResolvedBatch, SymbolLookupCache, UnresolvedRelationship, VariableBinding, |
| 12 | +}; |
| 13 | +use codanna::indexing::pipeline::{ResolveStage, ResolveStats}; |
| 14 | +use codanna::parsing::resolution::GenericResolutionContext; |
| 15 | +use codanna::parsing::{LanguageBehavior, LanguageId, ParserFactory}; |
| 16 | +use codanna::relationship::RelationshipMetadata; |
| 17 | +use codanna::symbol::ScopeContext; |
| 18 | +use codanna::types::{FileId, Range, SymbolId}; |
| 19 | +use codanna::{RelationKind, Symbol, SymbolKind, Visibility}; |
| 20 | +use std::collections::HashMap; |
| 21 | +use std::sync::Arc; |
| 22 | + |
| 23 | +fn js() -> LanguageId { |
| 24 | + LanguageId::new("javascript") |
| 25 | +} |
| 26 | + |
| 27 | +fn build_behaviors() -> HashMap<LanguageId, Arc<dyn LanguageBehavior>> { |
| 28 | + let settings = Settings::load().expect("Failed to load settings"); |
| 29 | + let factory = ParserFactory::new(Arc::new(settings)); |
| 30 | + let mut map = HashMap::new(); |
| 31 | + let behavior: Arc<dyn LanguageBehavior> = |
| 32 | + Arc::from(factory.create_behavior_from_registry(js())); |
| 33 | + map.insert(js(), behavior); |
| 34 | + map |
| 35 | +} |
| 36 | + |
| 37 | +fn method_of_class(id: u32, name: &str, class: &str, file: u32) -> Symbol { |
| 38 | + let mut sym = Symbol::new( |
| 39 | + SymbolId::new(id).unwrap(), |
| 40 | + name, |
| 41 | + SymbolKind::Method, |
| 42 | + FileId::new(file).unwrap(), |
| 43 | + Range::new(1, 1, 3, 2), |
| 44 | + ); |
| 45 | + sym.language_id = Some(js()); |
| 46 | + sym.visibility = Visibility::Public; |
| 47 | + sym.scope_context = Some(ScopeContext::ClassMember { |
| 48 | + class_name: Some(class.into()), |
| 49 | + }); |
| 50 | + sym |
| 51 | +} |
| 52 | + |
| 53 | +fn caller_fn(id: u32, file: u32) -> Symbol { |
| 54 | + let mut sym = Symbol::new( |
| 55 | + SymbolId::new(id).unwrap(), |
| 56 | + "run", |
| 57 | + SymbolKind::Function, |
| 58 | + FileId::new(file).unwrap(), |
| 59 | + Range::new(2, 0, 6, 1), |
| 60 | + ); |
| 61 | + sym.language_id = Some(js()); |
| 62 | + sym.visibility = Visibility::Public; |
| 63 | + sym |
| 64 | +} |
| 65 | + |
| 66 | +/// `run` holds `v = new Vector3()` (a binding inside its span) and calls |
| 67 | +/// `v.set(...)` after the binding site. |
| 68 | +fn typed_receiver_call(caller_file: u32) -> (UnresolvedRelationship, Vec<VariableBinding>) { |
| 69 | + let call = UnresolvedRelationship { |
| 70 | + from_id: Some(SymbolId::new(1).unwrap()), |
| 71 | + from_name: "run".into(), |
| 72 | + to_name: "set".into(), |
| 73 | + file_id: FileId::new(caller_file).unwrap(), |
| 74 | + kind: RelationKind::Calls, |
| 75 | + metadata: Some(RelationshipMetadata { |
| 76 | + line: Some(5), |
| 77 | + column: Some(1), |
| 78 | + context: None, |
| 79 | + receiver: Some("v".into()), |
| 80 | + static_call: false, |
| 81 | + }), |
| 82 | + to_range: Some(Range::new(5, 1, 5, 20)), |
| 83 | + }; |
| 84 | + let bindings = vec![VariableBinding { |
| 85 | + name: "v".to_string(), |
| 86 | + type_name: "Vector3".to_string(), |
| 87 | + range: Range::new(4, 1, 4, 30), |
| 88 | + }]; |
| 89 | + (call, bindings) |
| 90 | +} |
| 91 | + |
| 92 | +fn resolve_with_candidates(duplicate_copy: bool) -> (ResolvedBatch, ResolveStats) { |
| 93 | + let caller_file = 1; |
| 94 | + let cache = Arc::new(SymbolLookupCache::new()); |
| 95 | + cache.insert(caller_fn(1, caller_file)); |
| 96 | + // Lowest id: a same-name member of a SIBLING class. The pre-fix |
| 97 | + // fall-through picked this via language_matches[0]. |
| 98 | + cache.insert(method_of_class(2, "set", "Vector2", 2)); |
| 99 | + cache.insert(method_of_class(3, "set", "Vector3", 3)); |
| 100 | + if duplicate_copy { |
| 101 | + // Byte-copy of the receiver's class in another file (bundle copy). |
| 102 | + cache.insert(method_of_class(4, "set", "Vector3", 4)); |
| 103 | + } |
| 104 | + |
| 105 | + let stage = ResolveStage::new(Arc::clone(&cache), build_behaviors()); |
| 106 | + let (call, bindings) = typed_receiver_call(caller_file); |
| 107 | + |
| 108 | + let context = ResolutionContext { |
| 109 | + file_id: FileId::new(caller_file).unwrap(), |
| 110 | + language_id: js(), |
| 111 | + imports: vec![], |
| 112 | + local_symbols: vec![], |
| 113 | + scope: Box::new(GenericResolutionContext::new( |
| 114 | + FileId::new(caller_file).unwrap(), |
| 115 | + )), |
| 116 | + unresolved_rels: vec![call], |
| 117 | + variable_bindings: bindings, |
| 118 | + }; |
| 119 | + stage.resolve(&context) |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn duplicate_class_copies_fail_closed() { |
| 124 | + let (batch, stats) = resolve_with_candidates(true); |
| 125 | + assert_eq!( |
| 126 | + batch.len(), |
| 127 | + 0, |
| 128 | + "two Vector3.set copies tie at distance 0: multiple survivors must \ |
| 129 | + fail closed, not fall through to the ladder (which would first-pick \ |
| 130 | + the lowest-id sibling Vector2.set)" |
| 131 | + ); |
| 132 | + assert_eq!(stats.calls_resolved, 0); |
| 133 | + assert_eq!(stats.total_processed, 1); |
| 134 | +} |
| 135 | + |
| 136 | +#[test] |
| 137 | +fn single_class_copy_resolves_correct_member() { |
| 138 | + let (batch, stats) = resolve_with_candidates(false); |
| 139 | + assert_eq!( |
| 140 | + batch.len(), |
| 141 | + 1, |
| 142 | + "exactly one distance-0 survivor must resolve" |
| 143 | + ); |
| 144 | + let rel = batch |
| 145 | + .relationships |
| 146 | + .first() |
| 147 | + .expect("one resolved relationship"); |
| 148 | + assert_eq!( |
| 149 | + rel.to_id, |
| 150 | + SymbolId::new(3).unwrap(), |
| 151 | + "the survivor is Vector3.set — not the lower-id sibling Vector2.set" |
| 152 | + ); |
| 153 | + assert_eq!(stats.calls_resolved, 1); |
| 154 | +} |
0 commit comments