[llvm-bugs] [Bug 52199] New: C-style cast wrongly falls back to reinterpret_cast when static_cast would choose deleted function

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Oct 17 11:02:06 PDT 2021


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

            Bug ID: 52199
           Summary: C-style cast wrongly falls back to reinterpret_cast
                    when static_cast would choose deleted function
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: arthur.j.odwyer 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

Notice that this is NOT diagnostic-QoI issue #7103 a.k.a. #32065 (even though I
strongly support those diagnostics and wish Clang had them). This particular
bug report is actually Clang being non-conforming.

The relevant part of the Standard is here:
https://eel.is/c++draft/expr.cast#4.sentence-3
> If a conversion can be interpreted in more than one of the ways listed above, 
> the interpretation that appears first in the list is used,
> even if a cast resulting from that interpretation is ill-formed.

In each of these cases, a `static_cast` would work, but Clang prematurely sees
that it would end up calling a deleted function (either a deleted ctor or a
deleted conversion operator) and avoids it, proceeding to interpret the C-style
cast as a `reinterpret_cast`. The correct behavior (GCC/MSVC/EDG) is to select
the `static_cast`, smash into the deleted function, and error out.


// https://godbolt.org/z/e9EvrWYEP
struct A {};
struct B { B(A&) = delete; };
int main() {
    A a;
    B b = (const B&)a;  // Clang accepts, GCC/MSVC/EDG reject
}

Similarly:

struct C {};
struct D { operator const C&() = delete; };
int main() {
    D d;
    C c = (const C&)d;  // Clang accepts, GCC/MSVC/EDG reject
}

Similar but different (MSVC joins Clang in wrongly accepting):

struct A {};
struct I1 { operator A() const; };
struct I2 { operator A() const; };
struct D : I1, I2 {};
int main() {
    D d;
    A a = (const A&)d;  // Clang/MSVC accept, GCC/EDG reject
}

-- 
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/20211017/0ebbce9d/attachment.html>


More information about the llvm-bugs mailing list