Skip to content

Commit e2f93ba

Browse files
authored
Merge pull request DeusData#2231 from yasmoradi/fix/csharp14-extension-members
fix(extraction): recover C# 14 extension block members
2 parents 4a1e6d1 + dad6a01 commit e2f93ba

4 files changed

Lines changed: 174 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8923,6 +8923,68 @@ static void extract_objc_impl_methods(CBMExtractCtx *ctx, TSNode impl_node, cons
89238923
}
89248924
}
89258925

8926+
/* True for a C# 14 extension block. The vendored tree-sitter-c-sharp has no
8927+
* `extension` production, so `extension(string text) { ... }` parses as a
8928+
* constructor_declaration named `extension` and nothing flags it: has_error
8929+
* stays 0. A constructor carries its own type's name, so this shape can only be
8930+
* a genuine constructor inside a type literally called `extension` (#2071). */
8931+
static bool csharp_is_extension_block(TSNode node, TSNode class_node, const char *source) {
8932+
if (strcmp(ts_node_type(node), "constructor_declaration") != 0) {
8933+
return false;
8934+
}
8935+
TSNode name = resolve_method_name(node, CBM_LANG_CSHARP);
8936+
if (ts_node_is_null(name)) {
8937+
return false;
8938+
}
8939+
const size_t kw_len = sizeof("extension") - 1;
8940+
uint32_t len = ts_node_end_byte(name) - ts_node_start_byte(name);
8941+
if (len != kw_len || strncmp(source + ts_node_start_byte(name), "extension", kw_len) != 0) {
8942+
return false;
8943+
}
8944+
TSNode type_name = ts_node_child_by_field_name(class_node, TS_FIELD("name"));
8945+
if (!ts_node_is_null(type_name)) {
8946+
const char *type_text = source + ts_node_start_byte(type_name);
8947+
uint32_t tlen = ts_node_end_byte(type_name) - ts_node_start_byte(type_name);
8948+
/* A verbatim identifier names the same type: class @extension may
8949+
* declare a genuine constructor spelled extension(). */
8950+
if (tlen > 0 && type_text[0] == '@') {
8951+
type_text++;
8952+
tlen--;
8953+
}
8954+
if (tlen == kw_len && strncmp(type_text, "extension", kw_len) == 0) {
8955+
return false;
8956+
}
8957+
}
8958+
return true;
8959+
}
8960+
8961+
/* Lift the members of a misparsed C# 14 extension block onto the enclosing
8962+
* type. They sit in the block the grammar took for a constructor body, which
8963+
* nothing recurses into, so every one of them was dropped. Methods land there
8964+
* as local_function_statement; a property in the block parses as ERROR and is
8965+
* beyond recovery without a grammar that knows the construct (#2071). */
8966+
static void extract_csharp_extension_members(CBMExtractCtx *ctx, TSNode ext_node, TSNode class_node,
8967+
const char *class_qn, const CBMLangSpec *spec) {
8968+
TSNode body = cbm_find_child_by_kind(ext_node, "block");
8969+
if (ts_node_is_null(body)) {
8970+
return;
8971+
}
8972+
member_iter_t it;
8973+
member_iter_init(&it, body, ctx->language, false);
8974+
TSNode member;
8975+
while (member_iter_next(&it, &member)) {
8976+
if (strcmp(ts_node_type(member), "local_function_statement") != 0) {
8977+
continue;
8978+
}
8979+
TSNode name = resolve_method_name(member, ctx->language);
8980+
if (ts_node_is_null(name)) {
8981+
continue;
8982+
}
8983+
push_method_def(ctx, member, class_node, class_qn, spec, name);
8984+
}
8985+
member_iter_done(&it);
8986+
}
8987+
89268988
// Extract methods inside a class body
89278989
static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
89288990
const CBMLangSpec *spec) {
@@ -8936,6 +8998,15 @@ static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const c
89368998
TSNode child;
89378999
while (member_iter_next(&it, &child)) {
89389000

9001+
/* C# 14 extension block: emit the members it holds as methods of the
9002+
* enclosing type, rather than the block itself as a constructor named
9003+
* `extension` with its members buried in the body (#2071). */
9004+
if (ctx->language == CBM_LANG_CSHARP &&
9005+
csharp_is_extension_block(child, class_node, ctx->source)) {
9006+
extract_csharp_extension_members(ctx, child, class_node, class_qn, spec);
9007+
continue;
9008+
}
9009+
89399010
if (ctx->language == CBM_LANG_OBJC &&
89409011
strcmp(ts_node_type(child), "implementation_definition") == 0) {
89419012
extract_objc_impl_methods(ctx, child, class_qn, spec);

‎tests/test_extraction.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,89 @@ TEST(csharp_interface) {
879879
PASS();
880880
}
881881

882+
/* Issue #2071: a C# 14 extension block parses as a constructor_declaration
883+
* named `extension` whose body holds the members, and has_error stays 0, so the
884+
* members were dropped without any signal. They belong to the enclosing type. */
885+
TEST(csharp_extension_block_members) {
886+
CBMFileResult *r = extract("namespace App;\n"
887+
"public static class NumberExtensions\n"
888+
"{\n"
889+
" extension(IEnumerable<int> numbers)\n"
890+
" {\n"
891+
" public int SumPositive() => 0;\n"
892+
" public static int Zero() => 0;\n"
893+
" }\n"
894+
"}\n",
895+
CBM_LANG_CSHARP, "t", "NumberExtensions.cs");
896+
ASSERT_NOT_NULL(r);
897+
ASSERT_FALSE(r->has_error);
898+
ASSERT(has_def(r, "Class", "NumberExtensions"));
899+
ASSERT(has_def(r, "Method", "SumPositive"));
900+
ASSERT(has_def(r, "Method", "Zero"));
901+
ASSERT_FALSE(has_def_any(r, "extension"));
902+
cbm_free_result(r);
903+
PASS();
904+
}
905+
906+
/* The same shape is a genuine constructor inside a type actually called
907+
* `extension`, and must still be emitted (#2071). */
908+
TEST(csharp_constructor_of_type_named_extension) {
909+
CBMFileResult *r =
910+
extract("namespace App { public class extension { public extension() { } } }",
911+
CBM_LANG_CSHARP, "t", "extension.cs");
912+
ASSERT_NOT_NULL(r);
913+
ASSERT_FALSE(r->has_error);
914+
ASSERT(has_def(r, "Method", "extension"));
915+
cbm_free_result(r);
916+
PASS();
917+
}
918+
919+
/* Escaping a type identifier does not change its constructor's identity. */
920+
TEST(csharp_constructor_of_escaped_type_named_extension) {
921+
const char *src = "namespace App { public class @extension { "
922+
"public extension() { int ConstructorLocal() => 1; } } }";
923+
CBMFileResult *r = extract(src, CBM_LANG_CSHARP, "t", "extension.cs");
924+
ASSERT_NOT_NULL(r);
925+
ASSERT_FALSE(r->has_error);
926+
ASSERT(has_def(r, "Method", "extension"));
927+
ASSERT_FALSE(has_def(r, "Method", "ConstructorLocal"));
928+
cbm_free_result(r);
929+
PASS();
930+
}
931+
932+
/* All conditional branches expose extension members, but method-local
933+
* functions must never be promoted to members of the enclosing type. */
934+
TEST(csharp_extension_block_conditional_members) {
935+
const char *src = "public static class TextExtensions\n"
936+
"{\n"
937+
" extension(string text)\n"
938+
" {\n"
939+
"#if FIRST\n"
940+
" public int First() { int Nested() => 1; return Nested(); }\n"
941+
"#elif SECOND\n"
942+
" public int Second() => 2;\n"
943+
"#else\n"
944+
"#if NESTED\n"
945+
" public int Third() => 3;\n"
946+
"#else\n"
947+
" public int Fourth() => 4;\n"
948+
"#endif\n"
949+
"#endif\n"
950+
" }\n"
951+
"}\n";
952+
CBMFileResult *r = extract(src, CBM_LANG_CSHARP, "t", "TextExtensions.cs");
953+
ASSERT_NOT_NULL(r);
954+
ASSERT_FALSE(r->has_error);
955+
ASSERT(has_def(r, "Method", "First"));
956+
ASSERT(has_def(r, "Method", "Second"));
957+
ASSERT(has_def(r, "Method", "Third"));
958+
ASSERT(has_def(r, "Method", "Fourth"));
959+
ASSERT_FALSE(has_def_any(r, "extension"));
960+
ASSERT_FALSE(has_def(r, "Method", "Nested"));
961+
cbm_free_result(r);
962+
PASS();
963+
}
964+
882965
/* --- Swift --- */
883966
TEST(swift_class) {
884967
CBMFileResult *r = extract("class Vehicle {\n var speed: Int = 0\n func accelerate() { "
@@ -13156,6 +13239,10 @@ SUITE(extraction) {
1315613239
RUN_TEST(ruby_module);
1315713240
RUN_TEST(csharp_class);
1315813241
RUN_TEST(csharp_interface);
13242+
RUN_TEST(csharp_extension_block_members);
13243+
RUN_TEST(csharp_constructor_of_type_named_extension);
13244+
RUN_TEST(csharp_constructor_of_escaped_type_named_extension);
13245+
RUN_TEST(csharp_extension_block_conditional_members);
1315913246
RUN_TEST(swift_class);
1316013247
RUN_TEST(swift_protocol);
1316113248
RUN_TEST(kotlin_function);

‎tests/test_grammar_labels.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static const LabelGolden LABEL_GOLDENS[] = {
9393
{"ruby", "Class:1,Function:1,Module:1"},
9494
{"php", "Class:1,Function:1,Module:1"},
9595
{"c_sharp", "Class:1,Method:1,Module:1"},
96+
{"csharp_extension_declaration", "Class:1,Method:1,Module:1"},
9697
{"bash", "Function:2,Module:1"},
9798
{"zsh", "Function:2,Module:1"},
9899
{"lua", "Function:2,Module:1"},

‎tests/test_grammar_regression.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ const GrammarCase CBM_GRAMMAR_CASES[] = {
8181
"class A {\n void Foo() {}\n}\n",
8282
2,
8383
{"A", "Foo", NULL}},
84+
{"csharp_extension_declaration",
85+
CBM_LANG_CSHARP,
86+
"Extensions.cs",
87+
"using System.Collections.Generic;\n"
88+
"using System.Linq;\n"
89+
"public static class NumberExtensions\n"
90+
"{\n"
91+
" extension(IEnumerable<int> numbers)\n"
92+
" {\n"
93+
" public int SumPositive() =>\n"
94+
" numbers.Where(n => n > 0).Aggregate(0, static (total, n) => total + n);\n"
95+
" }\n"
96+
"}\n",
97+
2,
98+
{"NumberExtensions", "SumPositive", NULL}},
8499
{"bash",
85100
CBM_LANG_BASH,
86101
"a.sh",

0 commit comments

Comments
 (0)