This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix constant evaluation about static member function
Needs RevisionPublic

Authored by yaxunl on Jul 5 2023, 7:27 PM.

Details

Summary

Currently, clang does not allow static constexpr member
functions called through a reference of an object
in constant expression, e.g. the following code

class static_multimap{
    public:
        static constexpr int size() noexcept{
            return 8;
        }
};

template <typename Map>
void test_non_shmem_pair_retrieve(Map& map){
    auto constexpr cg_size = map.size();
}

int main(){
    static_multimap map;
    test_non_shmem_pair_retrieve(map);
    return 0;
}

fails to compile with clang. (https://godbolt.org/z/T17vTWYcs)

This does not make sense since the evaluation of map.size
does not rely on map. The same code compiles with GCC.

Diff Detail

Event Timeline

yaxunl created this revision.Jul 5 2023, 7:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 5 2023, 7:27 PM
yaxunl requested review of this revision.Jul 5 2023, 7:27 PM
yaxunl edited the summary of this revision. (Show Details)
tra added a comment.Jul 18 2023, 11:13 AM

@rsmith Richard, PTAL. This needs your language lawyering expertise.

rsmith requested changes to this revision.Aug 7 2023, 11:32 AM

Clang was correct here until fairly recently; P2280 (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2280r4.html) changed the language rules. It was applied as a DR, so we should make that change retroactively rather than only in C++23 mode. See https://github.com/llvm/llvm-project/issues/63139, which tracks implementation of that language change.

I don't think we should be applying an ad-hoc patch like this which doesn't follow either the old or the new language rule, and should instead implement the new rule from P2280.

This revision now requires changes to proceed.Aug 7 2023, 11:32 AM