Skip to content

Commit 2c0dbe2

Browse files
committed
fix(resolution): unresolvable instance-call receivers fail closed
- `is_instance_type_compatible` fails closed instead of passing through when the receiver's type is neither inferred nor a self-form alias - `has_uninferrable_instance_receiver` distinguishes a present-but-untyped receiver from no-receiver, static-call, and self-form calls, which still resolve via existing logic - Priority-ladder candidate selection returns no match for untyped-receiver calls instead of picking the first same-name candidate by insertion order
1 parent 028d7c9 commit 2c0dbe2

3 files changed

Lines changed: 219 additions & 24 deletions

File tree

‎src/indexing/pipeline/stages/resolve.rs‎

Lines changed: 202 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,52 @@ impl ResolveStage {
299299
Some((type_name, caller))
300300
}
301301

302+
/// An instance call whose receiver is syntactically present but whose
303+
/// type is neither inferred nor indexed. Bare-name fallback fails closed
304+
/// here: with no type to check, any same-name candidate is a guess, and
305+
/// first-pick guesses attach std/foreign-receiver calls to arbitrary
306+
/// user methods. Self-form receivers stay resolvable: the enclosing
307+
/// type is known even without signature inference.
308+
fn has_uninferrable_instance_receiver(
309+
&self,
310+
unresolved: &UnresolvedRelationship,
311+
language_id: &LanguageId,
312+
) -> bool {
313+
if unresolved.kind != RelationKind::Calls {
314+
return false;
315+
}
316+
let Some(metadata) = unresolved.metadata.as_ref() else {
317+
return false;
318+
};
319+
if metadata.static_call {
320+
return false;
321+
}
322+
let Some(receiver) = metadata.receiver.as_deref() else {
323+
return false;
324+
};
325+
let self_aliases = self
326+
.get_behavior(language_id)
327+
.map(|b| b.self_receiver_aliases())
328+
.unwrap_or(&["self"]);
329+
if self_aliases.contains(&receiver) {
330+
return false;
331+
}
332+
self.infer_receiver_type(unresolved, language_id).is_none()
333+
}
334+
302335
/// Single-candidate gate (Found arm of `resolve_one`): when the inferred
303336
/// receiver type is known, the candidate is compatible iff its containing
304-
/// class matches via `is_receiver_compatible`. When no inference data is
305-
/// available (filter doesn't apply), returns `true` (pass-through).
337+
/// class matches via `is_receiver_compatible`. When inference misses,
338+
/// three-state: no receiver in play (plain call, static call, self form)
339+
/// passes through; a receiver whose type is unknown fails closed.
306340
fn is_instance_type_compatible(
307341
&self,
308342
unresolved: &UnresolvedRelationship,
309343
to_id: SymbolId,
310344
language_id: &LanguageId,
311345
) -> bool {
312346
let Some((type_name, caller)) = self.infer_receiver_type(unresolved, language_id) else {
313-
return true;
347+
return !self.has_uninferrable_instance_receiver(unresolved, language_id);
314348
};
315349
let Some(behavior) = self.get_behavior(language_id) else {
316350
return true;
@@ -517,6 +551,10 @@ impl ResolveStage {
517551
0 => return None,
518552
_ => {}
519553
}
554+
} else if self.has_uninferrable_instance_receiver(unresolved, language_id) {
555+
// Receiver present, type unknown: an unresolved edge beats
556+
// a first-pick guess from the priority ladder below.
557+
return None;
520558
}
521559
}
522560
let candidates = &filtered[..];
@@ -778,6 +816,167 @@ mod tests {
778816
ResolveStage::new(cache, HashMap::new())
779817
}
780818

819+
fn make_instance_call(
820+
from_id: u32,
821+
to_name: &str,
822+
file_id: u32,
823+
receiver: &str,
824+
) -> UnresolvedRelationship {
825+
let mut unresolved = make_unresolved(from_id, to_name, file_id, RelationKind::Calls);
826+
unresolved.metadata = Some(crate::relationship::RelationshipMetadata {
827+
line: Some(5),
828+
column: Some(4),
829+
context: None,
830+
receiver: Some(receiver.into()),
831+
static_call: false,
832+
});
833+
unresolved
834+
}
835+
836+
#[test]
837+
fn unknown_receiver_instance_call_fails_closed_cross_file() {
838+
// some_vec.len() where some_vec's type is neither inferred nor
839+
// indexed must not attach to a same-name user method elsewhere.
840+
let cache = Arc::new(SymbolLookupCache::new());
841+
cache.insert(make_symbol(1, "caller", 1, LanguageId::new("rust")));
842+
let mut method = make_symbol(2, "len", 2, LanguageId::new("rust"));
843+
method.kind = SymbolKind::Method;
844+
cache.insert(method);
845+
846+
let stage = make_stage(cache);
847+
let context = make_context(
848+
1,
849+
LanguageId::new("rust"),
850+
vec![SymbolId::new(1).unwrap()],
851+
vec![make_instance_call(1, "len", 1, "some_vec")],
852+
);
853+
854+
let (batch, stats) = stage.resolve(&context);
855+
856+
assert_eq!(stats.resolved, 0, "unknown receiver must fail closed");
857+
assert!(batch.is_empty());
858+
}
859+
860+
#[test]
861+
fn unknown_receiver_instance_call_fails_closed_same_file() {
862+
// The local-first ladder is the strongest absorber: a same-file
863+
// same-name method must not win on file locality alone when the
864+
// receiver's type is unknown.
865+
let cache = Arc::new(SymbolLookupCache::new());
866+
cache.insert(make_symbol(1, "caller", 1, LanguageId::new("rust")));
867+
let mut local = make_symbol(2, "get", 1, LanguageId::new("rust"));
868+
local.kind = SymbolKind::Method;
869+
cache.insert(local);
870+
let mut remote = make_symbol(3, "get", 2, LanguageId::new("rust"));
871+
remote.kind = SymbolKind::Method;
872+
cache.insert(remote);
873+
874+
let stage = make_stage(cache);
875+
let context = make_context(
876+
1,
877+
LanguageId::new("rust"),
878+
vec![SymbolId::new(1).unwrap(), SymbolId::new(2).unwrap()],
879+
vec![make_instance_call(1, "get", 1, "settings_map")],
880+
);
881+
882+
let (batch, stats) = stage.resolve(&context);
883+
884+
assert_eq!(stats.resolved, 0, "same-file candidate must not absorb");
885+
assert!(batch.is_empty());
886+
}
887+
888+
#[test]
889+
fn self_receiver_instance_call_still_resolves() {
890+
// self.helper(): the enclosing type is known without signature
891+
// inference; the self form must keep resolving through locality.
892+
let cache = Arc::new(SymbolLookupCache::new());
893+
cache.insert(make_symbol(1, "caller", 1, LanguageId::new("rust")));
894+
let mut method = make_symbol(2, "helper", 1, LanguageId::new("rust"));
895+
method.kind = SymbolKind::Method;
896+
cache.insert(method);
897+
898+
let stage = make_stage(cache);
899+
let context = make_context(
900+
1,
901+
LanguageId::new("rust"),
902+
vec![SymbolId::new(1).unwrap(), SymbolId::new(2).unwrap()],
903+
vec![make_instance_call(1, "helper", 1, "self")],
904+
);
905+
906+
let (batch, stats) = stage.resolve(&context);
907+
908+
assert_eq!(stats.resolved, 1, "self receiver must keep resolving");
909+
assert_eq!(batch.relationships[0].to_id, SymbolId::new(2).unwrap());
910+
}
911+
912+
#[test]
913+
fn inferred_receiver_type_still_resolves_cross_file() {
914+
// fn process(w: Wrapper) { w.len() }: signature inference names the
915+
// receiver type, so the call resolves to Wrapper::len cross-file.
916+
let rust = LanguageId::new("rust");
917+
let cache = Arc::new(SymbolLookupCache::new());
918+
let mut caller = make_symbol(1, "process", 1, rust);
919+
caller.signature = Some("fn process(w: Wrapper)".into());
920+
cache.insert(caller);
921+
let mut method = make_symbol(2, "len", 2, rust);
922+
method.kind = SymbolKind::Method;
923+
method.scope_context = Some(crate::symbol::ScopeContext::ClassMember {
924+
class_name: Some("Wrapper".into()),
925+
});
926+
cache.insert(method);
927+
928+
let behaviors: HashMap<LanguageId, StdArc<dyn LanguageBehavior>> = HashMap::from([(
929+
rust,
930+
StdArc::new(crate::parsing::rust::RustBehavior::new()) as StdArc<dyn LanguageBehavior>,
931+
)]);
932+
let stage = ResolveStage::new(cache, behaviors);
933+
let context = make_context(
934+
1,
935+
rust,
936+
vec![SymbolId::new(1).unwrap()],
937+
vec![make_instance_call(1, "len", 1, "w")],
938+
);
939+
940+
let (batch, stats) = stage.resolve(&context);
941+
942+
assert_eq!(stats.resolved, 1, "inferred receiver type must resolve");
943+
assert_eq!(batch.relationships[0].to_id, SymbolId::new(2).unwrap());
944+
}
945+
946+
#[test]
947+
fn python_self_aliases_pass_untyped_locals_fail_closed() {
948+
// cls.get() resolves through the python self vocabulary;
949+
// prefix_settings.get() (untyped local, the story-15 phantom class)
950+
// does not.
951+
let python = LanguageId::new("python");
952+
let cache = Arc::new(SymbolLookupCache::new());
953+
cache.insert(make_symbol(1, "run_example", 1, python));
954+
let mut method = make_symbol(2, "get", 1, python);
955+
method.kind = SymbolKind::Method;
956+
cache.insert(method);
957+
958+
let behaviors: HashMap<LanguageId, StdArc<dyn LanguageBehavior>> = HashMap::from([(
959+
python,
960+
StdArc::new(crate::parsing::python::PythonBehavior::new())
961+
as StdArc<dyn LanguageBehavior>,
962+
)]);
963+
let stage = ResolveStage::new(cache, behaviors);
964+
let context = make_context(
965+
1,
966+
python,
967+
vec![SymbolId::new(1).unwrap(), SymbolId::new(2).unwrap()],
968+
vec![
969+
make_instance_call(1, "get", 1, "cls"),
970+
make_instance_call(1, "get", 1, "prefix_settings"),
971+
],
972+
);
973+
974+
let (batch, stats) = stage.resolve(&context);
975+
976+
assert_eq!(stats.resolved, 1, "cls resolves, untyped local does not");
977+
assert_eq!(batch.relationships[0].to_id, SymbolId::new(2).unwrap());
978+
}
979+
781980
#[test]
782981
fn test_resolve_single_candidate() {
783982
let cache = Arc::new(SymbolLookupCache::new());

‎tests/integration/test_resolve_param_type_inference.rs‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ fn instance_call_filters_ambiguous_by_inferred_parameter_type() {
195195
}
196196

197197
#[test]
198-
fn instance_call_falls_through_when_receiver_not_a_parameter() {
198+
fn instance_call_fails_closed_when_receiver_not_a_parameter() {
199199
// When `receiver` doesn't name a parameter on the caller's signature
200-
// (e.g., a non-parameter local), the inference filter must skip and let
201-
// the existing priority logic decide.
200+
// (e.g., a non-parameter local), the receiver's type is unknown and
201+
// resolution fails closed: any same-name pick would be a guess that
202+
// attaches std/foreign-receiver calls to arbitrary user methods.
202203
let caller_file = FileId::new(1).unwrap();
203204
let other_store_file = FileId::new(2).unwrap();
204205
let document_store_file = FileId::new(3).unwrap();
@@ -210,7 +211,6 @@ fn instance_call_falls_through_when_receiver_not_a_parameter() {
210211
"fn caller(store: &DocumentStore)",
211212
));
212213

213-
let other_store_method_id = SymbolId::new(2).unwrap();
214214
cache.insert(make_method_on_class(
215215
2,
216216
"process",
@@ -240,16 +240,13 @@ fn instance_call_falls_through_when_receiver_not_a_parameter() {
240240
)],
241241
};
242242

243-
let (batch, _stats) = stage.resolve(&context);
243+
let (batch, stats) = stage.resolve(&context);
244244

245-
let rel = batch
246-
.relationships
247-
.first()
248-
.expect("one resolved relationship");
249-
assert_eq!(
250-
rel.to_id, other_store_method_id,
251-
"with no matching parameter, existing priority logic must pick the first-inserted same-name candidate"
245+
assert!(
246+
batch.is_empty(),
247+
"unknown-type receiver must not resolve to any same-name candidate"
252248
);
249+
assert_eq!(stats.resolved, 0);
253250
}
254251

255252
#[test]

‎tests/integration/test_resolve_static_call.rs‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ fn instance_call_skips_static_filter() {
150150

151151
let cache = Arc::new(SymbolLookupCache::new());
152152
cache.insert(make_caller(1, caller_file));
153-
let symbol_method_id = SymbolId::new(2).unwrap();
154153
cache.insert(make_method_on_class(2, "new", symbol_file, Some("Symbol")));
155154
cache.insert(make_method_on_class(
156155
3,
@@ -176,16 +175,16 @@ fn instance_call_skips_static_filter() {
176175
)],
177176
};
178177

179-
let (batch, _stats) = stage.resolve(&context);
178+
let (batch, stats) = stage.resolve(&context);
180179

181-
let rel = batch
182-
.relationships
183-
.first()
184-
.expect("one resolved relationship");
185-
assert_eq!(
186-
rel.to_id, symbol_method_id,
187-
"instance call must bypass the static-call filter and fall through to priority logic (insertion-order pick)"
180+
// The static-call filter is bypassed (no receiver-class pick), and the
181+
// instance receiver's type is unknown, so resolution fails closed
182+
// rather than falling through to an insertion-order guess.
183+
assert!(
184+
batch.is_empty(),
185+
"instance call with unknown-type receiver must not resolve"
188186
);
187+
assert_eq!(stats.resolved, 0);
189188
}
190189

191190
#[test]

0 commit comments

Comments
 (0)