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

            Bug ID: 99902
           Summary: Deduced return type of lambda in default template
                    argument takes return type from variable template
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jason.e.cobb at gmail dot com
  Target Milestone: ---

In the following code:

[BEGIN EXAMPLE]

template<void(*)() = []{ return; }>
struct foo {};

template<typename T>
inline constexpr auto foo_v = foo<>{};

auto x = foo_v<long>;

[END EXAMPLE]

On Compiler Explorer: https://godbolt.org/z/nor57d4Ke

GCC believes the return type of the lambda to be long, even though the return
type should be deduced as void. This is apparent from the error message that
GCC rejects with:

[BEGIN ERROR]

<source>: In instantiation of 'constexpr const auto foo_v<long int>':
<source>:7:10:   required from here
<source>:1:26: error: return-statement with no value, in function returning
'long int' [-fpermissive]
    1 | template<void(*)() = []{ return; }>
      |                          ^~~~~~
<source>:5:23: error: conversion from 'long int (*)()' to 'void (*)()' in a
converted constant expression
    5 | inline constexpr auto foo_v = foo<>{};
      |                       ^~~~~
<source>:5:23: error: could not convert '<lambda closure object><lambda()>{}'
from '<lambda()>' to 'void (*)()'
Compiler returned: 1

[END ERROR]

GCC rejects with a similar error message in the following case with an alias
template instead of a variable template:

[BEGIN EXAMPLE]

template<void(*)() = []{ return; }>
struct foo {};

template<typename T>
using foo_t = foo<>;

using some_foo = foo_t<long>;

[END EXAMPLE]

In the case where foo_t or foo_v has multiple template arguments, the first is
taken to be the return type.

In the case where the first template parameter foo_t or foo_v is a non-type
template parameter, GCC ICEs:

[BEGIN EXAMPLE]

template<void(*)() = []{ return; }>
struct foo {};

template<int>
using foo_t = foo<>;

using some_foo = foo_t<12>;

[END EXAMPLE]

On Compiler Explorer: https://godbolt.org/z/MoPEGzYP7

This results in the following error:

[BEGIN ERROR]

<source>: In substitution of 'template<int <anonymous> > using foo_t = foo<>
[with int <anonymous> = 12]':
<source>:7:26:   required from here
<source>:1:22: internal compiler error: in tsubst, at cp/pt.c:15578
    1 | template<void(*)() = []{ return; }>
      |                      ^~~~~~~~~~~~~
0x1cfd119 internal_error(char const*, ...)
        ???:0
0x6baa1b fancy_abort(char const*, int, char const*)
        ???:0
0x9182c1 tsubst(tree_node*, tree_node*, int, tree_node*)
        ???:0
0x916211 tsubst(tree_node*, tree_node*, int, tree_node*)
        ???:0
0x949897 tsubst_lambda_expr(tree_node*, tree_node*, int, tree_node*)
        ???:0
0x941fc6 tsubst_template_args(tree_node*, tree_node*, int, tree_node*)
        ???:0
0x91658d tsubst(tree_node*, tree_node*, int, tree_node*)
        ???:0
0x916637 tsubst(tree_node*, tree_node*, int, tree_node*)
        ???:0
0x94ce32 instantiate_template(tree_node*, tree_node*, int)
        ???:0
0x916b31 tsubst(tree_node*, tree_node*, int, tree_node*)
        ???:0
0x92a38e lookup_template_class(tree_node*, tree_node*, tree_node*, tree_node*,
int, int)
        ???:0
0x97ff1d finish_template_type(tree_node*, tree_node*, int)
        ???:0
0x8e140d c_parse_file()
        ???:0
0xa606f2 c_common_parse_file()
        ???:0
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.

[END ERROR]

On Compiler Explorer: https://godbolt.org/z/713js5aq7

Reply via email to