* [PATCH] c++: parse error with >= in template argument list [PR105436]
@ 2022-05-02 16:19 Marek Polacek
2022-05-03 20:26 ` Jason Merrill
0 siblings, 1 reply; 6+ messages in thread
From: Marek Polacek @ 2022-05-02 16:19 UTC (permalink / raw)
To: GCC Patches, Jason Merrill; +Cc: Jakub Jelinek
This patch fixes an oversight whereby we treated >= as the end of
a template argument. This causes problems in C++14, because in
cp_parser_template_argument we go different ways for C++14 and C++17:
/* It must be a non-type argument. In C++17 any constant-expression is
allowed. */
if (cxx_dialect > cxx14)
goto general_expr;
so in this testcase in C++14 we get "N" as the template argument but in
C++17 it is the whole "N >= 5" expression. So in C++14 the remaining
">= 5" triggered the newly-added diagnostic.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
PR c++/105436
gcc/cp/ChangeLog:
* parser.cc (cp_parser_next_token_ends_template_argument_p): Don't
return true for CPP_GREATER_EQ.
gcc/testsuite/ChangeLog:
* g++.dg/parse/template31.C: New test.
---
gcc/cp/parser.cc | 1 -
gcc/testsuite/g++.dg/parse/template31.C | 4 ++++
2 files changed, 4 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/parse/template31.C
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index a5cbb3e896f..5fa743b5a8e 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -33224,7 +33224,6 @@ cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
|| ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
/* For better diagnostics, treat >>= like that too, that
shouldn't appear non-nested in template arguments. */
- || token->type == CPP_GREATER_EQ
|| token->type == CPP_RSHIFT_EQ);
}
diff --git a/gcc/testsuite/g++.dg/parse/template31.C b/gcc/testsuite/g++.dg/parse/template31.C
new file mode 100644
index 00000000000..a5693e851f7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/template31.C
@@ -0,0 +1,4 @@
+// PR c++/105436
+
+template<bool> struct A;
+template<int N> A<N >= 5> f();
base-commit: 1cb220498e1f59021dab36c39c5d726e9f070c6a
--
2.35.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: parse error with >= in template argument list [PR105436]
2022-05-02 16:19 [PATCH] c++: parse error with >= in template argument list [PR105436] Marek Polacek
@ 2022-05-03 20:26 ` Jason Merrill
2022-05-03 20:43 ` Jakub Jelinek
0 siblings, 1 reply; 6+ messages in thread
From: Jason Merrill @ 2022-05-03 20:26 UTC (permalink / raw)
To: Marek Polacek, GCC Patches; +Cc: Jakub Jelinek
On 5/2/22 12:19, Marek Polacek wrote:
> This patch fixes an oversight whereby we treated >= as the end of
> a template argument. This causes problems in C++14, because in
> cp_parser_template_argument we go different ways for C++14 and C++17:
>
> /* It must be a non-type argument. In C++17 any constant-expression is
> allowed. */
> if (cxx_dialect > cxx14)
> goto general_expr;
>
> so in this testcase in C++14 we get "N" as the template argument but in
> C++17 it is the whole "N >= 5" expression. So in C++14 the remaining
> ">= 5" triggered the newly-added diagnostic.
Hmm, I think >>= is questionable as well, as it could resolve to a
constexpr operator>>=. Seems like the two calls to
cp_parser_next_token_ends_template_argument_p in the C++14 non-type
argument parsing code need to be strictly correct to avoid the problem
in this testcase; maybe we want a separate overload or another parameter
to indicate that?
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
> PR c++/105436
>
> gcc/cp/ChangeLog:
>
> * parser.cc (cp_parser_next_token_ends_template_argument_p): Don't
> return true for CPP_GREATER_EQ.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/parse/template31.C: New test.
> ---
> gcc/cp/parser.cc | 1 -
> gcc/testsuite/g++.dg/parse/template31.C | 4 ++++
> 2 files changed, 4 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/parse/template31.C
>
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index a5cbb3e896f..5fa743b5a8e 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -33224,7 +33224,6 @@ cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
> || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
> /* For better diagnostics, treat >>= like that too, that
> shouldn't appear non-nested in template arguments. */
> - || token->type == CPP_GREATER_EQ
> || token->type == CPP_RSHIFT_EQ);
> }
>
> diff --git a/gcc/testsuite/g++.dg/parse/template31.C b/gcc/testsuite/g++.dg/parse/template31.C
> new file mode 100644
> index 00000000000..a5693e851f7
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/parse/template31.C
> @@ -0,0 +1,4 @@
> +// PR c++/105436
> +
> +template<bool> struct A;
> +template<int N> A<N >= 5> f();
>
> base-commit: 1cb220498e1f59021dab36c39c5d726e9f070c6a
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: parse error with >= in template argument list [PR105436]
2022-05-03 20:26 ` Jason Merrill
@ 2022-05-03 20:43 ` Jakub Jelinek
2022-05-03 20:46 ` Jason Merrill
0 siblings, 1 reply; 6+ messages in thread
From: Jakub Jelinek @ 2022-05-03 20:43 UTC (permalink / raw)
To: Jason Merrill; +Cc: Marek Polacek, GCC Patches
On Tue, May 03, 2022 at 04:26:51PM -0400, Jason Merrill wrote:
> On 5/2/22 12:19, Marek Polacek wrote:
> > This patch fixes an oversight whereby we treated >= as the end of
> > a template argument. This causes problems in C++14, because in
> > cp_parser_template_argument we go different ways for C++14 and C++17:
> >
> > /* It must be a non-type argument. In C++17 any constant-expression is
> > allowed. */
> > if (cxx_dialect > cxx14)
> > goto general_expr;
> >
> > so in this testcase in C++14 we get "N" as the template argument but in
> > C++17 it is the whole "N >= 5" expression. So in C++14 the remaining
> > ">= 5" triggered the newly-added diagnostic.
>
> Hmm, I think >>= is questionable as well, as it could resolve to a constexpr
> operator>>=. Seems like the two calls to
The template argument is a constant-expression and >>= can't appear non-nested
in constant-expression non-terminal, can it?
>= certainly can.
Jakub
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: parse error with >= in template argument list [PR105436]
2022-05-03 20:43 ` Jakub Jelinek
@ 2022-05-03 20:46 ` Jason Merrill
2022-05-04 15:15 ` Marek Polacek
0 siblings, 1 reply; 6+ messages in thread
From: Jason Merrill @ 2022-05-03 20:46 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: Marek Polacek, GCC Patches
On 5/3/22 16:43, Jakub Jelinek wrote:
> On Tue, May 03, 2022 at 04:26:51PM -0400, Jason Merrill wrote:
>> On 5/2/22 12:19, Marek Polacek wrote:
>>> This patch fixes an oversight whereby we treated >= as the end of
>>> a template argument. This causes problems in C++14, because in
>>> cp_parser_template_argument we go different ways for C++14 and C++17:
>>>
>>> /* It must be a non-type argument. In C++17 any constant-expression is
>>> allowed. */
>>> if (cxx_dialect > cxx14)
>>> goto general_expr;
>>>
>>> so in this testcase in C++14 we get "N" as the template argument but in
>>> C++17 it is the whole "N >= 5" expression. So in C++14 the remaining
>>> ">= 5" triggered the newly-added diagnostic.
>>
>> Hmm, I think >>= is questionable as well, as it could resolve to a constexpr
>> operator>>=. Seems like the two calls to
>
> The template argument is a constant-expression and >>= can't appear non-nested
> in constant-expression non-terminal, can it?
Ah, true, a constant-expression is a conditional-expression, which can't
involve an assignment operator.
Jason
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: parse error with >= in template argument list [PR105436]
2022-05-03 20:46 ` Jason Merrill
@ 2022-05-04 15:15 ` Marek Polacek
2022-05-04 15:55 ` Jason Merrill
0 siblings, 1 reply; 6+ messages in thread
From: Marek Polacek @ 2022-05-04 15:15 UTC (permalink / raw)
To: Jason Merrill; +Cc: Jakub Jelinek, GCC Patches
On Tue, May 03, 2022 at 04:46:11PM -0400, Jason Merrill wrote:
> On 5/3/22 16:43, Jakub Jelinek wrote:
> > On Tue, May 03, 2022 at 04:26:51PM -0400, Jason Merrill wrote:
> > > On 5/2/22 12:19, Marek Polacek wrote:
> > > > This patch fixes an oversight whereby we treated >= as the end of
> > > > a template argument. This causes problems in C++14, because in
> > > > cp_parser_template_argument we go different ways for C++14 and C++17:
> > > >
> > > > /* It must be a non-type argument. In C++17 any constant-expression is
> > > > allowed. */
> > > > if (cxx_dialect > cxx14)
> > > > goto general_expr;
> > > >
> > > > so in this testcase in C++14 we get "N" as the template argument but in
> > > > C++17 it is the whole "N >= 5" expression. So in C++14 the remaining
> > > > ">= 5" triggered the newly-added diagnostic.
> > >
> > > Hmm, I think >>= is questionable as well, as it could resolve to a constexpr
> > > operator>>=. Seems like the two calls to
> >
> > The template argument is a constant-expression and >>= can't appear non-nested
> > in constant-expression non-terminal, can it?
>
> Ah, true, a constant-expression is a conditional-expression, which can't
> involve an assignment operator.
So do you want me to make any changes or is the patch OK as-is?
Marek
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] c++: parse error with >= in template argument list [PR105436]
2022-05-04 15:15 ` Marek Polacek
@ 2022-05-04 15:55 ` Jason Merrill
0 siblings, 0 replies; 6+ messages in thread
From: Jason Merrill @ 2022-05-04 15:55 UTC (permalink / raw)
To: Marek Polacek; +Cc: Jakub Jelinek, GCC Patches
On 5/4/22 11:15, Marek Polacek wrote:
> On Tue, May 03, 2022 at 04:46:11PM -0400, Jason Merrill wrote:
>> On 5/3/22 16:43, Jakub Jelinek wrote:
>>> On Tue, May 03, 2022 at 04:26:51PM -0400, Jason Merrill wrote:
>>>> On 5/2/22 12:19, Marek Polacek wrote:
>>>>> This patch fixes an oversight whereby we treated >= as the end of
>>>>> a template argument. This causes problems in C++14, because in
>>>>> cp_parser_template_argument we go different ways for C++14 and C++17:
>>>>>
>>>>> /* It must be a non-type argument. In C++17 any constant-expression is
>>>>> allowed. */
>>>>> if (cxx_dialect > cxx14)
>>>>> goto general_expr;
>>>>>
>>>>> so in this testcase in C++14 we get "N" as the template argument but in
>>>>> C++17 it is the whole "N >= 5" expression. So in C++14 the remaining
>>>>> ">= 5" triggered the newly-added diagnostic.
>>>>
>>>> Hmm, I think >>= is questionable as well, as it could resolve to a constexpr
>>>> operator>>=. Seems like the two calls to
>>>
>>> The template argument is a constant-expression and >>= can't appear non-nested
>>> in constant-expression non-terminal, can it?
>>
>> Ah, true, a constant-expression is a conditional-expression, which can't
>> involve an assignment operator.
>
> So do you want me to make any changes or is the patch OK as-is?
OK.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-05-04 15:55 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-02 16:19 [PATCH] c++: parse error with >= in template argument list [PR105436] Marek Polacek
2022-05-03 20:26 ` Jason Merrill
2022-05-03 20:43 ` Jakub Jelinek
2022-05-03 20:46 ` Jason Merrill
2022-05-04 15:15 ` Marek Polacek
2022-05-04 15:55 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).