Skip to content

Commit 706ebc8

Browse files
JohelEGPhsutter
andauthored
fix(parse): recognize const and pointer template arguments (#521)
* fix(parse): recognize const and pointer template arguments * Minor: Massage the new code's format and comment Signed-off-by: Herb Sutter <herb.sutter@gmail.com> * Update parse.h Signed-off-by: Herb Sutter <herb.sutter@gmail.com> --------- Signed-off-by: Herb Sutter <herb.sutter@gmail.com> Co-authored-by: Herb Sutter <herb.sutter@gmail.com>
1 parent 9ec94ca commit 706ebc8

6 files changed

+47
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
main: () = //
2+
std::is_void_v<* i32> //
3+
&& std::is_void_v<const i32>;

regression-tests/test-results/gcc-13/pure2-bugfix-for-template-argument.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/pure2-bugfix-for-template-argument.cpp.output

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
#line 1 "pure2-bugfix-for-template-argument.cpp2"
14+
auto main() -> int;
15+
16+
17+
//=== Cpp2 function definitions =================================================
18+
19+
#line 1 "pure2-bugfix-for-template-argument.cpp2"
20+
auto main() -> int { //
21+
std::is_void_v<cpp2::i32*> //
22+
&& std::is_void_v<cpp2::i32 const>; }
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-bugfix-for-template-argument.cpp2... ok (all Cpp2, passes safety checks)
2+

source/parse.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -4822,6 +4822,7 @@ class parser
48224822
//G
48234823
//G template-argument:
48244824
//G # note: < > << >> are not allowed in expressions until new ( is opened
4825+
//G 'const' type-id
48254826
//G expression
48264827
//G type-id
48274828
//G
@@ -4855,16 +4856,32 @@ class parser
48554856
auto term = unqualified_id_node::term{};
48564857

48574858
do {
4858-
// disallow unparenthesized relational comparisons in template args
4859-
if (auto e = expression(false)) {
4859+
// If it doesn't start with * or const (which can only be a type id),
4860+
// try parsing it as an expression
4861+
if (auto e = [&]{
4862+
if (
4863+
curr().type() == lexeme::Multiply // '*'
4864+
|| curr() == "const" // 'const'
4865+
)
4866+
{
4867+
return decltype(expression()){};
4868+
}
4869+
return expression(false); // false == disallow unparenthesized relational comparisons in template args
4870+
}()
4871+
)
4872+
{
48604873
term.arg = std::move(e);
48614874
}
4875+
4876+
// Else try parsing it as a type id
48624877
else if (auto i = type_id()) {
48634878
term.arg = std::move(i);
48644879
}
4880+
48654881
else {
48664882
break;
48674883
}
4884+
48684885
n->template_args.push_back( std::move(term) );
48694886
}
48704887
// Use the lambda trick to jam in a "next" clause

0 commit comments

Comments
 (0)