Repository navigation
Description
Found while starting work on #775 (strict-parsing tests for csharp, part of epic #518).
The bug
Unlike every other C-family language in this codebase (C, C++, Java, ...), csharp's func_start
regex (gitgalaxy/standards/language_standards.py, csharp block) does not consume the
parameter list or require a trailing { -- it stops matching right at the opening (:
r"([@A-Za-z_$][\w_$.]*)(?:[ \t\n]*<[^>]{0,100}>)?[ \t\n]{0,200}\("The actual "is this really a function body, and where does it end" work is deferred entirely to
detector.py's _slice_by_braces (Mode B), which searches for the next literal { within a
bounded window (min(next_match_start, start_idx + 2000)) after each regex match, and skips the
match via continue if none is found.
This mostly works by accident for typical brace-bodied methods sitting inside an enclosing type,
because _slice_by_braces's if start_idx < last_end_idx: continue guard discards any match
that falls inside an already-claimed enclosing method's body (confirmed: Console.WriteLine(...),
Environment.Exit(...), this.Foo(...) call statements inside a real method are correctly
absorbed and don't produce spurious extra function entries). But it breaks in two confirmed,
practically common ways:
1. Expression-bodied members are never counted as functions at all. They have no { at
all -- they use => then an expression then ;. Confirmed via direct _slice_by_braces call:
public class Calc
{
public int Square(int x) => x * x;
public int Cube(int x) => x * x * x;
}produces zero satellites. Expression-bodied methods/properties are extremely common,
idiomatic modern C# (introduced C# 6, expanded every version since) -- this means a large and
growing fraction of real C# methods are structurally invisible to every function-level metric
(complexity, magnitude, LOC-per-function, etc.), not just miscounted.
2. Bare call statements can be hallucinated as functions when there's no enclosing brace to
bound them, i.e. C# 9+ top-level statements (the default dotnet new console template since
.NET 6). Confirmed via direct _slice_by_braces call:
Console.WriteLine("Hello");
Environment.Exit(0);
if (args.Length > 0) {
Console.WriteLine(args[0]);
}produces a single hallucinated satellite named Environment.Exit whose body incorrectly spans
through the if block below it (the first { the bounded window happens to find), attributing
unrelated code's complexity/branch-count to a bare Environment.Exit(0); call statement.
Why this wasn't caught by prior isolated tests
The 4 pre-existing scattered csharp regression tests (test_csharp_iron_wall_redos,
test_csharp_args_lambda_redos_immunity, test_csharp_events_plus_minus_equals_operators,
test_csharp_should_and_wait_boundary_regression) all test the func_start/other regexes in
isolation via .search()/.finditer() directly against the pattern, never through the actual
detector.py pipeline (_slice_by_braces) that performs the brace-search/absorption logic. The
regex-only view looks fine (it matches real method signatures); the gap only appears once you
trace how those matches get turned into actual function-level records.
Scope
Not fixing this as part of #775 -- matches this epic's established pattern (#691, #694/#697,
#733, #746, #770) of keeping per-language strict-parsing issues narrowly scoped to the
rules-dict regexes in isolation, and filing pipeline/architectural-level gaps as their own
issue. #775 will proceed testing csharp's func_start regex against its actual, currently
observed matching behavior (which the regex itself does correctly, matching a leading
identifier + open-paren) -- it's the downstream brace-search/absorption step in detector.py
that needs the real fix here, most likely by giving csharp's func_start an C/C++-style
parameter-list-then-required-terminator match (either a real { or a =>...; expression body)
so the regex itself can determine the match boundary instead of relying on a bounded post-hoc
brace search.
Suggested fix shape (not attempted here)
func_start would need to consume the parameter list itself (bounded nested-paren tolerance,
same 1-level trick already used elsewhere in this file for function-pointer-style params) and
then require either:
- a
{(block-bodied method), or =>followed by a bounded expression up to;(expression-bodied method/property)
before considering the match complete -- removing the need for detector.py's separate
post-hoc brace search for this language, and closing both gaps above at the source.
Activity
Fix #775: Strict parsing tests for csharp structural signatures (#790)
Extraction hardening: typescript (#815) (#862)
Fix/859 php docblock corruption (#864)
Fix squid-protocol#789: csharp expression-bodied methods and bare-cal…
docs: add JavaScript language-status doc, first tree-sitter-based §9 …
fix(core): func_start Invocation Shield missing for 6 languages -- cl…
fix(core): prevent Prism from swallowing code due to quotes in commen…
fix(core): broaden scala/matlab/haskell func_start recall -- closes #…
fix(rust): recognize bodyless trait-method signatures in func_start (#…
fix(objective-c): recover objc's bodyless @interface method declarati…
fix(csharp): make Invocation Shield terminator scan paren/bracket-dep…
fix(go): extract bodyless function declarations (#1756) (#1760)
Fix csharp func_start dropping bodyless interface/abstract method dec…