Bug 86002

Summary: ICE with requires in constexpr if condition
Product: gcc Reporter: Hubert Matthews <gcc-bugs>
Component: c++Assignee: Marek Polacek <mpolacek>
Status: RESOLVED FIXED    
Severity: normal CC: mpolacek, webrown.cpp
Priority: P3 Keywords: ice-on-valid-code
Version: 8.1.1   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2020-07-23 00:00:00
Attachments: Pre-processed source and command-line options

Description Hubert Matthews 2018-05-30 18:08:45 UTC
Created attachment 44214 [details]
Pre-processed source and command-line options

// ICE when using requires to detect welll-formedness of code using constexpr if

struct X {};
struct Y { int i; };

template <typename T>
int f(T t)
{
    if constexpr (requires { t.i; })
        return t.i;
    else
        return {};
}

int main()
{
    return f(X{}) + f(Y{});
}
Comment 1 Marek Polacek 2020-07-23 19:29:30 UTC
GCC 9 ICEs

86002.C: In instantiation of ‘int f(T) [with T = X]’:
86002.C:15:17:   required from here
86002.C:7:30: internal compiler error: in tsubst_copy, at cp/pt.c:15680
    7 |     if constexpr (requires { t.i; })
      |                              ^
0xa07611 tsubst_copy
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:15680
0xa1dd78 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:19676
0xa1e716 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:19794
0xa16968 tsubst_non_call_postfix_expression
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:18159
0xa1c97b tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:19455
0xa16347 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:18067
0x8a018e tsubst_simple_requirement
	/home/mpolacek/src/gcc9/gcc/cp/constraint.cc:1778
0x8a018e tsubst_requirement
	/home/mpolacek/src/gcc9/gcc/cp/constraint.cc:1836
0x8a018e tsubst_requirement_body
	/home/mpolacek/src/gcc9/gcc/cp/constraint.cc:1859
0x8a018e tsubst_requires_expr(tree_node*, tree_node*, int, tree_node*)
	/home/mpolacek/src/gcc9/gcc/cp/constraint.cc:1891
0xa1e641 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:19782
0xa16347 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:18067
0xa11390 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:17417
0xa11afb tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:17472
0xa34241 instantiate_decl(tree_node*, bool, bool)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:24975
0xa34c1a instantiate_pending_templates(int)
	/home/mpolacek/src/gcc9/gcc/cp/pt.c:25091
0x915c43 c_parse_final_cleanups()
	/home/mpolacek/src/gcc9/gcc/cp/decl2.c:4818

But GCC 10+ compiles it fine.
Comment 2 GCC Commits 2020-09-17 20:14:59 UTC
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:4f0aa5b051c0d3e81478bcb495e4e072b2d9827d

commit r11-3268-g4f0aa5b051c0d3e81478bcb495e4e072b2d9827d
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Sep 17 15:31:50 2020 -0400

    c++: Add tests for fixed PRs.
    
    Bugzilla inspection turned up a bunch of old(er) PRs that have been
    fixed.  Let's include them not to regress in the future.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/87530
            PR c++/58156
            PR c++/68828
            PR c++/86002
            PR c++/91525
            PR c++/96223
            PR c++/87032
            PR c++/35098
            * g++.dg/cpp0x/move-return4.C: New test.
            * g++.dg/cpp0x/vt-58156.C: New test.
            * g++.dg/cpp2a/concepts-pr68828.C: New test.
            * g++.dg/cpp2a/concepts-pr86002.C: New test.
            * g++.dg/cpp2a/concepts-pr91525.C: New test.
            * g++.dg/cpp2a/constexpr-indeterminate1.C: New test.
            * g++.dg/cpp2a/desig17.C: New test.
            * g++.dg/ext/attrib62.C: New test.
Comment 3 Marek Polacek 2020-09-17 20:18:29 UTC
Fixed.