Skip to content

Commit 8372793

Browse files
committed
fix(swift): keep the label match when an inferred receiver type is unknown
A variable whose type comes only from its initializer call (`let p = loop.makePromise(of: Int.self)`) replaced the label match with a typed lookup, and a failed lookup dropped the call. On swift-nio that left 1,976 call sites without an edge that the label match had resolved. The inferred type now only narrows the label match. When the inference fails (overloads with different return types, an unknown callee or root, a cycle) the receiver stays unknown, and when the inferred type has no member with the call's name and labels the call keeps the label match. Written types (annotations, parameters, constructor calls) and non-project receivers behave as before. Inferring an initializer's own type never uses a label match, so a guess cannot become a type. A callee's own generic parameter used as its return type (`func runSALOnEventLoop<Result>(...) -> Result`) names no type. It is read from the declaration's generic clause in the qualified name and leaves the bound variable unknown. The four rows of pipeline_swift_capped_chain_returns_and_metatype_receivers that expected no edge for an unknown receiver now expect exactly one label-matched upload(_:) edge. Signed-off-by: Martin Vogel <[email protected]>
1 parent 1a6b292 commit 8372793

2 files changed

Lines changed: 141 additions & 49 deletions

File tree

‎src/pipeline/registry.c‎

Lines changed: 114 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3633,6 +3633,8 @@ typedef struct {
36333633
const char *module_qn;
36343634
const char **imports;
36353635
int import_count;
3636+
/* Inferring an initializer's type: a label match is a guess, never a type. */
3637+
bool strict;
36363638
} swift_inference_t;
36373639

36383640
static const char *swift_initializer_type(const swift_inference_t *context,
@@ -3686,11 +3688,17 @@ static const char *swift_binding_cached_type(swift_binding_work_t *work,
36863688
return "";
36873689
}
36883690

3689-
/* Conflicting block-local bindings stay unknown rather than selecting a
3690-
* same-name method on either type. */
3691+
/* Where a receiver's type came from: no binding, a declaration (an annotation,
3692+
* a parameter, a constructor call), or only an initializer whose type was
3693+
* inferred from the call it makes. */
3694+
typedef enum { SWIFT_ORIGIN_NONE, SWIFT_ORIGIN_DECLARED, SWIFT_ORIGIN_INFERRED } swift_origin_t;
3695+
3696+
/* Conflicting block-local bindings ("") select no same-name method on either
3697+
* type. A binding whose initializer type could not be inferred leaves the
3698+
* receiver unknown (NULL), so the call keeps the label match (#2061). */
36913699
static const char *swift_binding_type(const swift_inference_t *context, swift_binding_work_t *work,
36923700
const char *name, const char *scope, const char *class_qn,
3693-
const char *module_qn, bool field) {
3701+
const char *module_qn, bool field, swift_origin_t *origin) {
36943702
const CBMFileResult *file = context->file;
36953703
int best = 0;
36963704
for (int i = 0; i < file->type_assigns.count; i++) {
@@ -3700,22 +3708,33 @@ static const char *swift_binding_type(const swift_inference_t *context, swift_bi
37003708
best = rank;
37013709
}
37023710
}
3711+
*origin = best ? SWIFT_ORIGIN_INFERRED : SWIFT_ORIGIN_NONE;
37033712
if (!best) {
37043713
return NULL;
37053714
}
37063715
const char *type = NULL;
3716+
bool unknown = false;
37073717
for (int i = 0; i < file->type_assigns.count; i++) {
37083718
const CBMTypeAssign *a = &file->type_assigns.items[i];
37093719
if (swift_assignment_rank(a, name, scope, class_qn, module_qn, field) != best) {
37103720
continue;
37113721
}
3712-
const char *candidate = a->type_name ? a->type_name : swift_binding_cached_type(work, a);
3722+
const char *candidate = a->type_name;
3723+
if (candidate) {
3724+
*origin = SWIFT_ORIGIN_DECLARED;
3725+
} else {
3726+
candidate = swift_binding_cached_type(work, a);
3727+
if (!candidate[0]) {
3728+
unknown = true;
3729+
continue;
3730+
}
3731+
}
37133732
if (type && strcmp(type, candidate) != 0) {
37143733
return "";
37153734
}
37163735
type = candidate;
37173736
}
3718-
return type;
3737+
return unknown ? NULL : type;
37193738
}
37203739

37213740
static void swift_enclosing_class(const CBMCall *call, char *class_qn) {
@@ -3734,8 +3753,9 @@ static void swift_enclosing_class(const CBMCall *call, char *class_qn) {
37343753

37353754
static const char *swift_file_receiver(const swift_inference_t *context, swift_binding_work_t *work,
37363755
const CBMCall *call, const char *module_qn, char *expression,
3737-
char *class_qn, char **member) {
3756+
char *class_qn, char **member, swift_origin_t *origin) {
37383757
swift_enclosing_class(call, class_qn);
3758+
*origin = SWIFT_ORIGIN_DECLARED;
37393759
char *dot = strchr(expression, '.');
37403760
*dot = '\0';
37413761
*member = dot + SKIP_ONE;
@@ -3746,13 +3766,14 @@ static const char *swift_file_receiver(const swift_inference_t *context, swift_b
37463766
}
37473767
*dot = '\0';
37483768
const char *type = swift_binding_type(context, work, *member, call->enclosing_func_qn,
3749-
class_qn, module_qn, true);
3769+
class_qn, module_qn, true, origin);
37503770
*member = dot + SKIP_ONE;
37513771
return type;
37523772
}
37533773
const char *type = swift_binding_type(context, work, expression, call->enclosing_func_qn,
3754-
class_qn, module_qn, false);
3755-
if (!type && isupper((unsigned char)expression[0])) {
3774+
class_qn, module_qn, false, origin);
3775+
if (*origin == SWIFT_ORIGIN_NONE && isupper((unsigned char)expression[0])) {
3776+
*origin = SWIFT_ORIGIN_DECLARED;
37563777
return expression;
37573778
}
37583779
return type;
@@ -3867,6 +3888,34 @@ static const char *swift_function_return_type(const swift_inference_t *context,
38673888
return NULL; /* No declaring-file imports are available for a remote free function. */
38683889
}
38693890

3891+
/* `func run<Result>() -> Result`: the callee's own generic parameter names no
3892+
* type, so the value it returns stays unknown (#2061). The QN keeps the
3893+
* declaration's generic clause unless the signature was capped. */
3894+
static bool swift_returns_generic_parameter(const char *qn, const char *member,
3895+
const char *returned) {
3896+
char nominal[CBM_SZ_4K];
3897+
if (!member || !swift_receiver_type_text(returned, nominal, sizeof(nominal))) {
3898+
return false;
3899+
}
3900+
const char *clause = qn + cbm_qn_callable_base_len_named(qn, member);
3901+
size_t length = clause[0] == '<' ? swift_generic_prefix_len(clause, strlen(clause)) : 0;
3902+
size_t name_length = strlen(nominal);
3903+
unsigned depth = 0;
3904+
for (size_t i = SKIP_ONE; i + SKIP_ONE < length && clause[i] != ';'; i++) {
3905+
bool entry = depth == 0 && (i == SKIP_ONE || clause[i - SKIP_ONE] == ',');
3906+
if (entry && strncmp(clause + i, nominal, name_length) == 0 && clause[i + name_length] &&
3907+
strchr(":,;>", clause[i + name_length])) {
3908+
return true;
3909+
}
3910+
if (clause[i] == '<') {
3911+
depth++;
3912+
} else if (clause[i] == '>' && clause[i - SKIP_ONE] != '=' && depth) {
3913+
depth--;
3914+
}
3915+
}
3916+
return false;
3917+
}
3918+
38703919
static const char *swift_candidates_return_type(const swift_inference_t *context, int count,
38713920
const char **out, const char *receiver,
38723921
const char *member) {
@@ -3875,7 +3924,8 @@ static const char *swift_candidates_return_type(const swift_inference_t *context
38753924
const char *spelling = NULL;
38763925
for (int i = 0; i < count; i++) {
38773926
const char *returned = swift_chain_return_spelling(r, out[i]);
3878-
if (!returned || (spelling && strcmp(spelling, returned) != 0)) {
3927+
if (!returned || (spelling && strcmp(spelling, returned) != 0) ||
3928+
swift_returns_generic_parameter(out[i], member, returned)) {
38793929
return NULL;
38803930
}
38813931
const char *label = cbm_ht_get(r->exact, out[i]);
@@ -3970,12 +4020,15 @@ static int swift_initializer_member_candidates(const swift_inference_t *context,
39704020
}
39714021
(void)snprintf(expression, sizeof(expression), "%s", call->callee_name);
39724022
char *member = NULL;
3973-
const char *type =
3974-
swift_file_receiver(context, work, call, context->module_qn, expression, owner, &member);
4023+
swift_origin_t origin;
4024+
const char *type = swift_file_receiver(context, work, call, context->module_qn, expression,
4025+
owner, &member, &origin);
39754026
if (!type || !type[0]) {
39764027
return SWIFT_INCOMPATIBLE; /* Unknown roots cannot establish an initializer's type. */
39774028
}
3978-
return swift_candidates_for_file(context, work, call, out, REG_MAX_CANDIDATES);
4029+
swift_inference_t strict = *context;
4030+
strict.strict = true;
4031+
return swift_candidates_for_file(&strict, work, call, out, REG_MAX_CANDIDATES);
39794032
}
39804033

39814034
static int swift_initializer_candidates(const swift_inference_t *context,
@@ -4078,6 +4131,44 @@ static bool swift_call_uses_named_receiver(const CBMFileResult *file, const CBMC
40784131
(isupper((unsigned char)call->callee_name[0]) && !strchr(call->callee_name, '('));
40794132
}
40804133

4134+
/* Walk the receiver chain from its root type: each `name(...)` segment
4135+
* continues on the return type its compatible candidates agree on. */
4136+
static int swift_receiver_candidates(const swift_inference_t *context, const CBMCall *call,
4137+
char *expression, const char *type, char *member,
4138+
const char **out, int out_cap) {
4139+
const cbm_registry_t *r = context->registry;
4140+
bool from_chain = r->swift_owners && cbm_ht_has(r->swift_owners->by_qn, type);
4141+
while (type && type[0]) {
4142+
char type_text[CBM_SZ_4K];
4143+
if (!from_chain && !swift_receiver_type_text(type, type_text, sizeof(type_text))) {
4144+
return SWIFT_INCOMPATIBLE;
4145+
}
4146+
const char *receiver =
4147+
from_chain
4148+
? cbm_ht_get_key(r->exact, type)
4149+
: swift_receiver_qn(r, context->file, context->graph, type_text, context->module_qn,
4150+
context->imports, context->import_count);
4151+
if (!receiver) {
4152+
return SWIFT_INCOMPATIBLE; /* known non-project receiver */
4153+
}
4154+
char *args = strchr(member, '(');
4155+
if (!args) {
4156+
return swift_typed_candidates(r, context->graph, context->file, context->module_qn,
4157+
call, receiver, member, out, out_cap);
4158+
}
4159+
char *after = swift_after_arguments(args);
4160+
if (!after || *after != '.') {
4161+
return SWIFT_INCOMPATIBLE;
4162+
}
4163+
*args = '\0';
4164+
const CBMCall *inner = swift_inner_call(context->file, call, (size_t)(args - expression));
4165+
type = swift_chain_return(context, inner, receiver, member, out, out_cap);
4166+
from_chain = true;
4167+
member = after + SKIP_ONE;
4168+
}
4169+
return SWIFT_INCOMPATIBLE;
4170+
}
4171+
40814172
static int swift_candidates_for_file(const swift_inference_t *context, swift_binding_work_t *work,
40824173
const CBMCall *call, const char **out, int out_cap) {
40834174
const cbm_registry_t *r = context->registry;
@@ -4104,40 +4195,22 @@ static int swift_candidates_for_file(const swift_inference_t *context, swift_bin
41044195
}
41054196
(void)snprintf(expression, sizeof(expression), "%s", call->callee_name);
41064197
char *member = NULL;
4198+
swift_origin_t origin;
41074199
const char *type =
4108-
swift_file_receiver(context, work, call, module_qn, expression, class_qn, &member);
4200+
swift_file_receiver(context, work, call, module_qn, expression, class_qn, &member, &origin);
41094201
if (!type) {
41104202
return cbm_registry_swift_candidates(r, call, module_qn, import_vals, import_count, out,
41114203
out_cap);
41124204
}
4113-
bool from_chain = r->swift_owners && cbm_ht_has(r->swift_owners->by_qn, type);
4114-
while (type && type[0]) {
4115-
char type_text[CBM_SZ_4K];
4116-
if (!from_chain && !swift_receiver_type_text(type, type_text, sizeof(type_text))) {
4117-
return SWIFT_INCOMPATIBLE;
4118-
}
4119-
const char *receiver = from_chain ? cbm_ht_get_key(r->exact, type)
4120-
: swift_receiver_qn(r, file, graph, type_text, module_qn,
4121-
import_vals, import_count);
4122-
if (!receiver) {
4123-
return SWIFT_INCOMPATIBLE; /* known non-project receiver */
4124-
}
4125-
char *args = strchr(member, '(');
4126-
if (!args) {
4127-
return swift_typed_candidates(r, graph, file, module_qn, call, receiver, member, out,
4128-
out_cap);
4129-
}
4130-
char *after = swift_after_arguments(args);
4131-
if (!after || *after != '.') {
4132-
return SWIFT_INCOMPATIBLE;
4133-
}
4134-
*args = '\0';
4135-
const CBMCall *inner = swift_inner_call(file, call, (size_t)(args - expression));
4136-
type = swift_chain_return(context, inner, receiver, member, out, out_cap);
4137-
from_chain = true;
4138-
member = after + SKIP_ONE;
4205+
int found = swift_receiver_candidates(context, call, expression, type, member, out, out_cap);
4206+
/* A type inferred from an initializer call only narrows the label match:
4207+
* when it has no member with this name and these labels, keep the label
4208+
* match, as before that inference existed (#2061). */
4209+
if (found <= 0 && origin == SWIFT_ORIGIN_INFERRED && !context->strict) {
4210+
return cbm_registry_swift_candidates(r, call, module_qn, import_vals, import_count, out,
4211+
out_cap);
41394212
}
4140-
return SWIFT_INCOMPATIBLE;
4213+
return found;
41414214
}
41424215

41434216
int cbm_registry_swift_candidates_in_file(const cbm_registry_t *r, const CBMCall *call,

‎tests/test_pipeline.c‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17400,16 +17400,8 @@ TEST(pipeline_swift_capped_chain_returns_and_metatype_receivers) {
1740017400
{"CrossSuite.wrapped()", "OtherRequest.responseDecodable(of:Type,completion:()=>Void)", 0},
1740117401
{"CrossSuite.storedStream()", "Type.responseStream(stream:Handler<Int,Never>)", 1},
1740217402
{"CrossSuite.awaited()async", "Type.moveReaderIndex(forwardBy:Int)", 1},
17403-
{"CrossSuite.mixedFactory()", "Session.upload(_:Int)", 0},
17404-
{"CrossSuite.mixedFactory()", "Unrelated.upload(_:Int)", 0},
1740517403
{"nestedFactory()", "Session.upload(_:Int)", 1},
1740617404
{"nestedFactory()", "Unrelated.upload(_:Int)", 0},
17407-
{"unknownFactory()", "Session.upload(_:Int)", 0},
17408-
{"unknownFactory()", "Unrelated.upload(_:Int)", 0},
17409-
{"unknownRoot()", "Session.upload(_:Int)", 0},
17410-
{"unknownRoot()", "Unrelated.upload(_:Int)", 0},
17411-
{"cyclicFactory()", "Session.upload(_:Int)", 0},
17412-
{"cyclicFactory()", "Unrelated.upload(_:Int)", 0},
1741317405
{"divergent(_:Session)", "Request.responseDecodable(of:Type,completion:()=>Void)", 0},
1741417406
{"divergent(_:Session)", "OtherRequest.responseDecodable(of:Type,completion:()=>Void)", 0},
1741517407
{"wrongTail(_:Session)", "Request.responseDecodable(of:Type,completion:()=>Void)", 0},
@@ -17507,6 +17499,33 @@ TEST(pipeline_swift_capped_chain_returns_and_metatype_receivers) {
1750717499
cbm_node_free_fields(&caller);
1750817500
cbm_node_free_fields(&target);
1750917501
}
17502+
/* #2061: a receiver whose initializer type stays unknown (split
17503+
* overload returns, an unknown callee or root, a cycle) keeps the
17504+
* label match: the upload(1) call still gets exactly one edge. */
17505+
const char *unknown[] = {"CrossSuite.mixedFactory()", "unknownFactory()",
17506+
"unknownRoot()", "cyclicFactory()"};
17507+
for (size_t i = 0; i < sizeof(unknown) / sizeof(unknown[0]); i++) {
17508+
char qn[1024];
17509+
cbm_node_t caller = {0};
17510+
snprintf(qn, sizeof(qn), "%s.Calls.%s", project, unknown[i]);
17511+
ASSERT_EQ(cbm_store_find_node_by_qn(store, project, qn, &caller), CBM_STORE_OK);
17512+
cbm_edge_t *edges = NULL;
17513+
int edge_count = 0;
17514+
ASSERT_EQ(cbm_store_find_edges_by_source_type(store, caller.id, "CALLS", &edges,
17515+
&edge_count),
17516+
CBM_STORE_OK);
17517+
int uploads = 0;
17518+
for (int edge = 0; edge < edge_count; edge++) {
17519+
cbm_node_t target = {0};
17520+
ASSERT_EQ(cbm_store_find_node_by_id(store, edges[edge].target_id, &target),
17521+
CBM_STORE_OK);
17522+
uploads += target.name && strcmp(target.name, "upload") == 0;
17523+
cbm_node_free_fields(&target);
17524+
}
17525+
cbm_store_free_edges(edges, edge_count);
17526+
ASSERT_EQ(uploads, 1);
17527+
cbm_node_free_fields(&caller);
17528+
}
1751017529
cbm_store_close(store);
1751117530
cbm_pipeline_free(p);
1751217531
}

0 commit comments

Comments
 (0)