Menu

#4000 Spurious "block-scope variable'i' declared extern and intialized"

open
nobody
None
other
5
7 days ago
2026-06-03
No

1: Sample code that reproduces the problem.

int f2(void);

int main(void)
{
  typeof(f2()) i = 3;
  i = 2;
  return i;
}

2: Exact command used to run SDCC on this sample code

sdcc -c --std=c23 --stack-auto test.c

3: SDCC version tested (type "sdcc -v" to find it)

SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/r4k/r5k/r6k/sm83/tlcs90/ez80/z80n/r800/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02/f8/f8l TD- 4.5.24 #16456 (Mac OS X ppc)
published under GNU General Public License (GPL)

4: Copy of the error message or incorrect output, or a clear description of the observed versus expected behavior.
The output is:

test.c:5: error 320: block-scope variable'i' declared extern and intialized

I don't think the variable should be considered 'extern' because 'extern' isn't part of the type of the return value of f2(). The error message also seems to contain errors.

Discussion

  • Christopher Bazley

    I am attaching a proposed bugfix for this issue, created with assistance from Codex in ChatGPT Work. I hope that it will prove acceptable when reviewed by SDCC's maintainers. I have done the following testing:

    • complete diagnostic-validation results.
    • complete runtime regression results for six z80 targets with 0 failures (excepting one failure triggered by me manually deleting a file during the test run).
     
    • Philipp Klaus Krause

      The patch looks good to me. When I apply it, I'll also add a test in support/valdiag.

      For now, IMO we should wait a few days for the discussion on sdcc-devel on LLM use to settle.

       
      • Christopher Bazley

        Thanks for the review. I attach an updated version of the patch that is unchanged except that it has the extra valdiag tests. I hope they are satisfactory. I also retested it:

        Summary for 'ucz80': 0 failures, 36742 tests, 6407 test cases, 7347757 bytes, 1613638604 ticks
        Summary for 'mcs51': 0 failures, 643 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'mcs51-large': 0 failures, 641 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'mcs51-stack-auto': 0 failures, 642 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'ds390': 0 failures, 629 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'z80': 0 failures, 632 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'z180': 0 failures, 631 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'r2k': 0 failures, 631 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'r4k': 0 failures, 631 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'sm83': 0 failures, 631 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'tlcs90': 0 failures, 631 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'hc08': 0 failures, 630 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 's08': 0 failures, 630 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'mos6502': 0 failures, 630 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'stm8': 0 failures, 633 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'f8': 0 failures, 629 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'pdk13': 0 failures, 632 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'pdk14': 0 failures, 630 tests, 365 test cases, 0 bytes, 0 ticks
        Summary for 'pdk15': 0 failures, 628 tests, 365 test cases, 0 bytes, 0 ticks

         

Log in to post a comment.