Bug 93689 - ICE with default argument in lambda used as non type template argument
Summary: ICE with default argument in lambda used as non type template argument
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2020-02-11 21:25 UTC by malle
Modified: 2024-04-15 22:23 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-04-15 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description malle 2020-02-11 21:25:16 UTC
This code

```
template <
    auto Z = [](int l = int(0)) -> int { return l; }
>
int f() {
    return Z();
}

int main() {
    return f();
}
```

causes this error

```
<source>: In function 'int f() [with auto Z = <lambda closure object>main()::<lambda(int)>{}]':
<source>:5:13: internal compiler error: in gimplify_expr, at gimplify.c:12489
    5 |     return Z();
      |            ~^~
Please submit a full bug report,
with preprocessed source if appropriate.
See <https://gcc.gnu.org/bugs/> for instructions.
ASM generation compiler returned: 1
```

from `g++ (Compiler-Explorer-Build) 9.2.0` compiled with these options

```
-g -o /tmp/compiler-explorer-compiler120111-1689-nlrcj.y9iua/output.s -fdiagnostics-color=always -Wl,-rpath,/opt/compiler-explorer/gcc-9.2.0/lib -Wl,-rpath,/opt/compiler-explorer/gcc-9.2.0/lib32 -Wl,-rpath,/opt/compiler-explorer/gcc-9.2.0/lib64 -std=c++2a /tmp/compiler-explorer-compiler120111-1689-nlrcj.y9iua/example.cpp
```

See it on the compiler explorer: https://godbolt.org/z/TD8aHC

----

The reason I want to do this is to get source_location::current from an operator+'s call site.  Since the operator cannot take an extra argument, I tried template arguments and then a template argument lambda that returns a source location.
Comment 1 Marek Polacek 2020-02-11 21:33:35 UTC
Confirmed.

$ ./cc1plus -quiet  93689.C -std=c++2a 
93689.C: In instantiation of ‘int f() [with auto Z = <lambda closure object>main()::<lambda(int)>{}]’:
93689.C:9:14:   required from here
93689.C:5:14: internal compiler error: unexpected expression ‘(int)(0)’ of kind cast_expr
    5 |     return Z();
      |              ^
0x9a013c cxx_eval_constant_expression
	/home/mpolacek/src/gcc/gcc/cp/constexpr.c:6203
0x98cafa cxx_bind_parameters_in_call
	/home/mpolacek/src/gcc/gcc/cp/constexpr.c:1522
0x99018a cxx_eval_call_expression
	/home/mpolacek/src/gcc/gcc/cp/constexpr.c:2291
0x99ce51 cxx_eval_constant_expression
	/home/mpolacek/src/gcc/gcc/cp/constexpr.c:5395
0x9a14a5 cxx_eval_outermost_constant_expr
	/home/mpolacek/src/gcc/gcc/cp/constexpr.c:6404
0x9a2641 maybe_constant_value(tree_node*, tree_node*, bool, bool)
	/home/mpolacek/src/gcc/gcc/cp/constexpr.c:6692
0xa94da4 fold_for_warn(tree_node*)
	/home/mpolacek/src/gcc/gcc/cp/expr.c:401
0xcb3a85 maybe_warn_about_returning_address_of_local
	/home/mpolacek/src/gcc/gcc/cp/typeck.c:9472
0xcb6c2f check_return_expr(tree_node*, bool*)
	/home/mpolacek/src/gcc/gcc/cp/typeck.c:10081
0xc358a5 finish_return_stmt(tree_node*)
	/home/mpolacek/src/gcc/gcc/cp/semantics.c:955
0xbe0b11 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	/home/mpolacek/src/gcc/gcc/cp/pt.c:17691
0xbe3622 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	/home/mpolacek/src/gcc/gcc/cp/pt.c:17999
0xc07109 instantiate_decl(tree_node*, bool, bool)
	/home/mpolacek/src/gcc/gcc/cp/pt.c:25523
0xc07ac6 instantiate_pending_templates(int)
	/home/mpolacek/src/gcc/gcc/cp/pt.c:25639
0xa6c433 c_parse_final_cleanups()
	/home/mpolacek/src/gcc/gcc/cp/decl2.c:4875
0xd4adb6 c_common_parse_file()
	/home/mpolacek/src/gcc/gcc/c-family/c-opts.c:1208
Comment 2 Marek Polacek 2020-02-11 21:52:20 UTC
The test was rejected with various errors, then with r9-4045-g0c1e0d63fe0ceabbd04384070f3b59f8bf50de09 we got this ICE:

93689.C: In function ‘int f() [with auto Z = <lambda closure object>main()::<lambda(int)>{}]’:
93689.C:5:13: internal compiler error: in gimplify_expr, at gimplify.c:12491
    5 |     return Z();
      |            ~^~
0x6dbdf3 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*), int)
	/home/mpolacek/src/gcc9/gcc/gimplify.c:12491
0xceaa9d gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*), int)
	/home/mpolacek/src/gcc9/gcc/gimplify.c:13226
0xcf3ce4 gimplify_expr
	/home/mpolacek/src/gcc9/gcc/gimplify.c:13512
0xcf5965 gimplify_call_expr
	/home/mpolacek/src/gcc9/gcc/gimplify.c:3405
...

and since r10-6527-gaaa26bf496a646778ac861aed124d960b5bf549f the ICE in Comment 1.
Comment 3 malle 2020-02-11 22:20:42 UTC
@Marek Polacek do you (or anyone) think this is a good first issue?  I am curious to try contributing.
Comment 4 Marek Polacek 2020-02-11 22:58:32 UTC
That's hard to say without really understanding what the issue is, but I'm afraid this might not be the best first issue -- it involves some pretty convoluted features, plus it seems there are two issues (that maybe_constant_value gets a template code and the gimplifier ICE).  "Luckily" we have over 3000 C++ issues and some of them should be more straightforward:

https://gcc.gnu.org/bugzilla/buglist.cgi?component=c%2B%2B&limit=0&list_id=240106&product=gcc&query_format=advanced&resolution=---&order=bug_id%20DESC&query_based_on=

Note that we're in stage 4 now, meaning that we only fix regressions.  Good luck.
Comment 5 Marek Polacek 2020-02-11 23:02:44 UTC
Actually the issue might be just one, even the gimplifier ICE seems to be caused by a CAST_EXPR leaking where it should not.  Maybe we fail to substitute default arguments in lambdas in a template parameter list.
Comment 6 bastien penavayre 2020-09-02 10:39:30 UTC
It seems that it's the cast '(int)0' that causes the issue.
If we remove the cast it works fine:

```
template <
    auto Z = [](int l = 0) -> int { return l; }
>
int f() {
    return Z();
}

int main() {
    return f();
}
```

https://godbolt.org/z/zq3E7M
Comment 7 bastien penavayre 2020-09-02 10:55:46 UTC
somewhat related 93595 ( https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93595 ) is still in UNCONFIRMED state.