Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/cbm/cbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ typedef struct {
const char *enclosing_func_qn; // enclosing function, class, or module QN
uint32_t value_start_byte; // Swift call initializer span; type_name may be unknown
uint32_t value_end_byte; // exclusive; zero when no initializer call was recorded
uint32_t decl_end_byte; // Swift: the binding is in scope from the end of its
uint32_t scope_end_byte; // declaration to the end of its block; zero = unknown
bool constructed; // Swift: type_name is the type the initializer call
// constructs (`Foo(...)`), not an annotation or parameter
} CBMTypeAssign;
Expand Down
6 changes: 6 additions & 0 deletions internal/cbm/extract_type_assigns.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,13 @@ void handle_type_assigns(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spe
.enclosing_func_qn = scope,
/* No annotation: the type is the one the initializer call constructs. */
.constructed = type_name && initializer_call && ts_node_is_null(type),
.decl_end_byte = ts_node_end_byte(node),
};
/* A parameter's block is its function; a local's is its statements. */
TSNode block = ts_node_parent(node);
if (!ts_node_is_null(block)) {
assignment.scope_end_byte = ts_node_end_byte(block);
}
if (initializer_call) {
assignment.value_start_byte = ts_node_start_byte(value);
assignment.value_end_byte = ts_node_end_byte(value);
Expand Down
48 changes: 38 additions & 10 deletions src/pipeline/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -3811,14 +3811,24 @@ static int swift_binding_rank(const char *binding, const char *scope, const char
return 0;
}

/* A block-local binding names the variable only where it is in scope: after
* its declaration and inside its block. `var request = try request.make()`
* reads the parameter on its right-hand side and the new variable after it. */
static int swift_assignment_rank(const CBMTypeAssign *assignment, const char *name,
const char *scope, const char *class_qn, const char *module_qn,
const CBMCall *call, const char *class_qn, const char *module_qn,
bool field) {
if (!assignment->var_name || !assignment->enclosing_func_qn ||
strcmp(assignment->var_name, name) != 0) {
return 0;
}
return swift_binding_rank(assignment->enclosing_func_qn, scope, class_qn, module_qn, field);
int rank = swift_binding_rank(assignment->enclosing_func_qn, call->enclosing_func_qn, class_qn,
module_qn, field);
if (rank == SWIFT_SCOPE_LOCAL && assignment->scope_end_byte &&
(call->site_start_byte < assignment->decl_end_byte ||
call->site_start_byte >= assignment->scope_end_byte)) {
return 0;
}
return rank;
}

static int swift_cached_binding(const swift_binding_work_t *work, const CBMTypeAssign *assignment) {
Expand Down Expand Up @@ -3852,17 +3862,33 @@ typedef enum {
SWIFT_ORIGIN_INFERRED
} swift_origin_t;

/* Conflicting block-local bindings ("") select no same-name method on either
/* Of the block-local bindings in scope at the call, the innermost block's: a
* local shadows a same-name local of an enclosing block, and a parameter,
* whose block is its whole function. Zero when no binding records a block. */
static uint32_t swift_innermost_block(const CBMFileResult *file, const char *name,
const CBMCall *call) {
uint32_t block = 0;
for (int i = 0; i < file->type_assigns.count; i++) {
const CBMTypeAssign *a = &file->type_assigns.items[i];
if (a->scope_end_byte && (!block || a->scope_end_byte < block) &&
swift_assignment_rank(a, name, call, NULL, NULL, false) == SWIFT_SCOPE_LOCAL) {
block = a->scope_end_byte;
}
}
return block;
}

/* Conflicting bindings of one block ("") select no same-name method on either
* type. A binding whose initializer type could not be inferred leaves the
* receiver unknown (NULL), so the call keeps the label match (#2061). */
static const char *swift_binding_type(const swift_inference_t *context, swift_binding_work_t *work,
const char *name, const char *scope, const char *class_qn,
const char *name, const CBMCall *call, const char *class_qn,
const char *module_qn, bool field, swift_origin_t *origin) {
const CBMFileResult *file = context->file;
int best = 0;
for (int i = 0; i < file->type_assigns.count; i++) {
const CBMTypeAssign *a = &file->type_assigns.items[i];
int rank = swift_assignment_rank(a, name, scope, class_qn, module_qn, field);
int rank = swift_assignment_rank(a, name, call, class_qn, module_qn, field);
if (rank > best) {
best = rank;
}
Expand All @@ -3871,11 +3897,13 @@ static const char *swift_binding_type(const swift_inference_t *context, swift_bi
if (!best) {
return NULL;
}
uint32_t block = best == SWIFT_SCOPE_LOCAL ? swift_innermost_block(file, name, call) : 0;
const char *type = NULL;
bool unknown = false;
for (int i = 0; i < file->type_assigns.count; i++) {
const CBMTypeAssign *a = &file->type_assigns.items[i];
if (swift_assignment_rank(a, name, scope, class_qn, module_qn, field) != best) {
if (swift_assignment_rank(a, name, call, class_qn, module_qn, field) != best ||
(block && a->scope_end_byte && a->scope_end_byte != block)) {
continue;
}
const char *candidate = a->type_name;
Expand Down Expand Up @@ -3929,13 +3957,13 @@ static const char *swift_file_receiver(const swift_inference_t *context, swift_b
return class_qn;
}
*dot = '\0';
const char *type = swift_binding_type(context, work, *member, call->enclosing_func_qn,
class_qn, module_qn, true, origin);
const char *type =
swift_binding_type(context, work, *member, call, class_qn, module_qn, true, origin);
*member = dot + SKIP_ONE;
return type;
}
const char *type = swift_binding_type(context, work, expression, call->enclosing_func_qn,
class_qn, module_qn, false, origin);
const char *type =
swift_binding_type(context, work, expression, call, class_qn, module_qn, false, origin);
if (*origin == SWIFT_ORIGIN_NONE && isupper((unsigned char)expression[0])) {
*origin = SWIFT_ORIGIN_DECLARED;
return expression;
Expand Down
57 changes: 57 additions & 0 deletions tests/test_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -18296,6 +18296,62 @@ TEST(pipeline_swift_constructor_receiver_keeps_label_match_issue2061) {
return swift_call_pairs_case(files, 2, pairs, (int)(sizeof(pairs) / sizeof(pairs[0])));
}

/* #2436 follow-up: a block-local binding names the variable only where it is
* in scope. The inner `semaphore`, whose type is not inferred, ends with its
* block, so the outer call is typed by the outer annotation and gets no label
* match to Decoy.wait(). `var request = try request.asRequest()` (Alamofire's
* encoders) reads the parameter on its right and the new variable after it,
* whose call keeps its label match. Sibling blocks each type their own
* `store`, so neither call reaches the Decoy. */
TEST(pipeline_swift_in_scope_binding_issue2436) {
const char *files[] = {
"Types.swift",
"class Decoy {\n"
" func wait() {}\n"
" func write(count: Int) {}\n"
" func read(count: Int) {}\n"
"}\n"
"func identity<T>(_ value: T) -> T { value }\n"
"protocol RequestConvertible { func asRequest() throws -> Request }\n"
"struct Request { func validate(code: Int) {} }\n"
"class Store { func write(count: Int) {} }\n"
"class Reader { func read(count: Int) {} }\n",
"Calls.swift",
"import Foundation\n"
"func declaredShadow(_ flag: Bool) {\n"
" let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)\n"
" if flag {\n"
" let semaphore = identity(DispatchSemaphore(value: 0))\n"
" semaphore.signal()\n"
" }\n"
" semaphore.wait()\n"
"}\n"
"func encode(_ request: any RequestConvertible) throws -> Request {\n"
" var request = try request.asRequest()\n"
" request.validate(code: 1)\n"
" return request\n"
"}\n"
"func siblings(_ flag: Bool) {\n"
" if flag {\n"
" let store = Store()\n"
" store.write(count: 1)\n"
" } else {\n"
" let store = Reader()\n"
" store.read(count: 2)\n"
" }\n"
"}\n",
};
const swift_call_pair_t pairs[] = {
{"Calls.declaredShadow(_:Bool)", "Types.Decoy.wait()", 0},
{"Calls.encode(_:any RequestConvertible)=>Request", "Types.Request.validate(code:Int)", 1},
{"Calls.siblings(_:Bool)", "Types.Store.write(count:Int)", 1},
{"Calls.siblings(_:Bool)", "Types.Reader.read(count:Int)", 1},
{"Calls.siblings(_:Bool)", "Types.Decoy.write(count:Int)", 0},
{"Calls.siblings(_:Bool)", "Types.Decoy.read(count:Int)", 0},
};
return swift_call_pairs_case(files, 2, pairs, (int)(sizeof(pairs) / sizeof(pairs[0])));
}

TEST(pipeline_swift_return_identity_issue2061) {
const char *returns[] = {"pick()=>Int", "pick()=>String"};
return swift_declaration_identity_case(
Expand Down Expand Up @@ -34432,6 +34488,7 @@ SUITE(pipeline) {
RUN_TEST(pipeline_swift_generic_return_keeps_label_match_issue2061);
RUN_TEST(pipeline_swift_try_initializer_types_receiver_issue2061);
RUN_TEST(pipeline_swift_constructor_receiver_keeps_label_match_issue2061);
RUN_TEST(pipeline_swift_in_scope_binding_issue2436);
RUN_TEST(pipeline_swift_default_before_required_candidates_issue2061);
RUN_TEST(pipeline_swift_incompatible_overload_unresolved_issue2061);
RUN_TEST(pipeline_swift_incompatible_overload_parallel_fallback_issue2061);
Expand Down
Loading