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

            Bug ID: 112595
           Summary: ICE on invalid code: Literal class NTTP aggregate
                    initialized with self-referential pointer
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mital at mitalashok dot co.uk
  Target Milestone: ---

This code is invalid because it tries to create a NTTP with a pointer to a
temporary object:

<https://godbolt.org/z/af44badz6>

    struct S {
        S* self = this;
    };

    template<S s>
    void f() {}

    int main() {
        f<S{}>();
    }

With either -std=c++20 or -std=c++23, this causes an internal compiler error:

    <source>: In function 'int main()':
    <source>:9:11: error: no matching function for call to 'f<S{(&*this)}>()'
        9 |     f<S{}>();
          |     ~~~~~~^~
    <source>:6:6: note: candidate: 'template<S s> void f()'
        6 | void f() {}
          |      ^
    <source>:6:6: note:   template argument deduction/substitution failed:
    <source>:9:9: internal compiler error: in cxx_eval_constant_expression, at
cp/constexpr.cc:8236
        9 |     f<S{}>();
          |         ^
    0x26a4c3e internal_error(char const*, ...)
            ???:0
    0xb0affd fancy_abort(char const*, int, char const*)
            ???:0
0xb77c23 cxx_constant_value(tree_node*, tree_node*, int)
            ???:0
0xd1580b coerce_template_parms(tree_node*, tree_node*, tree_node*, int, bool)
            ???:0
0xd440fc fn_type_unification(tree_node*, tree_node*, tree_node*, tree_node*
const*, unsigned int, tree_node*, unification_kind_t, int, conversion**, bool,
bool)
            ???:0
0xb3a13d build_new_function_call(tree_node*, vec<tree_node*, va_gc,
vl_embed>**, int)
            ???:0
0xd6991c finish_call_expr(tree_node*, vec<tree_node*, va_gc, vl_embed>**, bool,
bool, int)
            ???:0
0xcf5ac9 c_parse_file()
            ???:0
0xe37da9 c_common_parse_file()
            ???:0

This ICE doesn't happen if aggregate initialization isn't used (Changing to
`f<S()>()` or adding `constexpr S() = default;`)
  • [Bug c++/112595] New: ICE on in... mital at mitalashok dot co.uk via Gcc-bugs

Reply via email to