https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117317

            Bug ID: 117317
           Summary: An internal compiler error on call to virtual
                    constexpr function via comparison operator in CRTP
                    with multiple inheritance
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kirshamir at gmail dot com
  Target Milestone: ---

The following code results with internal compiler error:

class Base {
public:
    constexpr bool operator==(const Base& b) const {
        return getIdentity() == b.getIdentity();
    }
private:
    constexpr virtual int getIdentity() const = 0;
};

template<typename ActualType, typename Base1, typename... AdditionalBases>
class Comparable: public Base1, public AdditionalBases... {
public:
    constexpr bool operator==(const Comparable&) const = default;
private:
    constexpr int getIdentity() const override {
        return 1;
    }
};

class A: public Comparable<A, Base> {};

class B: public Comparable<B, Base> {};

class AB: public Comparable<AB, A, B> {};

int main() {
    constexpr AB ab_1;
    constexpr AB ab_2;

    // internal compiler error :-(
    static_assert(ab_1 == ab_2);
}

=====

Error:

<source>: In instantiation of 'constexpr int Comparable<ActualType, Base1,
AdditionalBases>::getIdentity() const [with ActualType = AB; Base1 = A;
AdditionalBases = {B}]':
<source>:4:27:   required from here
    4 |         return getIdentity() == b.getIdentity();
      |                ~~~~~~~~~~~^~
<source>:31:24:   in 'constexpr' expansion of 'ab_1.AB::Comparable<AB, A,
B>.Comparable<AB, A, B>::operator==(ab_2.AB::Comparable<AB, A, B>)'
<source>:13:20:   in 'constexpr' expansion of '((const Comparable<AB, A,
B>*)this)->Comparable<AB, A, B>::A.A::Comparable<A, Base>.Comparable<A,
Base>::operator==(<anonymous>.Comparable<AB, A, B>::A.A::Comparable<A, Base>)'
<source>:13:20:   in 'constexpr' expansion of '((const Comparable<A,
Base>*)this)->Comparable<A,
Base>::Base.Base::operator==(<anonymous>.Comparable<A, Base>::Base)'
<source>:17:5: internal compiler error: in use_thunk, at cp/method.cc:324
   17 |     }
      |     ^
0x287e175 diagnostic_context::diagnostic_impl(rich_location*,
diagnostic_metadata const*, diagnostic_option_id, char const*, __va_list_tag
(*) [1], diagnostic_t)
        ???:0
0x28945d5 internal_error(char const*, ...)
        ???:0
0xa90ace fancy_abort(char const*, int, char const*)
        ???:0
0xd1eb6b emit_associated_thunks(tree_node*)
        ???:0
0xd1ed0e expand_or_defer_fn(tree_node*)
        ???:0
0xcc60cd instantiate_decl(tree_node*, bool, bool)
        ???:0
0xb08a50 maybe_constant_value(tree_node*, tree_node*, mce_value)
        ???:0
0xd23df0 finish_static_assert(tree_node*, tree_node*, unsigned int, bool, bool)
        ???:0
0xc9e57a c_parse_file()
        ???:0
0xdf7f99 c_common_parse_file()
        ???:0


--------------
Notes:

1. Code compiles with clang: https://godbolt.org/z/j8oMxar8o
2. Removing the constexpr or the virtual from getIdentity() - code compiles:
https://godbolt.org/z/G5nMnWMd9 and https://godbolt.org/z/f3PWez1sb
3. Without CRTP code compiles: https://godbolt.org/z/56ah81n6h

Reply via email to