[llvm-bugs] [Bug 37886] New: clang++ accept illegal code

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jun 20 19:33:51 PDT 2018


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

            Bug ID: 37886
           Summary: clang++ accept illegal code
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: zhonghao at pku.org.cn
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org

The code is as follow:

#include <string>

template <typename T>
struct SimpleAllocator
{
    template <typename U> friend struct SimpleAllocator;

    using value_type = T;
    using pointer = T *;

    struct rebind { template <typename U> using other = SimpleAllocator<U>; };

    using propagate_on_container_copy_assignment = std::true_type;
    using propagate_on_container_move_assignment = std::true_type;
    using propagate_on_container_swap = std::true_type;

    explicit SimpleAllocator(void * p) : state(p) {}

    template <typename U>
    SimpleAllocator(SimpleAllocator<U> const & rhs) : state(rhs.state) {}

    pointer allocate(std::size_t n)
    {
        return static_cast<pointer>(::operator new(sizeof(T) * n));
    }

    void deallocate(pointer p, std::size_t)
    {
        ::operator delete(p);
    }

    template <typename U, typename V>
    friend bool operator==(SimpleAllocator<U> const & lhs, SimpleAllocator<V>
const & rhs)
    {
        return lhs.state == rhs.state;
    }

    template <typename U, typename V>
    friend bool operator!=(SimpleAllocator<U> const & lhs, SimpleAllocator<V>
const & rhs)
    {
        return lhs.state != rhs.state;
    }

private:
    void * state;
};

using astring = std::basic_string<char, std::char_traits<char>,
SimpleAllocator<char>>;

int main()
{
    int x, y;

    SimpleAllocator<void> a(&x), b(&y);

    astring s("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", a);
    astring t(std::move(s), b);
}

The code sample comes from https://bugs.llvm.org/show_bug.cgi?id=20335
The latest clang compiles the attached code sample, but gcc produces many
errors.

I reported the problem to gcc:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86074

In this discussion, programmers indicate that clang shall also reject the code. 
My report was marked as duplicated with multiple reports:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78925
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54376

It said that GCC, EDG, and MSVC all reject the code, but clang++ accepts it. 

Shall clang repair the problem?

-- 
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/20180621/291e4289/attachment.html>


More information about the llvm-bugs mailing list