Skip to content

Commit ce6035d

Browse files
committed
test(swift): pin generic returns, try initializers and the parse retry
pipeline_swift_generic_return_keeps_label_match_issue2061: a callee's generic parameter used as its return type, colliding with a project type of the same name, leaves the variable unknown and its call keeps the label match. pipeline_swift_try_initializer_types_receiver_issue2061: `try await Reader(...)` and `try ServerSocket(...)` type the variable; a subclass receiver reaches its base class and the base class's extension in another file, not same-labelled decoy members. swift_conditional_retry_keeps_the_better_parse_issue2061: an `#error` line from a dropped `#else` branch in a class body keeps the first parse, so the class and its initializer stay in the graph. The pipeline tests run sequential and parallel. Each test fails with its fix reverted. Signed-off-by: Martin Vogel <[email protected]>
1 parent 15906eb commit ce6035d

2 files changed

Lines changed: 169 additions & 0 deletions

File tree

‎tests/test_extraction.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3917,6 +3917,56 @@ TEST(swift_conditional_type_members_keep_owners_and_source_lines) {
39173917
PASS();
39183918
}
39193919

3920+
/* #2061: masking the directives leaves the `#error` line of the dropped
3921+
* branch in the class body. That re-parse reads less of the file than the
3922+
* first one (the type and its initializer disappear), so the first parse is
3923+
* kept. */
3924+
TEST(swift_conditional_retry_keeps_the_better_parse_issue2061) {
3925+
const char *source = "protocol Locking {\n"
3926+
" func lock()\n"
3927+
" func unlock()\n"
3928+
"}\n"
3929+
"\n"
3930+
"#if canImport(Darwin)\n"
3931+
"final class SpinLock: Locking {\n"
3932+
" init() {}\n"
3933+
" func lock() {}\n"
3934+
" func unlock() {}\n"
3935+
"}\n"
3936+
"#elseif canImport(Foundation)\n"
3937+
"extension NSLock: Locking {}\n"
3938+
"#else\n"
3939+
"#error(\"No lock is available on this platform.\")\n"
3940+
"#endif\n"
3941+
"\n"
3942+
"final class Guarded<Value> {\n"
3943+
" #if canImport(Darwin)\n"
3944+
" private let lock = SpinLock()\n"
3945+
" #elseif canImport(Foundation)\n"
3946+
" private let lock = NSLock()\n"
3947+
" #else\n"
3948+
" #error(\"No lock is available on this platform.\")\n"
3949+
" #endif\n"
3950+
"\n"
3951+
" private nonisolated(unsafe) var value: Value\n"
3952+
"\n"
3953+
" init(_ value: Value) {\n"
3954+
" self.value = value\n"
3955+
" }\n"
3956+
"\n"
3957+
" func read<U>(_ closure: (Value) throws -> U) rethrows -> U { "
3958+
"try closure(value) }\n"
3959+
"}\n";
3960+
CBMFileResult *r = extract(source, CBM_LANG_SWIFT, "t", "Guarded.swift");
3961+
ASSERT_NOT_NULL(r);
3962+
ASSERT_FALSE(r->has_error);
3963+
ASSERT_NOT_NULL(find_def_in_module(r, "Guarded"));
3964+
ASSERT_NOT_NULL(find_def_in_module(r, "init(_:Value)"));
3965+
ASSERT_NOT_NULL(find_def_in_module(r, "SpinLock.init()"));
3966+
cbm_free_result(r);
3967+
PASS();
3968+
}
3969+
39203970
TEST(swift_uncapped_selectors_keep_nested_return_types) {
39213971
CBMFileResult *r = extract("func generic(_ value: Int) -> Box<Result> { fatalError() }\n"
39223972
"func callback() -> (Int) -> Result { fatalError() }\n",
@@ -15818,6 +15868,7 @@ SUITE(extraction) {
1581815868
RUN_TEST(swift_method_call);
1581915869
RUN_TEST(swift_type_imports_preserve_markers_and_complete_file_imports);
1582015870
RUN_TEST(swift_conditional_type_members_keep_owners_and_source_lines);
15871+
RUN_TEST(swift_conditional_retry_keeps_the_better_parse_issue2061);
1582115872
RUN_TEST(swift_uncapped_selectors_keep_nested_return_types);
1582215873
RUN_TEST(swift_wrapped_trailing_closure_keeps_one_complete_call);
1582315874
RUN_TEST(swift_selector_expands_only_lexical_function_aliases);

‎tests/test_pipeline.c‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17535,6 +17535,122 @@ TEST(pipeline_swift_capped_chain_returns_and_metatype_receivers) {
1753517535
PASS();
1753617536
}
1753717537

17538+
/* One Swift corpus, sequential and parallel: every (caller, target) pair has
17539+
* the expected CALLS edge count. Callers and targets are "<file stem>.<QN>". */
17540+
typedef struct {
17541+
const char *caller;
17542+
const char *target;
17543+
int expected;
17544+
} swift_call_pair_t;
17545+
17546+
static int swift_call_pairs_case(const char *const *files, int file_count,
17547+
const swift_call_pair_t *pairs, int pair_count) {
17548+
for (int parallel = 0; parallel < 2; parallel++) {
17549+
char tmp[256] = "/tmp/cbm_swift_pairs_XXXXXX";
17550+
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
17551+
for (int i = 0; i < file_count; i++) {
17552+
write_temp_file(tmp, files[i * 2], files[i * 2 + 1]);
17553+
}
17554+
for (int i = 0; parallel && i < 55; i++) {
17555+
char path[64];
17556+
snprintf(path, sizeof(path), "Pad%d.swift", i);
17557+
write_temp_file(tmp, path, "// filler\n");
17558+
}
17559+
XlangEnvSave saved = xlang_env_select(parallel != 0);
17560+
char db[512];
17561+
snprintf(db, sizeof(db), "%s/graph.db", tmp);
17562+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);
17563+
ASSERT_NOT_NULL(p);
17564+
ASSERT_EQ(cbm_pipeline_run(p), 0);
17565+
cbm_store_t *store = cbm_store_open_path(db);
17566+
ASSERT_NOT_NULL(store);
17567+
const char *project = cbm_pipeline_project_name(p);
17568+
for (int i = 0; i < pair_count; i++) {
17569+
char qn[1024];
17570+
cbm_node_t caller = {0}, target = {0};
17571+
snprintf(qn, sizeof(qn), "%s.%s", project, pairs[i].caller);
17572+
ASSERT_EQ(cbm_store_find_node_by_qn(store, project, qn, &caller), CBM_STORE_OK);
17573+
snprintf(qn, sizeof(qn), "%s.%s", project, pairs[i].target);
17574+
ASSERT_EQ(cbm_store_find_node_by_qn(store, project, qn, &target), CBM_STORE_OK);
17575+
ASSERT_EQ(pipeline_has_calls_edge(store, caller.id, target.id), pairs[i].expected);
17576+
cbm_node_free_fields(&caller);
17577+
cbm_node_free_fields(&target);
17578+
}
17579+
cbm_store_close(store);
17580+
cbm_pipeline_free(p);
17581+
xlang_env_restore(&saved);
17582+
th_rmtree(tmp);
17583+
}
17584+
PASS();
17585+
}
17586+
17587+
/* #2061: `func run<Result>(...) -> Result` returns its own generic parameter,
17588+
* which names no type even where the project declares a `Result`. The bound
17589+
* variable stays unknown and its call keeps the label match, as it did before
17590+
* initializer types were inferred. */
17591+
TEST(pipeline_swift_generic_return_keeps_label_match_issue2061) {
17592+
const char *files[] = {
17593+
"Types.swift",
17594+
"struct Result { func salWait(context: Int) -> Int { 0 } }\n"
17595+
"class Context {\n"
17596+
" func run<Result>(body: () -> Result) -> Result { body() }\n"
17597+
" func make<Element>(body: () -> Element) -> Element { body() }\n"
17598+
"}\n",
17599+
"Calls.swift",
17600+
"class Waiter {\n"
17601+
" func salWait(context: Int) -> Int { 0 }\n"
17602+
" func drain(count: Int) -> Int { 0 }\n"
17603+
"}\n"
17604+
"func generic(_ context: Context) { let result = context.run(body: { Waiter() }); "
17605+
"_ = result.salWait(context: 1) }\n"
17606+
"func element(_ context: Context) { let value = context.make(body: { Waiter() }); "
17607+
"_ = value.drain(count: 1) }\n",
17608+
};
17609+
const swift_call_pair_t pairs[] = {
17610+
{"Calls.generic(_:Context)", "Calls.Waiter.salWait(context:Int)=>Int", 1},
17611+
{"Calls.generic(_:Context)", "Types.Result.salWait(context:Int)=>Int", 0},
17612+
{"Calls.element(_:Context)", "Calls.Waiter.drain(count:Int)=>Int", 1},
17613+
};
17614+
return swift_call_pairs_case(files, 2, pairs, (int)(sizeof(pairs) / sizeof(pairs[0])));
17615+
}
17616+
17617+
/* #2061: `try` / `try await` around a constructor still names the receiver's
17618+
* type, and a subclass receiver reaches its base class and the base class's
17619+
* extension in another file. The Decoy members match the same labels, so a
17620+
* label match alone would land on them. */
17621+
TEST(pipeline_swift_try_initializer_types_receiver_issue2061) {
17622+
const char *files[] = {
17623+
"Types.swift",
17624+
"class Reader {\n"
17625+
" init(path: String) async throws {}\n"
17626+
" func next(count: Int) -> Int { 0 }\n"
17627+
"}\n"
17628+
"class BaseSocket { func close() {} }\n"
17629+
"class ServerSocket: BaseSocket { init(port: Int) throws {} }\n",
17630+
"Vsock.swift",
17631+
"extension BaseSocket { func localVsockAddress() -> Int { 0 } }\n",
17632+
"Calls.swift",
17633+
"class Decoy {\n"
17634+
" func next(count: Int) -> Int { 0 }\n"
17635+
" func localVsockAddress() -> Int { 0 }\n"
17636+
" func close() {}\n"
17637+
"}\n"
17638+
"func awaited() async throws { let reader = try await Reader(path: \"x\"); "
17639+
"_ = reader.next(count: 1) }\n"
17640+
"func vsock() throws { let socket = try ServerSocket(port: 1); "
17641+
"_ = socket.localVsockAddress(); socket.close() }\n",
17642+
};
17643+
const swift_call_pair_t pairs[] = {
17644+
{"Calls.awaited()async", "Types.Reader.next(count:Int)=>Int", 1},
17645+
{"Calls.awaited()async", "Calls.Decoy.next(count:Int)=>Int", 0},
17646+
{"Calls.vsock()", "Vsock.BaseSocket.localVsockAddress()=>Int", 1},
17647+
{"Calls.vsock()", "Calls.Decoy.localVsockAddress()=>Int", 0},
17648+
{"Calls.vsock()", "Types.BaseSocket.close()", 1},
17649+
{"Calls.vsock()", "Calls.Decoy.close()", 0},
17650+
};
17651+
return swift_call_pairs_case(files, 3, pairs, (int)(sizeof(pairs) / sizeof(pairs[0])));
17652+
}
17653+
1753817654
TEST(pipeline_swift_return_identity_issue2061) {
1753917655
const char *returns[] = {"pick()=>Int", "pick()=>String"};
1754017656
return swift_declaration_identity_case(
@@ -33663,6 +33779,8 @@ SUITE(pipeline) {
3366333779
RUN_TEST(pipeline_swift_return_identity_issue2061);
3366433780
RUN_TEST(pipeline_swift_receiver_and_chained_closures_issue2061);
3366533781
RUN_TEST(pipeline_swift_capped_chain_returns_and_metatype_receivers);
33782+
RUN_TEST(pipeline_swift_generic_return_keeps_label_match_issue2061);
33783+
RUN_TEST(pipeline_swift_try_initializer_types_receiver_issue2061);
3366633784
RUN_TEST(pipeline_swift_default_before_required_candidates_issue2061);
3366733785
RUN_TEST(pipeline_swift_incompatible_overload_unresolved_issue2061);
3366833786
RUN_TEST(pipeline_swift_incompatible_overload_parallel_fallback_issue2061);

0 commit comments

Comments
 (0)