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

typeof_unqual does not remove qualifiers from qualified array types #92667

Open
Halalaluyafail3 opened this issue May 18, 2024 · 5 comments · May be fixed by #92767
Open

typeof_unqual does not remove qualifiers from qualified array types #92667

Halalaluyafail3 opened this issue May 18, 2024 · 5 comments · May be fixed by #92767
Labels
c23 clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@Halalaluyafail3
Copy link

Clang does not remove qualifiers from qualified array types, i.e. array types with qualified element types with typeof_unqual in some situations. See GCC bug 112841 which has the following example:

const char* const animals[] = {
"aardvark",
"bluejay",
 "catte",
};
int main (int argc, char* argv[]) {
 const char* animals2_array1[3];
 typeof_unqual(animals) animals2_array;
 animals2_array1[0] = 0;
 animals2_array[0] = 0;
 return 0;
}

Clang has typeof_unqual(animals) result in a const qualified type which results in the assignment animals2_array[0] = 0; failing.

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels May 18, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 18, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (Halalaluyafail3)

Clang does not remove qualifiers from qualified array types, i.e. array types with qualified element types with typeof_unqual in some situations. See [GCC bug 112841](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112841) which has the following example: ```cpp const char* const animals[] = { "aardvark", "bluejay", "catte", }; int main (int argc, char* argv[]) { const char* animals2_array1[3]; typeof_unqual(animals) animals2_array; animals2_array1[0] = 0; animals2_array[0] = 0; return 0; } ``` Clang has `typeof_unqual(animals)` result in a const qualified type which results in the assignment `animals2_array[0] = 0;` failing.

@shafik
Copy link
Collaborator

shafik commented May 20, 2024

CC @AaronBallman

@AaronBallman
Copy link
Collaborator

This looks like the correct behavior to me. In the final version of C23, there's an example specific to this (C23 6.7.3.6p7 EXAMPLE 2):

EXAMPLE 2 The following program:

const _Atomic int purr = 0;
const int meow = 1;
const char* const animals[] = {
  "aardvark",
  "bluejay",
  "catte",
};
typeof_unqual(meow) main (int argc, char* argv[]) {
  typeof_unqual(purr) plain_purr;
  typeof(_Atomic typeof(meow)) atomic_meow;
  typeof(animals) animals_array;
  typeof_unqual(animals) animals2_array;
  return 0;
}

is equivalent to this program:

const _Atomic int purr = 0;
const int meow = 1;
const char* const animals[] = {
  "aardvark",
  "bluejay",
  "catte",
};
int main (int argc, char* argv[]) {
  int plain_purr;
  const _Atomic int atomic_meow;
  const char* const animals_array[3];
  const char* animals2_array[3];
  return 0;
}

Note: typeof(animals) animals_array; is equivalent to const char* const animals_array[3]; and typeof_unqual(animals) animals2_array; is equivalent to const char* animals2_array[3]; -- only the top-level const is stripped.

MitalAshok added a commit to MitalAshok/llvm-project that referenced this issue May 20, 2024
Properly remove qualifiers for both the element type and the array type

Fixes llvm#92667
@AaronBallman
Copy link
Collaborator

Oh, wait a second, something odd is going on here: https://godbolt.org/z/4r6M49WrT

The static_assert passes, but the code still does not compile. So something is amiss.

@MitalAshok
Copy link
Contributor

@AaronBallman Currently, only the top-level const was removed, leaving us with an invalid non-const array of const element type. So the array type matches the non-const type, but the element type does not (when checking if the modification is valid)

This also allowed strange things like https://godbolt.org/z/11xG499PG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c23 clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants