[C++] Fix nsdmi handling for bitfields (PR c++/92732)

Message ID 20191129233508.GP10088@tucnak
State New
Headers show
Series
  • [C++] Fix nsdmi handling for bitfields (PR c++/92732)
Related show

Commit Message

Jakub Jelinek Nov. 29, 2019, 11:35 p.m.
Hi!

As the second testcase shows, we shouldn't be calling convert_for_*
with TREE_TYPE (decl) for bitfields, we need DECL_BIT_FIELD_TYPE
in that case instead (unlowered_expr_type doesn't work here,
as that wants a COMPONENT_REF which we don't have).

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2019-11-29  Jakub Jelinek  <jakub@redhat.com>

	PR c++/92732
	* typeck2.c (digest_nsdmi_init): For bitfields, use
	DECL_BIT_FIELD_TYPE instead of TREE_TYPE.

	* g++.dg/cpp2a/bitfield3.C: Don't expect narrowing conversion
	warnings.
	* g++.dg/cpp2a/bitfield4.C: New test.


	Jakub

Comments

Jason Merrill Dec. 2, 2019, 5:19 p.m. | #1
On 11/29/19 6:35 PM, Jakub Jelinek wrote:
> Hi!

> 

> As the second testcase shows, we shouldn't be calling convert_for_*

> with TREE_TYPE (decl) for bitfields, we need DECL_BIT_FIELD_TYPE

> in that case instead (unlowered_expr_type doesn't work here,

> as that wants a COMPONENT_REF which we don't have).

> 

> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

> 

> 2019-11-29  Jakub Jelinek  <jakub@redhat.com>

> 

> 	PR c++/92732

> 	* typeck2.c (digest_nsdmi_init): For bitfields, use

> 	DECL_BIT_FIELD_TYPE instead of TREE_TYPE.

> 

> 	* g++.dg/cpp2a/bitfield3.C: Don't expect narrowing conversion

> 	warnings.

> 	* g++.dg/cpp2a/bitfield4.C: New test.

> 

> --- gcc/cp/typeck2.c.jj	2019-11-25 22:44:24.911346688 +0100

> +++ gcc/cp/typeck2.c	2019-11-29 21:48:58.825713516 +0100

> @@ -1335,6 +1335,9 @@ digest_nsdmi_init (tree decl, tree init,

>     gcc_assert (TREE_CODE (decl) == FIELD_DECL);

>   

>     tree type = TREE_TYPE (decl);

> +  if (DECL_BIT_FIELD_TYPE (decl))

> +    type = cp_build_qualified_type (DECL_BIT_FIELD_TYPE (decl),

> +				    cp_type_quals (type));


I don't think we need to add qualifiers to a scalar initializer, they'll 
just get discarded again.  OK with that change.

Jason

Patch

--- gcc/cp/typeck2.c.jj	2019-11-25 22:44:24.911346688 +0100
+++ gcc/cp/typeck2.c	2019-11-29 21:48:58.825713516 +0100
@@ -1335,6 +1335,9 @@  digest_nsdmi_init (tree decl, tree init,
   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
 
   tree type = TREE_TYPE (decl);
+  if (DECL_BIT_FIELD_TYPE (decl))
+    type = cp_build_qualified_type (DECL_BIT_FIELD_TYPE (decl),
+				    cp_type_quals (type));
   int flags = LOOKUP_IMPLICIT;
   if (DIRECT_LIST_INIT_P (init))
     {
--- gcc/testsuite/g++.dg/cpp2a/bitfield3.C.jj	2017-09-29 19:49:17.684827064 +0200
+++ gcc/testsuite/g++.dg/cpp2a/bitfield3.C	2019-11-29 21:52:56.636122855 +0100
@@ -15,11 +15,9 @@  const int b = 0;
 struct S {
   int c : 5 = 2 * a;			// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
   int d : 6 { c + a };			// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
-					// { dg-warning "narrowing conversion of" "" { target *-*-* } .-1 }
   int e : true ? 7 : a = 3;
   int f : (true ? 8 : b) = d + a;	// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
   int g : (true ? 9 : b) { f + a };	// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
-					// { dg-warning "narrowing conversion of" "" { target *-*-* } .-1 }
   int h : 1 || new int { 0 };
   int i = g + a;
 };
@@ -28,11 +26,9 @@  template <bool V, int W>
 struct U {
   int j : W = 3 * a;			// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
   int k : W { j + a };			// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
-					// { dg-warning "narrowing conversion of" "" { target *-*-* } .-1 }
   int l : V ? 7 : a = 3;
   int m : (V ? W : b) = k + a;		// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
   int n : (V ? W : b) { m + a };	// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
-					// { dg-warning "narrowing conversion of" "" { target *-*-* } .-1 }
   int o : 1 || new int { 0 };
   int p = n + a;
 };
--- gcc/testsuite/g++.dg/cpp2a/bitfield4.C.jj	2019-11-29 21:48:28.752167687 +0100
+++ gcc/testsuite/g++.dg/cpp2a/bitfield4.C	2019-11-29 21:52:40.548365714 +0100
@@ -0,0 +1,12 @@ 
+// PR c++/92732
+// { dg-do compile { target c++17 } }
+// { dg-options "" }
+
+enum class byte : unsigned char { };
+using uint8_t = unsigned char;
+
+struct T
+{
+  byte a : 2 = byte{0};	// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
+  uint8_t b : 2 = 0;	// { dg-warning "default member initializers for bit-fields only available with" "" { target c++17_down } }
+} t;