@@ -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 --- */
883966TEST(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);
0 commit comments