diff mbox

Add a testcase for C++17 selection statements with initializer

Message ID 20161005214741.GZ3223@redhat.com
State New
Headers show

Commit Message

Marek Polacek Oct. 5, 2016, 9:47 p.m. UTC
This adds a further test for selection statements with initializer C++17
feature.  Except that I'm not quite sure if it's valid, but I think it is;
although the standard says
An if statement of the form

  if constexpr opt ( init-statement condition ) statement

is equivalent to

  {
    init-statement
    if constexpr opt ( condition ) statement
  }

it also says "except that names declared in the init-statement are in the same
declarative region as those declared in the condition."
and program like

  if (int x = 4)
    {
      int x;
    }
is ill-formed.

Ok for trunk?

2016-10-05  Marek Polacek  <polacek@redhat.com>

	* g++.dg/cpp1z/init-statement9.C: New test.


	Marek

Comments

Jason Merrill Oct. 5, 2016, 11:06 p.m. UTC | #1
OK.

On Wed, Oct 5, 2016 at 5:47 PM, Marek Polacek <polacek@redhat.com> wrote:
> This adds a further test for selection statements with initializer C++17
> feature.  Except that I'm not quite sure if it's valid, but I think it is;
> although the standard says
> An if statement of the form
>
>   if constexpr opt ( init-statement condition ) statement
>
> is equivalent to
>
>   {
>     init-statement
>     if constexpr opt ( condition ) statement
>   }
>
> it also says "except that names declared in the init-statement are in the same
> declarative region as those declared in the condition."
> and program like
>
>   if (int x = 4)
>     {
>       int x;
>     }
> is ill-formed.
>
> Ok for trunk?
>
> 2016-10-05  Marek Polacek  <polacek@redhat.com>
>
>         * g++.dg/cpp1z/init-statement9.C: New test.
>
> diff --git gcc/testsuite/g++.dg/cpp1z/init-statement9.C gcc/testsuite/g++.dg/cpp1z/init-statement9.C
> index e69de29..5425f97 100644
> --- gcc/testsuite/g++.dg/cpp1z/init-statement9.C
> +++ gcc/testsuite/g++.dg/cpp1z/init-statement9.C
> @@ -0,0 +1,17 @@
> +// { dg-options -std=c++1z }
> +
> +void
> +f ()
> +{
> +  {
> +    int c;
> +    if (int c = 2; c != 0)
> +      int c = 4; // { dg-error "redeclaration" }
> +  }
> +
> +  if (int c = 2; c != 0)
> +    int c = 4; // { dg-error "redeclaration" }
> +
> +  if (int c = 2; int c = 6) // { dg-error "redeclaration" }
> +    int c = 4; // { dg-error "redeclaration" }
> +}
>
>         Marek
diff mbox

Patch

diff --git gcc/testsuite/g++.dg/cpp1z/init-statement9.C gcc/testsuite/g++.dg/cpp1z/init-statement9.C
index e69de29..5425f97 100644
--- gcc/testsuite/g++.dg/cpp1z/init-statement9.C
+++ gcc/testsuite/g++.dg/cpp1z/init-statement9.C
@@ -0,0 +1,17 @@ 
+// { dg-options -std=c++1z }
+
+void
+f ()
+{
+  {
+    int c;
+    if (int c = 2; c != 0)
+      int c = 4; // { dg-error "redeclaration" }
+  }
+
+  if (int c = 2; c != 0)
+    int c = 4; // { dg-error "redeclaration" }
+
+  if (int c = 2; int c = 6) // { dg-error "redeclaration" }
+    int c = 4; // { dg-error "redeclaration" }
+}