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

[clang++] Does not error with a zero length array declaration (definition) #121398

Closed
safocl opened this issue Dec 31, 2024 · 4 comments
Closed
Labels
accepts-invalid clang:frontend Language frontend issues, e.g. anything involving "Sema" question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@safocl
Copy link

safocl commented Dec 31, 2024

Clang++ allows declaration (definition) of arrays of zero size (TYPE NAME[0]). However, this is prohibited by the c++ standard:
[c++23] (here)

In a declaration T D where D has the form
D1 [ constant-expression opt ] attribute-specifier-seq opt
[...]
The constant-expression shall be a converted constant expression of type std​::​size_t ([expr.const]).
Its value N specifies the array bound, i.e., the number of elements in the array; N shall be greater than zero.

[c++11] (here)

[c++17] (here)

Example :

#include <iostream>

struct Data {
   int lhs[0];
   explicit Data(const int rhs) {
    lhs[rhs];
    lhs[0]=rhs;
   }

   void print() {
      std::cout << lhs[0] << "\n";
   }
};


int main() {
   int aaa[0];
   int bbb[0]{};
   Data t(42);
   t.print();
   return sizeof(aaa) + sizeof(bbb);
}

Expected behavior
COMPILETIME ERROR.

Current behavior
Compilation completed without errors.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Dec 31, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" accepts-invalid and removed clang Clang issues not falling into any other category labels Dec 31, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 31, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (safocl)

Clang++ allows declaration (definition) of arrays of zero size (`TYPE NAME[0]`). However, this is prohibited by the c++ standard: [c++23] ([here](https://timsong-cpp.github.io/cppwp/n4950/dcl.array#1.sentence-3)) ``` In a declaration T D where D has the form D1 [ constant-expression opt ] attribute-specifier-seq opt [...] The constant-expression shall be a converted constant expression of type std​::​size_t ([expr.const]). Its value N specifies the array bound, i.e., the number of elements in the array; N shall be greater than zero. ```

[c++11] ([here](https://timsong-cpp.github.io/cppwp/n3337/dcl.array#1))

[c++17] ([here](https://timsong-cpp.github.io/cppwp/n4659/dcl.array#1))

Example :

#include &lt;iostream&gt;

struct Data {
   int lhs[0];
   explicit Data(const int rhs) {
    lhs[rhs];
    lhs[0]=rhs;
   }

   void print() {
      std::cout &lt;&lt; lhs[0] &lt;&lt; "\n";
   }
};


int main() {
   int aaa[0];
   int bbb[0]{};
   Data t(42);
   t.print();
   return sizeof(aaa) + sizeof(bbb);
}

Expected behavior
COMPILETIME ERROR.

Current behavior
compilation is done without error.

@MacDue
Copy link
Member

MacDue commented Dec 31, 2024

Note: If you use -pedantic -Werror this does become an error:

<source>:4:12: error: zero size arrays are an extension [-Werror,-Wzero-length-array]
    4 |    int lhs[0];
      |            ^

https://godbolt.org/z/717Y6xn3c

@safocl
Copy link
Author

safocl commented Dec 31, 2024

Note: If you use -pedantic -Werror this does become an error:

<source>:4:12: error: zero size arrays are an extension [-Werror,-Wzero-length-array]
    4 |    int lhs[0];
      |            ^

This error is a limitation of the core language - it should work without additional compiler options (which only help to generate correct code).
(this is not semantics, but syntax -- such a code entry is unacceptable in principle)

https://godbolt.org/z/1TfPEcr1f -- it does error for the The constant-expression shall be a converted constant expression of type std​::​size_t ([expr.const]) rule, that have same rank with issue

<source>:17:12: error: value of type 'const char[1]' is not implicitly convertible to 'unsigned long'
   17 |    int aaa[""];
      |            ^~

@shafik
Copy link
Collaborator

shafik commented Dec 31, 2024

This is a GNU extension: #54705

There is some question as to why this allowed outside of a flexible array context but I am not sure how much appetite there would be to change this as we would need to coordinate with both gcc/edg.

I am going to close this but perhaps in the future someone might want to consider thinking about this.

@shafik shafik closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2024
@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepts-invalid clang:frontend Language frontend issues, e.g. anything involving "Sema" question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

5 participants