Bug 124532 - Musttail fails with 2 returns in the same scope
Summary: Musttail fails with 2 returns in the same scope
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 15.2.0
: P3 normal
Target Milestone: 15.3
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid, tail-call
Depends on:
Blocks:
 
Reported: 2026-03-16 08:00 UTC by Andy Wingo
Modified: 2026-05-15 19:26 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 15.2.1, 16.1.1, 17.0
Known to fail: 15.2.0, 16.1.0
Last reconfirmed: 2026-03-16 00:00:00


Attachments
fix (1.10 KB, patch)
2026-05-14 21:36 UTC, Andi Kleen
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andy Wingo 2026-03-16 08:00:35 UTC
Consider:

```
$ gcc --version
gcc (GCC) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat /tmp/test1.c
extern int f(void);

static int g(void) {
  [[gnu::musttail]] return f();
  return 42;
}
$ gcc -o /tmp/test1.o -c /tmp/test1.c
/tmp/test1.c: In function ‘g’:
/tmp/test1.c:5:10: error: cannot tail-call: return value must be a call
    5 |   return 42;
      |          ^~
```

However, this works:
```
static int g(void) {
  if (1) [[gnu::musttail]] return f();
  return 42;
}
```
Comment 1 Drea Pinski 2026-03-16 14:02:20 UTC
Hmm,
Comment 2 Drea Pinski 2026-03-16 14:08:54 UTC
Confirmed, this is a C front-end issue. 
once we see `[[gnu::musttail]]`, astate.musttail_p is never reset.

I will fix this.
Comment 3 Drea Pinski 2026-05-14 19:55:44 UTC
.
Comment 4 Andi Kleen 2026-05-14 21:35:16 UTC
The workaround is to put each statement into an own compound statement to reset the state.
Comment 5 Andi Kleen 2026-05-14 21:36:14 UTC
Created attachment 64461 [details]
fix
Comment 6 GCC Commits 2026-05-15 17:17:43 UTC
The trunk branch has been updated by Andi Kleen <ak@gcc.gnu.org>:

https://gcc.gnu.org/g:591a8ac50d8faf914391e616faf6311f2143fde7

commit r17-534-g591a8ac50d8faf914391e616faf6311f2143fde7
Author: Andi Kleen <ak@gcc.gnu.org>
Date:   Thu May 14 14:37:01 2026 -0700

    pr124532: Reset musttail attribute in compound statements
    
    A compound statement didn't reset the musttail state after the statement
    with the attribute, which led to bogus errors later. Always reset it.
    
            PR c/124532
    
    gcc/c/ChangeLog:
    
            * c-parser.cc (struct attr_state): Add reset method.
            (c_parser_compound_statement_nostart): Rename a to astate.
            Reset state before iterating statements.
    
    gcc/testsuite/ChangeLog:
    
            * c-c++-common/pr124532.c: New test.
Comment 7 GCC Commits 2026-05-15 18:35:46 UTC
The releases/gcc-16 branch has been updated by Andi Kleen <ak@gcc.gnu.org>:

https://gcc.gnu.org/g:f1306d5ead2354ed2b7d142ee13f65a980fde424

commit r16-8920-gf1306d5ead2354ed2b7d142ee13f65a980fde424
Author: Andi Kleen <ak@gcc.gnu.org>
Date:   Thu May 14 14:37:01 2026 -0700

    pr124532: Reset musttail attribute in compound statements
    
    A compound statement didn't reset the musttail state after the statement
    with the attribute, which led to bogus errors later. Always reset it.
    
            PR c/124532
    
    gcc/c/ChangeLog:
    
            * c-parser.cc (struct attr_state): Add reset method.
            (c_parser_compound_statement_nostart): Rename a to astate.
            Reset state before iterating statements.
    
    gcc/testsuite/ChangeLog:
    
            * c-c++-common/pr124532.c: New test.
Comment 8 GCC Commits 2026-05-15 19:19:47 UTC
The releases/gcc-15 branch has been updated by Andi Kleen <ak@gcc.gnu.org>:

https://gcc.gnu.org/g:4a7fcf5b32a979f9a009f5dc71140eceb7db1b1a

commit r15-11172-g4a7fcf5b32a979f9a009f5dc71140eceb7db1b1a
Author: Andi Kleen <ak@gcc.gnu.org>
Date:   Thu May 14 14:37:01 2026 -0700

    pr124532: Reset musttail attribute in compound statements
    
    A compound statement didn't reset the musttail state after the statement
    with the attribute, which led to bogus errors later. Always reset it.
    
            PR c/124532
    
    gcc/c/ChangeLog:
    
            * c-parser.cc (struct attr_state): Add reset method.
            (c_parser_compound_statement_nostart): Rename a to astate.
            Reset state before iterating statements.
    
    gcc/testsuite/ChangeLog:
    
            * c-c++-common/pr124532.c: New test.
Comment 9 ak 2026-05-15 19:23:11 UTC
patches checked in starting with gcc 15