Skip to content

Commit 102c564

Browse files
committed
fix(swift): keep the label match for a constructor-typed receiver
A receiver typed only by its constructor call (`let x = Foo()`, also through `try` / `await`) now narrows the label match the way a receiver with an inferred initializer type already does: a member the type has still wins, and when the type has no member with the call's name and labels, the call keeps the label match instead of getting no edge. One such site is Alamofire's Session.swift:1389, where `mutableState` is a `Protected(...)` whose members the parser leaves as free functions. This applies only to a type the project declares. A constructor of a type the project does not know (DispatchSemaphore(), Task {...}) or only extends (Data()) still gets no edge: on swift-nio and Alamofire a label match for those receivers mostly lands on unrelated project methods, for example `semaphore.wait()` on EventLoopFuture.wait. A name the extractor reads as a constructor but that resolves to a project function (`Foo.make()`) keeps the label match like an inferred call result. Annotations and parameters still decide alone, and inferring an initializer's type still never uses a label match. The extractor marks a binding as `constructed` when its type comes from the initializer's constructor call rather than from an annotation. On swift-nio eabcc14f and Alamofire bda9ed57 this adds 947 and 166 call edges and removes none. pipeline_swift_constructor_receiver_keeps_label_match_issue2061 (sequential and parallel) pins the fallback, a member that still wins, and the annotated, parameter, unknown-type and extended-type cases. Signed-off-by: Martin Vogel <[email protected]>
1 parent bbd727f commit 102c564

4 files changed

Lines changed: 88 additions & 8 deletions

File tree

‎internal/cbm/cbm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ typedef struct {
579579
const char *enclosing_func_qn; // enclosing function, class, or module QN
580580
uint32_t value_start_byte; // Swift call initializer span; type_name may be unknown
581581
uint32_t value_end_byte; // exclusive; zero when no initializer call was recorded
582+
bool constructed; // Swift: type_name is the type the initializer call
583+
// constructs (`Foo(...)`), not an annotation or parameter
582584
} CBMTypeAssign;
583585

584586
// String reference: URL, config key, or async target found in source.

‎internal/cbm/extract_type_assigns.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ void handle_type_assigns(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spe
301301
.var_name = cbm_node_text(ctx->arena, name, ctx->source),
302302
.type_name = type_name,
303303
.enclosing_func_qn = scope,
304+
/* No annotation: the type is the one the initializer call constructs. */
305+
.constructed = type_name && initializer_call && ts_node_is_null(type),
304306
};
305307
if (initializer_call) {
306308
assignment.value_start_byte = ts_node_start_byte(value);

‎src/pipeline/registry.c‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3688,10 +3688,15 @@ static const char *swift_binding_cached_type(swift_binding_work_t *work,
36883688
return "";
36893689
}
36903690

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
3691+
/* Where a receiver's type came from: no binding, a declaration (an annotation
3692+
* or a parameter), a constructor call, or only an initializer whose type was
36933693
* inferred from the call it makes. */
3694-
typedef enum { SWIFT_ORIGIN_NONE, SWIFT_ORIGIN_DECLARED, SWIFT_ORIGIN_INFERRED } swift_origin_t;
3694+
typedef enum {
3695+
SWIFT_ORIGIN_NONE,
3696+
SWIFT_ORIGIN_DECLARED,
3697+
SWIFT_ORIGIN_CONSTRUCTED,
3698+
SWIFT_ORIGIN_INFERRED
3699+
} swift_origin_t;
36953700

36963701
/* Conflicting block-local bindings ("") select no same-name method on either
36973702
* type. A binding whose initializer type could not be inferred leaves the
@@ -3721,7 +3726,12 @@ static const char *swift_binding_type(const swift_inference_t *context, swift_bi
37213726
}
37223727
const char *candidate = a->type_name;
37233728
if (candidate) {
3724-
*origin = SWIFT_ORIGIN_DECLARED;
3729+
/* A declaration among the bindings decides; else a constructor. */
3730+
if (!a->constructed) {
3731+
*origin = SWIFT_ORIGIN_DECLARED;
3732+
} else if (*origin != SWIFT_ORIGIN_DECLARED) {
3733+
*origin = SWIFT_ORIGIN_CONSTRUCTED;
3734+
}
37253735
} else {
37263736
candidate = swift_binding_cached_type(work, a);
37273737
if (!candidate[0]) {
@@ -4169,6 +4179,33 @@ static int swift_receiver_candidates(const swift_inference_t *context, const CBM
41694179
return SWIFT_INCOMPATIBLE;
41704180
}
41714181

4182+
/* A type that only an initializer gives narrows the label match: when it has
4183+
* no member with this name and these labels, the call keeps the label match,
4184+
* as before initializer types were read (#2061). A constructor counts unless
4185+
* the project does not declare its type: DispatchSemaphore() and Task {...}
4186+
* name types it does not know, Data() one it only extends, and a label match
4187+
* would guess at members the project does not have. */
4188+
static bool swift_keeps_label_match(const swift_inference_t *context, swift_origin_t origin,
4189+
const char *type) {
4190+
if (context->strict) {
4191+
return false; /* Inferring an initializer's type: a label match is a guess. */
4192+
}
4193+
if (origin != SWIFT_ORIGIN_CONSTRUCTED) {
4194+
return origin == SWIFT_ORIGIN_INFERRED;
4195+
}
4196+
char type_text[CBM_SZ_4K];
4197+
const char *qn =
4198+
swift_receiver_type_text(type, type_text, sizeof(type_text))
4199+
? swift_receiver_qn(context->registry, context->file, context->graph, type_text,
4200+
context->module_qn, context->imports, context->import_count)
4201+
: NULL;
4202+
const swift_owners_t *index = context->registry->swift_owners;
4203+
const swift_owner_t *owner = qn && index ? cbm_ht_get(index->by_qn, qn) : NULL;
4204+
/* No owner: `Foo.make()` read as a constructor names a function, whose
4205+
* result keeps the label match like an inferred one. */
4206+
return qn && (!owner || (owner->primary && owner->primary->declaration));
4207+
}
4208+
41724209
static int swift_candidates_for_file(const swift_inference_t *context, swift_binding_work_t *work,
41734210
const CBMCall *call, const char **out, int out_cap) {
41744211
const cbm_registry_t *r = context->registry;
@@ -4203,10 +4240,7 @@ static int swift_candidates_for_file(const swift_inference_t *context, swift_bin
42034240
out_cap);
42044241
}
42054242
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) {
4243+
if (found <= 0 && swift_keeps_label_match(context, origin, type)) {
42104244
return cbm_registry_swift_candidates(r, call, module_qn, import_vals, import_count, out,
42114245
out_cap);
42124246
}

‎tests/test_pipeline.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17753,6 +17753,47 @@ TEST(pipeline_swift_try_initializer_types_receiver_issue2061) {
1775317753
return swift_call_pairs_case(files, 3, pairs, (int)(sizeof(pairs) / sizeof(pairs[0])));
1775417754
}
1775517755

17756+
/* #2061: a project type that only a constructor call gives (`Foo()`,
17757+
* `try Foo(...)`) narrows the label match like an inferred one: a member the
17758+
* type has wins over a same-label member elsewhere, and a member it lacks
17759+
* keeps the label match. An annotation or a parameter still decides alone,
17760+
* and so does a constructor of a type the project does not declare, whether
17761+
* it does not know the type (NSLock) or only extends it (Data). */
17762+
TEST(pipeline_swift_constructor_receiver_keeps_label_match_issue2061) {
17763+
const char *files[] = {
17764+
"Types.swift",
17765+
"class Store {\n"
17766+
" init() {}\n"
17767+
" init(path: String) throws {}\n"
17768+
" func write(count: Int) {}\n"
17769+
"}\n"
17770+
"extension Data { func checksum() -> Int { 0 } }\n",
17771+
"Calls.swift",
17772+
"class Reader {\n"
17773+
" func read(count: Int) -> Int { 0 }\n"
17774+
" func write(count: Int) {}\n"
17775+
"}\n"
17776+
"func constructed() { let store = Store(); _ = store.read(count: 1); "
17777+
"store.write(count: 1) }\n"
17778+
"func tried() throws { let store = try Store(path: \"x\"); _ = store.read(count: 1) }\n"
17779+
"func annotated() { let store: Store = Store(); _ = store.read(count: 1) }\n"
17780+
"func parameter(_ store: Store) { _ = store.read(count: 1) }\n"
17781+
"func external() { let lock = NSLock(); _ = lock.read(count: 1) }\n"
17782+
"func extended() { let data = Data(); _ = data.read(count: 1) }\n",
17783+
};
17784+
const swift_call_pair_t pairs[] = {
17785+
{"Calls.constructed()", "Calls.Reader.read(count:Int)=>Int", 1},
17786+
{"Calls.constructed()", "Types.Store.write(count:Int)", 1},
17787+
{"Calls.constructed()", "Calls.Reader.write(count:Int)", 0},
17788+
{"Calls.tried()", "Calls.Reader.read(count:Int)=>Int", 1},
17789+
{"Calls.annotated()", "Calls.Reader.read(count:Int)=>Int", 0},
17790+
{"Calls.parameter(_:Store)", "Calls.Reader.read(count:Int)=>Int", 0},
17791+
{"Calls.external()", "Calls.Reader.read(count:Int)=>Int", 0},
17792+
{"Calls.extended()", "Calls.Reader.read(count:Int)=>Int", 0},
17793+
};
17794+
return swift_call_pairs_case(files, 2, pairs, (int)(sizeof(pairs) / sizeof(pairs[0])));
17795+
}
17796+
1775617797
TEST(pipeline_swift_return_identity_issue2061) {
1775717798
const char *returns[] = {"pick()=>Int", "pick()=>String"};
1775817799
return swift_declaration_identity_case(
@@ -33883,6 +33924,7 @@ SUITE(pipeline) {
3388333924
RUN_TEST(pipeline_swift_capped_chain_returns_and_metatype_receivers);
3388433925
RUN_TEST(pipeline_swift_generic_return_keeps_label_match_issue2061);
3388533926
RUN_TEST(pipeline_swift_try_initializer_types_receiver_issue2061);
33927+
RUN_TEST(pipeline_swift_constructor_receiver_keeps_label_match_issue2061);
3388633928
RUN_TEST(pipeline_swift_default_before_required_candidates_issue2061);
3388733929
RUN_TEST(pipeline_swift_incompatible_overload_unresolved_issue2061);
3388833930
RUN_TEST(pipeline_swift_incompatible_overload_parallel_fallback_issue2061);

0 commit comments

Comments
 (0)