Skip to content

detector: Mode D recovers a function's name from the line above its opener, but leaves start_line on the opener #2758

Closed

Description

@squid-protocol

Found while fixing #2727 (the orphan census), where the same shape caused a different symptom. Not a blocker for that fix — #2754 deliberately does not depend on start_line — but it is wrong on its own terms.

Repro

from gitgalaxy.core.detector import StructuralExtractor
from gitgalaxy.standards.language_standards import LANGUAGE_DEFINITIONS

src = 'f_create_role()\n{\n    : "$1"\n}\n\nf_inline() {\n    : "$1"\n}\n'
for f in StructuralExtractor('shell', LANGUAGE_DEFINITIONS).splice(src, '')['functions']:
    print(f['name'], f['start_line'], f['end_line'])
f_create_role    start_line=2   -> line 2 is '{'
f_inline         start_line=6   -> line 6 is 'f_inline() {'   (correct)

The K&R form — declaration on one line, { on the next — reports start_line on the brace. The same-line form is right. Both are ordinary shell; curl's initscript.sh in the language-crucible is written entirely in the first style.

Root cause

Shell is Mode D, whose openers include \{, so the satellite starts at the brace. detector.py L5183–L5198 already knows the name is not there and walks backwards through past_safe_lines to recover it:

satellite_name = self._extract_semantic_name(safe_line, lang_key)
if satellite_name == 'Anonymous_Block':
    for past_safe in reversed(past_safe_lines):
        ...
        fallback = self._extract_semantic_name(past_safe, lang_key)
        if fallback != 'Anonymous_Block':
            satellite_name = fallback     # <-- name recovered here
        break
...
sat_start_line = current_line_offset + 1  # L5204 -- still the OPENER's line

The lookback recovers the name but not the position. sat_start_char (L5205) has the same problem.

Scope, measured — and it is small

Over the language-crucible (40 files/language, 28,619 named functions), start_line points at a line not containing the function's name for 3,560 functions (12.4%). Almost all of that is correct and must not be "fixed":

bucket count verdict
start_line is ABOVE the name — annotations/attributes (@isTest, @Override, #[proc_macro_derive], [Experimental]) 3,448 correct — the declaration does begin at its annotations
name nowhere within ±6 lines 93 mostly my matcher (dart operator== is written operator ==); jcl's 19 are the synthetic Unknown_Block
start_line is BELOW the name — the declaration is outside the span 19 the defect

The 19: shell 13, dart 2, groovy 2, assembly 1, scheme 1. It is a real coding style rather than a rare accident, so the count scales with how much K&R-braced shell a scan contains, not with how unusual the input is.

Why it matters

start_line is not diagnostic-only:

  • sarif_recorder.py L69/L88/L110/L130/L143 — it is the locations line for every SARIF result. A wrong value puts a code-scanning annotation on the brace instead of the function, in the artifact that goes to GitHub and IDEs.
  • spatial_correlation.py L316 builds each function's scoped window as (start_line, end_line + 1). A window that starts one line late can miss a signal sitting on the declaration line itself — and the dampener correlations are scoring inputs, not reporting.
  • record_keeper.py L833, audit_recorder.py L272 ("Start Line"), gpu_recorder.py L269 all persist it.

Fix shape

When the backwards lookback succeeds, move the position with the name: remember the index of the past_safe line that produced the fallback and set sat_start_line/sat_start_char from it rather than from the opener. Only that branch should move — when the name comes from the opener line itself, nothing changes.

Two things to check before blessing, neither obviously benign:

  1. end_line = start_line + loc - 1 in the brace/label modes, and loc is computed from the block text. If start_line moves back one line without loc growing, end_line moves back too and the function's recorded extent shifts rather than extends. Decide whether the declaration line joins the block or only the anchor moves.
  2. It is corpus-visible in principle through spatial_correlation's window. keyword-rosetta's shell shell uses the same-line form (probe_globals() {), so the control corpus should not move — worth confirming rather than assuming, and the language-crucible golden master is the real test.

Independent of the orphan census and of #2748–#2753; related only by discovery.

Activity

  1. added
    bug
    core-engine
    on Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-engine

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions