llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

ICE: is_invocable_v (access via :: of non static member)

Open bernd5 opened this issue 6 years ago • 12 comments

Bugzilla Link 47119
Version trunk
OS Windows NT
Attachments compiler output
CC @DougGregor,@bernd5,@ojeda,@zygoloid

Extended Description

Clang++ front-end crashes with the following code:

Compilation command: clang++ -std=c++17 sample.cpp

See here on godbolt: https://godbolt.org/z/o1xjK8

Code:

#include <type_traits>

struct S {
    void Func() {
    }
};

#include <iostream>
int main()
{
    auto lambda = [](const auto& cls) -> decltype(
        std::remove_const_t<std::remove_reference_t<decltype(cls)>>::Func()
    ) {};

    auto isInv = std::is_invocable_v<decltype(lambda), S>;
    if (!isInv){
        std::cout << "detected correctly";
    }
    return 0;
}

bernd5 avatar Aug 11 '20 16:08 bernd5

I posted some path - but please review the code. I think that there is maybe something missing...

https://reviews.llvm.org/D85791

bernd5 avatar Aug 11 '20 22:08 bernd5

To avoid the included code, it can be written like this:

template<class... > using void_t = void;

template T&& declval() noexcept;

auto l = [](auto s) -> decltype(decltype(s)::f()) {};

template<class, class = void_t<>> struct has_f_fkt{ constexpr static bool value = false; };

template struct has_f_fkt<T, void_t<decltype(l(declval()))>> { constexpr static bool value = true; };

struct A { void f() {} }; struct B { static void f() {} }; static_assert(has_f_fkt::value == false); static_assert(has_f_fkt::value == true);

But with C++20 it could be reduced to the following test case:

#include <type_traits> struct S { void f() {} }; constexpr bool hasStaticF = std::is_invocable_v<decltype( [](auto s) -> decltype(decltype(s)::f()) {} ), S>;

bernd5 avatar Aug 11 '20 21:08 bernd5

Nice "minified" test-cases. Clang should not crash in those cases but emit some error. Because f is not callable with "::".

Here you see a "bigger" sample which implements it twice - via template and via lambda: https://godbolt.org/z/q4W9cq

Currently just the code with the template works with clang.

bernd5 avatar Aug 11 '20 21:08 bernd5

Also crashes (with a slightly different trace) with:

struct S {
  void f() {}
};
auto l = [](auto s) -> decltype(s)::f() {}(S());

ojeda avatar Aug 11 '20 21:08 ojeda

Reduced testcase:

struct S {
  void f() {}
};
auto l = [](auto s) -> decltype(decltype(s)::f()) {}(S());

ojeda avatar Aug 11 '20 20:08 ojeda

Link to stackoverflow:

https://stackoverflow.com/questions/63364309/ice-in-clang-with-is-invocable-v-is-my-code-valid

bernd5 avatar Aug 11 '20 20:08 bernd5

The runtime type is a "FunctionDecl" which is the base class of CXXMethodDecl, but no CXXRecordDecl.

bernd5 avatar Aug 11 '20 17:08 bernd5

It crashes at SemaExprMember.cpp on line 166 "contextClass = cast(DC);"

This cast fails. But the real issue is that isStaticContext has the value false.

bernd5 avatar Aug 11 '20 17:08 bernd5

@llvm/issue-subscribers-clang-frontend

llvmbot avatar Aug 17 '23 07:08 llvmbot

@llvm/issue-subscribers-c-17

llvmbot avatar Aug 17 '23 07:08 llvmbot

The original repro is no longer crashing on trunk.

MagentaTreehouse avatar Nov 29 '24 21:11 MagentaTreehouse