[llvm-bugs] [Bug 48402] New: Rejects valid subscript expression on array of unknown bound in constant expression

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Dec 5 14:20:57 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48402

            Bug ID: 48402
           Summary: Rejects valid subscript expression on array of unknown
                    bound in constant expression
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: leni536 at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

clang version 12.0.0 (https://github.com/llvm/llvm-project.git
2518433f861fcb877d0a7bdd9aec1aec1f77505a)

command line options: -std=c++11 -pedantic-errors

Also reproducible with c++14, c++17 and c++20

```
extern const int arr[];
constexpr const int (&arrref)[] = arr;

constexpr int arr[2] = {1,2};

constexpr int x = arrref[0];
```

error: constexpr variable 'x' must be initialized by a constant expression
constexpr int x = arrref[0];
              ^   ~~~~~~~~~
note: read of element of array without known bound is not allowed in a constant
expression
constexpr int x = arrref[0];

Accessing elements through arrays of unknown bound is not explicitly forbidden
in constant expressions, so I think it is valid.

The followings are accepted:

```
extern const int arr[];
constexpr const int * ptr = arr;

constexpr int arr[2] = {1,2};

constexpr int x = ptr[0];
```

```
extern const int arr[];
constexpr const int (&arrref)[] = arr;

constexpr int arr[2] = {1,2};

constexpr const int* decay() {
    return arrref;
}

constexpr int x = decay()[0];
```

P0388 is accepted in C++20 and will enable other ways of creating subscript
constant expressions to arrays of unknown bound. Related gcc bug:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97645

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201205/5912317d/attachment.html>


More information about the llvm-bugs mailing list