Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a variably-modified type should not be referenceable from a local class scope #55686

Open
zygoloid opened this issue May 25, 2022 · 3 comments
Open
Labels
c++ clang Clang issues not falling into any other category confirmed Verified by a second party crash-on-invalid

Comments

@zygoloid
Copy link
Collaborator

zygoloid commented May 25, 2022

Various lines of this testcase cause Clang to crash:

void f(int n) {
  using T = int[n];
  struct A {
    using U = T;
    void f() { U u; }
    void g() { T t; }
  };
  A::U u;
  A a;
  a.f();
  a.g();
}

Most of this is pretty clearly unsupportable: in particular, the calls to A::f and A::g don't provide the value of the VLA bound, so those functions shouldn't be able to use the type. Probably the best rule here is that a variably-modified type is simply not referenceable at all from within a local class. (Note that we do allow this from within a local lambda, and have special logic to capture the type.)

@zygoloid zygoloid added clang Clang issues not falling into any other category c++ crash-on-invalid labels May 25, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented May 25, 2022

@llvm/issue-subscribers-c-1

@AaronBallman AaronBallman added the confirmed Verified by a second party label May 25, 2022
@xgupta
Copy link
Contributor

xgupta commented Jan 14, 2023

if we try to fix the first issue where struct "A" uses the type alias "U" as a non-static data member which is defined as "T" which is a variable length array type. then test case would be

void f(int n) {
  using T = int[n];
  struct A {
    using U = T;
  };
  A::U u;
}

I think there should be a check implemented in lib/Sema/SemaDecl.cpp to disallow non-static data member if the type is isVariablyModifiedType().

@shafik
Copy link
Collaborator

shafik commented Jan 14, 2023

gcc gives the following diagnostic: https://godbolt.org/z/qvdErjf6j

<source>: In function 'void f(int)':
<source>:4:11: error: data member may not have variably modified type 'T' {aka 'int [n]'}
    4 |     using U = T;
      |           ^
Compiler returned: 1

I think that is the approach we should take.

CC @AaronBallman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ clang Clang issues not falling into any other category confirmed Verified by a second party crash-on-invalid
Projects
None yet
Development

No branches or pull requests

5 participants