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

            Bug ID: 121408
           Summary: ICE when using dynamic_cast inside a lambda in a
                    template parameter with C++20/C++23
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jirehguo at tju dot edu.cn
  Target Milestone: ---

The following invalid code causes an internal compiler error when compiled with
-std=c++20 or -std=c++23 since gcc-10.1
Reproducer link: https://godbolt.org/z/4Wf1e1brP


Code:
----------------------------------
#include <memory>

template <class T>
void spam(decltype([] {
  std::weak_ptr<int> weakRef = std::make_shared<int>(42);
  auto sharedRef = weakRef.lock();
  if (sharedRef) {
    int *rawPtr = sharedRef.get();
    if (dynamic_cast<std::string *>(rawPtr)) {
    }
  }
}) (*s)[sizeof(T)]) {}

int main() { spam<char>(1); }

Command:
--------
g++ test.cpp -std=c++20


Output:
-------------
<source>: In substitution of 'template<class T> void spam(decltype (<lambda>)
(*)[sizeof (T)]) [with T = char]':
<source>:14:24:   required from here
   14 | int main() { spam<char>(1); }
      |              ~~~~~~~~~~^~~
<source>:9:9: error: cannot 'dynamic_cast' 'rawPtr' (of type 'int*') to type
'std::string*' {aka 'class std::__cxx11::basic_string<char>*'} (source is not a
pointer to class)
    9 |     if (dynamic_cast<std::string *>(rawPtr)) {
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:9:9: error: cannot 'dynamic_cast' 'rawPtr' (of type 'int*') to type
'std::string*' {aka 'class std::__cxx11::basic_string<char>*'} (source is not a
pointer to class)
<source>: In function 'int main()':
<source>:14:25: error: invalid conversion from 'int' to '<lambda()> (*)[1]'
[-fpermissive]
   14 | int main() { spam<char>(1); }
      |                         ^
      |                         |
      |                         int

internal compiler error: error reporting routines re-entered.
0x288b1ee
diagnostics::context::report_diagnostic(diagnostics::diagnostic_info*)
        ???:0
0x288b305 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
        ???:0
0x288049f error_at(unsigned long, char const*, ...)
        ???:0
0xd9211f build_dynamic_cast(unsigned long, tree_node*, tree_node*, int)
        ???:0
0xd75de5 tsubst_lambda_expr(tree_node*, tree_node*, int, tree_node*)
        ???:0
0xd59a0d tsubst(tree_node*, tree_node*, int, tree_node*)
        ???:0
0x28eb883 pretty_printer::format(text_info&)
        ???:0
0x288ae4e
diagnostics::context::report_diagnostic(diagnostics::diagnostic_info*)
        ???:0
0x288b305 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
        ???:0
0x287f1a0 inform(rich_location*, char const*, ...)
        ???:0
0xb2e05e build_new_function_call(tree_node*, vec<tree_node*, va_gc,
vl_embed>**, int)
        ???:0
0xda6e4c finish_call_expr(tree_node*, vec<tree_node*, va_gc, vl_embed>**, bool,
bool, int)
        ???:0
0xd24503 c_parse_file()
        ???:0
0xe8f0f9 c_common_parse_file()
        ???:0
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Compiler returned: 1

Reply via email to