Skip to content

Commit 8087c1c

Browse files
committed
fix(swift): read a constructor type through try and await
`let socket = try ServerSocket(...)` and `let reader = try await Reader(...)` left the variable untyped, so every member call on it went to a label-only guess or was dropped. The initializer is now unwrapped from `try` / `await` before its constructor type is read. Only a bare type name counts there: `try Foo.make()` returns what make returns, which the registry still infers from the call. With the type in place the existing base-class and extension walk applies: on swift-nio socket.close(), socket.bind(to:) and socket.getLocalVsockAddress() in VsockAddressTest.swift resolve to ServerSocket, BaseSocket and the BaseSocket extension in VsockAddress.swift. Signed-off-by: Martin Vogel <[email protected]>
1 parent 8372793 commit 8087c1c

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

‎internal/cbm/extract_type_assigns.c‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,42 @@ static void swift_parameter_binding(TSNode node, TSNode *name, TSNode *type) {
219219
}
220220
}
221221

222-
static bool swift_initializer_is_call(TSNode value) {
222+
/* `try Foo(...)`, `try await make()`: the expression `try` / `await` wrap. */
223+
static TSNode swift_initializer_value(TSNode value) {
223224
while (!ts_node_is_null(value)) {
224225
const char *kind = ts_node_type(value);
225-
if (strcmp(kind, "call_expression") == 0 || strcmp(kind, "call") == 0) {
226-
return true;
227-
}
228226
if ((strcmp(kind, "try_expression") != 0 && strcmp(kind, "await_expression") != 0) ||
229227
ts_node_named_child_count(value) == 0) {
230-
return false;
228+
return value;
231229
}
232230
value = ts_node_named_child(value, ts_node_named_child_count(value) - SKIP_ONE);
233231
}
234-
return false;
232+
return value;
233+
}
234+
235+
static bool swift_initializer_is_call(TSNode value) {
236+
value = swift_initializer_value(value);
237+
if (ts_node_is_null(value)) {
238+
return false;
239+
}
240+
const char *kind = ts_node_type(value);
241+
return strcmp(kind, "call_expression") == 0 || strcmp(kind, "call") == 0;
242+
}
243+
244+
/* `Foo(...)` names the type it builds. Under `try` / `await` only a bare type
245+
* name does: `try Foo.make()` returns what make returns, which the registry
246+
* infers from the initializer call instead. */
247+
static const char *swift_constructor_type(CBMExtractCtx *ctx, TSNode value) {
248+
if (strcmp(ts_node_type(value), "array_literal") == 0) {
249+
return "[?]";
250+
}
251+
TSNode constructed = swift_initializer_value(value);
252+
const char *type =
253+
extract_constructor_type(ctx->arena, constructed, ctx->source, ctx->language);
254+
if (type && !ts_node_eq(constructed, value) && strpbrk(type, ".(")) {
255+
return NULL;
256+
}
257+
return type;
235258
}
236259

237260
// --- Unified handler ---
@@ -265,13 +288,8 @@ void handle_type_assigns(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spe
265288
}
266289
const char *type_name =
267290
ts_node_is_null(type) ? NULL : cbm_node_text(ctx->arena, type, ctx->source);
268-
if (!type_name && !parameter) {
269-
if (!ts_node_is_null(value)) {
270-
type_name = extract_constructor_type(ctx->arena, value, ctx->source, ctx->language);
271-
if (strcmp(ts_node_type(value), "array_literal") == 0) {
272-
type_name = "[?]";
273-
}
274-
}
291+
if (!type_name && !parameter && !ts_node_is_null(value)) {
292+
type_name = swift_constructor_type(ctx, value);
275293
}
276294
bool initializer_call = swift_initializer_is_call(value);
277295
if (!ts_node_is_null(name) && (type_name || initializer_call)) {

0 commit comments

Comments
 (0)