https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107360

--- Comment #4 from Alexey Izbyshev <izbyshev at ispras dot ru> ---
(In reply to Martin Uecker from comment #3)
> I think there there are cases were variably modified
> return types are allowed in ISO C:
> 
> void f(int n, double (*(bar(void)))[n])
> {
>     double (*p)[n] = bar();
> }

Yes, I should have been more precise. Per C11 6.7.6.2:

> If an identifier is declared as having a variably modified type, it shall be 
> an ordinary identifier (as defined in 6.2.3), have no linkage, and have 
> either block scope or function prototype scope.

So, since my original example is in C++ and involves return type deduction,
what I really wanted to say is that it's not allowed to *define* a function
with a variably modified return type in ISO C (hence it seems fine to not
support it in GCC's VLA extension for C++ too and consider such code invalid).

GCC does allow to define a nested function with a variably modified return type
though, and, perhaps surprisingly, a test case similar to my original C++ one
is compiled successfully:

extern int n;

int f() {
  int a[n];
  int (*g())[n] {
    return &a;
  };
  return sizeof *g();
}

But what would seem to be a C++ equivalent crashes GCC:

extern int n;

int f() {
  int a[n];
  auto g = [&]() {
    return &a;
  };
  return sizeof *g();
}

during RTL pass: expand
<source>: In function 'int f()':
<source>:6:13: internal compiler error: in expand_expr_real_1, at expr.cc:10586
    6 |     return &a;
      |             ^
...

And for this one it's less clear to me whether GCC wants to consider it
invalid.

But without return type deduction GCC refuses to compile:

extern int n;

int f() {
  typedef int t[n];
  t a;
  auto g = [&]() -> t* {
    return &a;
  };
  return sizeof *g();
}

<source>: In function 'int f()':
<source>:6:12: error: data member may not have variably modified type 'int
(*())[n]'
    6 |   auto g = [&]() -> t* {
      |            ^
<source>:6:12: error: declaration of 'operator()' as non-function
Compiler returned: 1

Reply via email to