This is an archive of the discontinued LLVM Phabricator instance.

[llvm] Fix APInt to work in C++20 mode
Needs ReviewPublic

Authored by jloser on Dec 18 2022, 8:20 PM.

Details

Summary

Currently, an APSInt test fails to compile in C++20 mode due to ambiguity in
the available set of operator== thanks to synthetic three-way rewrite rules.

Specifically, the error is:

error: use of overloaded operator '==' is ambiguous
(with operand types 'const llvm::APSInt' and 'const unsigned long long')

from llvm/unittests/ADT/APFixedPointTest.cpp:698:3:
note: in instantiation of function template specialization
'testing::internal::EqHelper::Compare<llvm::APSInt, unsigned long long, nullptr>' requested here
  ASSERT_EQ(Val.convert(getLFractSema()).getValue(), -(1ULL << 31));
  ^

The candidates are:

llvm/include/llvm/ADT/APInt.h:2032:13: note: candidate function (with reversed parameter order)
inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
            ^
llvm/include/llvm/ADT/APSInt.h:174:8: note: candidate function
  bool operator==(int64_t RHS) const {
       ^
llvm/include/llvm/ADT/APSInt.h:343:13: note: candidate function (with reversed parameter order)
inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }

Make this case unambiguous by gating the extra operator== and operator!= on
the existence of three way comparison feature test macro.

Diff Detail

Event Timeline

jloser created this revision.Dec 18 2022, 8:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 18 2022, 8:20 PM
Herald added a subscriber: StephenFan. · View Herald Transcript
jloser requested review of this revision.Dec 18 2022, 8:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 18 2022, 8:20 PM
jloser updated this revision to Diff 483857.Dec 18 2022, 8:21 PM
jloser edited the summary of this revision. (Show Details)

Prettify commit message formatting

Is there a way to write this in a more portable fashion?

(if the uint64_t on nthe RHS versions were non-members, would that help?)

jloser added a comment.Jan 9 2023, 3:44 PM

Is there a way to write this in a more portable fashion?

(if the uint64_t on nthe RHS versions were non-members, would that help?)

Sorry for the delay here — I've been on vacation from the holidays. I'll play with it some more to get this to be more portable as I'm not too thrilled with the solution in its current form. IIRC, making the member comparison operator==(uint64_t) a non-member didn't help things.