[llvm-bugs] [Bug 48074] New: Improper resolution of constraint requirements when using CRTP

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Nov 4 06:58:25 PST 2020


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

            Bug ID: 48074
           Summary: Improper resolution of constraint requirements when
                    using CRTP
           Product: clang
           Version: 11.0
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: barry.revzin at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Using libstdc++'s <ranges> header with clang 11 doesn't compile even the most
basic examples:

#include <ranges>
auto w = std::views::iota(0);

The failure reduces to the following:

template <typename T> T declval();

template <typename T> using begin_t = decltype(declval<T&>().begin());
template <typename T> concept C = true;

template <typename D>
struct simple_view_interface
{
    auto also_begin() requires
        #ifdef TYPE_TRAIT
        C<begin_t<D>>
        #else
        requires (D d) { {d.begin()} -> C; }
        #endif
    {
        return static_cast<D&>(*this).begin();
    }
};

struct wat_view : simple_view_interface<wat_view>
{
    auto begin() -> int* { return nullptr; }
};

auto w = wat_view().also_begin();

Both spellings of the requirement on also_begin() should be equivalent, and
should be valid in this case. The checking of the requirements should be
deferred until instantiation, at which point, wat_view is complete. 

If TYPE_TRAIT is defined, the requirement is eagerly instantiated, an even the
definition of wat_view fails to compile:

<source>:3:62: error: no member named 'begin' in 'wat_view'
template <typename T> using begin_t = decltype(declval<T&>().begin());
                                               ~~~~~~~~~~~~~ ^
<source>:11:11: note: in instantiation of template type alias 'begin_t'
requested here
        C<begin_t<D>>
          ^
<source>:20:19: note: in instantiation of template class
'simple_view_interface<wat_view>' requested here
struct wat_view : simple_view_interface<wat_view>
                  ^

If TYPE_TRAIT is not defined, wat_view is properly defined, but usage fails:

<source>:25:21: error: invalid reference to function 'also_begin': constraints
not satisfied
auto w = wat_view().also_begin();
                    ^
<source>:13:29: note: because 'd.begin()' would be invalid: no member named
'begin' in 'wat_view'
        requires (D d) { {d.begin()} -> C; }
                            ^

Even though there is a member named 'begin' in 'wat_view'.

The program should be valid either way. gcc accepts both.

-- 
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/20201104/ddb2d3ba/attachment.html>


More information about the llvm-bugs mailing list