[llvm-bugs] [Bug 43856] New: Cast from past-the-end pointer from Derived to Base invalid inside constant expression

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 30 17:02:39 PDT 2019


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

            Bug ID: 43856
           Summary: Cast from past-the-end pointer from Derived to Base
                    invalid inside constant expression
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ldionne at apple.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Hi,

The following code produces an error with Clang trunk:

    $ cat <<EOF | clang++ -xc++ -std=c++17 -
    struct Base { };
    struct Derived : Base { };

    constexpr bool f() {
        Derived array[5] = {};
        Derived* ptr = array + 5;
        Base* base = ptr; // <<<<< error HERE
        return true;
    }

    static_assert(f());
    EOF

The error is:

    <stdin>:4:16: error: constexpr function never produces a constant
expression [-Winvalid-constexpr]
    constexpr bool f() {
                   ^
    <stdin>:7:18: note: cannot access base class of pointer past the end of
object
        Base* base = ptr;
                     ^
    <stdin>:11:15: error: static_assert expression is not an integral constant
expression
    static_assert(f());
                  ^~~
    <stdin>:7:18: note: cannot access base class of pointer past the end of
object
        Base* base = ptr;
                     ^
    <stdin>:11:15: note: in call to 'f()'
    static_assert(f());
                  ^

I believe this should still be a constant expression because we are not
actually accessing `ptr` (which is indeed past-the-end), we're just casting it
from a Derived class to a Base class.

-- 
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/20191031/87687e88/attachment.html>


More information about the llvm-bugs mailing list