Skip to content

Commit 7054e4f

Browse files
author
yasmoradi
committed
fix(extraction): recover C# 14 extension block members
The vendored tree-sitter-c-sharp has no extension production, so an extension block parses as a constructor_declaration named extension whose body holds the members as local_function_statement nodes. Nothing recurses into a constructor body, and has_error stays 0, so the members were dropped with no signal at all. Emit them against the enclosing type instead. A constructor carries its own type name, so this shape is only a genuine constructor inside a type literally called extension, which keeps its own test. Fixes DeusData#2071 Signed-off-by: yasmoradi <msynk.dev@gmail.com>
1 parent 59a05eb commit 7054e4f

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5032,6 +5032,61 @@ static void extract_objc_impl_methods(CBMExtractCtx *ctx, TSNode impl_node, cons
50325032
}
50335033
}
50345034

5035+
/* True for a C# 14 extension block. The vendored tree-sitter-c-sharp has no
5036+
* `extension` production, so `extension(string text) { ... }` parses as a
5037+
* constructor_declaration named `extension` and nothing flags it: has_error
5038+
* stays 0. A constructor carries its own type's name, so this shape can only be
5039+
* a genuine constructor inside a type literally called `extension` (#2071). */
5040+
static bool csharp_is_extension_block(TSNode node, TSNode class_node, const char *source) {
5041+
if (strcmp(ts_node_type(node), "constructor_declaration") != 0) {
5042+
return false;
5043+
}
5044+
TSNode name = resolve_method_name(node, CBM_LANG_CSHARP);
5045+
if (ts_node_is_null(name)) {
5046+
return false;
5047+
}
5048+
const size_t kw_len = sizeof("extension") - 1;
5049+
uint32_t len = ts_node_end_byte(name) - ts_node_start_byte(name);
5050+
if (len != kw_len || strncmp(source + ts_node_start_byte(name), "extension", kw_len) != 0) {
5051+
return false;
5052+
}
5053+
TSNode type_name = ts_node_child_by_field_name(class_node, TS_FIELD("name"));
5054+
if (!ts_node_is_null(type_name)) {
5055+
uint32_t tlen = ts_node_end_byte(type_name) - ts_node_start_byte(type_name);
5056+
if (tlen == kw_len &&
5057+
strncmp(source + ts_node_start_byte(type_name), "extension", kw_len) == 0) {
5058+
return false;
5059+
}
5060+
}
5061+
return true;
5062+
}
5063+
5064+
/* Lift the members of a misparsed C# 14 extension block onto the enclosing
5065+
* type. They sit in the block the grammar took for a constructor body, which
5066+
* nothing recurses into, so every one of them was dropped. Methods land there
5067+
* as local_function_statement; a property in the block parses as ERROR and is
5068+
* beyond recovery without a grammar that knows the construct (#2071). */
5069+
static void extract_csharp_extension_members(CBMExtractCtx *ctx, TSNode ext_node, TSNode class_node,
5070+
const char *class_qn, const CBMLangSpec *spec) {
5071+
TSNode body = cbm_find_child_by_kind(ext_node, "block");
5072+
if (ts_node_is_null(body)) {
5073+
return;
5074+
}
5075+
uint32_t count = ts_node_child_count(body);
5076+
for (uint32_t i = 0; i < count; i++) {
5077+
TSNode member = ts_node_child(body, i);
5078+
if (ts_node_is_null(member) ||
5079+
strcmp(ts_node_type(member), "local_function_statement") != 0) {
5080+
continue;
5081+
}
5082+
TSNode name = resolve_method_name(member, ctx->language);
5083+
if (ts_node_is_null(name)) {
5084+
continue;
5085+
}
5086+
push_method_def(ctx, member, class_node, class_qn, spec, name);
5087+
}
5088+
}
5089+
50355090
// Extract methods inside a class body
50365091
static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
50375092
const CBMLangSpec *spec) {
@@ -5047,6 +5102,15 @@ static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const c
50475102
continue;
50485103
}
50495104

5105+
/* C# 14 extension block: emit the members it holds as methods of the
5106+
* enclosing type, rather than the block itself as a constructor named
5107+
* `extension` with its members buried in the body (#2071). */
5108+
if (ctx->language == CBM_LANG_CSHARP &&
5109+
csharp_is_extension_block(child, class_node, ctx->source)) {
5110+
extract_csharp_extension_members(ctx, child, class_node, class_qn, spec);
5111+
continue;
5112+
}
5113+
50505114
if (ctx->language == CBM_LANG_OBJC &&
50515115
strcmp(ts_node_type(child), "implementation_definition") == 0) {
50525116
extract_objc_impl_methods(ctx, child, class_qn, spec);

‎tests/test_extraction.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,43 @@ TEST(csharp_interface) {
772772
PASS();
773773
}
774774

775+
/* Issue #2071: a C# 14 extension block parses as a constructor_declaration
776+
* named `extension` whose body holds the members, and has_error stays 0, so the
777+
* members were dropped without any signal. They belong to the enclosing type. */
778+
TEST(csharp_extension_block_members) {
779+
CBMFileResult *r = extract("namespace App;\n"
780+
"public static class NumberExtensions\n"
781+
"{\n"
782+
" extension(IEnumerable<int> numbers)\n"
783+
" {\n"
784+
" public int SumPositive() => 0;\n"
785+
" public static int Zero() => 0;\n"
786+
" }\n"
787+
"}\n",
788+
CBM_LANG_CSHARP, "t", "NumberExtensions.cs");
789+
ASSERT_NOT_NULL(r);
790+
ASSERT_FALSE(r->has_error);
791+
ASSERT(has_def(r, "Class", "NumberExtensions"));
792+
ASSERT(has_def(r, "Method", "SumPositive"));
793+
ASSERT(has_def(r, "Method", "Zero"));
794+
ASSERT_FALSE(has_def_any(r, "extension"));
795+
cbm_free_result(r);
796+
PASS();
797+
}
798+
799+
/* The same shape is a genuine constructor inside a type actually called
800+
* `extension`, and must still be emitted (#2071). */
801+
TEST(csharp_constructor_of_type_named_extension) {
802+
CBMFileResult *r =
803+
extract("namespace App { public class extension { public extension() { } } }",
804+
CBM_LANG_CSHARP, "t", "extension.cs");
805+
ASSERT_NOT_NULL(r);
806+
ASSERT_FALSE(r->has_error);
807+
ASSERT(has_def(r, "Method", "extension"));
808+
cbm_free_result(r);
809+
PASS();
810+
}
811+
775812
/* --- Swift --- */
776813
TEST(swift_class) {
777814
CBMFileResult *r = extract("class Vehicle {\n var speed: Int = 0\n func accelerate() { "
@@ -7580,6 +7617,8 @@ SUITE(extraction) {
75807617
RUN_TEST(ruby_module);
75817618
RUN_TEST(csharp_class);
75827619
RUN_TEST(csharp_interface);
7620+
RUN_TEST(csharp_extension_block_members);
7621+
RUN_TEST(csharp_constructor_of_type_named_extension);
75837622
RUN_TEST(swift_class);
75847623
RUN_TEST(swift_protocol);
75857624
RUN_TEST(kotlin_function);

0 commit comments

Comments
 (0)