Skip to content

Commit c572536

Browse files
Show why incomplete auto is not diagnosed in a template definition. [GH #27]
1 parent 4a6753d commit c572536

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎src/Changes‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ is for decltype(auto). For example:
1313
auto *p = g(); // Error: cannot deduce "auto" type
1414

1515
Forms that are not a plain "auto", such as "auto *", still fail deduction.
16+
The diagnostic is not issued while scanning a template definition, because
17+
the initializer type may be completed before the template is instantiated.
1618

1719

1820
10/5/26 [GH #195]

‎src/overload.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31244,9 +31244,11 @@ TRUE and FALSE is returned.
3124431244
/* Adjust the argument and parameter types for deduction. Some types can
3124531245
never succeed: Issue an error and don't attempt deduction any
3124631246
further. */
31247-
/* In a prototype instantiation a deduction failure is often
31248-
suppressed. Keep that behavior for an incomplete initializer:
31249-
the incomplete-type diagnostic is issued at instantiation. */
31247+
/* Do not deduce an incomplete type while scanning a template
31248+
definition. The type may be completed before instantiation, and
31249+
a deduction failure here is suppressed in GNU and Microsoft modes.
31250+
Diagnosing an incomplete type instead would reject a valid
31251+
template. See GH27. */
3125031252
a_boolean allow_incomplete_arg =
3125131253
!scope_stack_top().in_prototype_instantiation;
3125231254
if (!adjust_deduction_pair(&type, &arg_type, initializer_operand,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
fe_only -DTEST_NUMBER=2 --c++23 --gnu_version=999999 -DSHOW_TEMPLATE_DEFN Test_name.c
2+
"Test_name.c", line 27: error: incomplete type "void" is not allowed
3+
auto v1 = void();
4+
^
5+
6+
"Test_name.c", line 28: error: incomplete type "void" is not allowed
7+
decltype(auto) v2 = void();
8+
^
9+
10+
"Test_name.c", line 29: error: incomplete type "Incomplete" is not allowed
11+
auto v3 = getIncomplete();
12+
^
13+
14+
"Test_name.c", line 30: error: incomplete type "void" is not allowed
15+
auto opts = Args{}.parse();
16+
^
17+
18+
"Test_name.c", line 32: error: cannot deduce "auto" type
19+
auto *v4 = void();
20+
^
21+
22+
"Test_name.c", line 33: error: cannot deduce "auto" type
23+
auto *v5 = 1;
24+
^
25+
26+
"Test_name.c", line 34: error: cannot deduce "auto" type
27+
auto *p = g();
28+
^
29+
30+
7 errors detected in the compilation of "Test_name.c".

‎tests/tests/changes/GH27.sft.cpp‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//type:fn
2-
//options_all:--c++23
2+
//options:--c++23:--c++23 --gnu_version=999999 -DSHOW_TEMPLATE_DEFN
33
//remark:[GH #27] Clearer diagnostic when auto deduces to void
44
// 10/6/26 [GH #27]
55
//
@@ -33,3 +33,23 @@ void f()
3333
auto *v5 = 1;
3434
auto *p = g();
3535
}
36+
37+
#if SHOW_TEMPLATE_DEFN
38+
/* Incomplete in the template definition, complete before instantiation.
39+
The definition must not be rejected. */
40+
struct Later;
41+
extern Later later_obj;
42+
43+
template<int>
44+
void use_later()
45+
{
46+
auto x = later_obj;
47+
}
48+
49+
struct Later { int n; };
50+
51+
void call_later()
52+
{
53+
use_later<0>();
54+
}
55+
#endif /* SHOW_TEMPLATE_DEFN */

0 commit comments

Comments
 (0)