[Bug c++/78890] [5/6/7 Regression] ICE on invalid reference type in union

jakub at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Dec 22 11:09:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78890

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Note that clang++ rejects this in all C++ modes.  So, are reference types
really allowed in anonymous unions?  If they are valid, the ICE is because the
VAR_DECL b has REFERENCE_TYPE, but also has DECL_VALUE_EXPR which has type int
rather than int &.

Untested:

--- gcc/cp/decl2.c.jj   2016-11-15 16:18:49.000000000 +0100
+++ gcc/cp/decl2.c      2016-12-22 11:59:14.656688431 +0100
@@ -1510,6 +1510,10 @@ build_anon_union_vars (tree type, tree o
          TREE_STATIC (decl) = TREE_STATIC (base);
          DECL_EXTERNAL (decl) = DECL_EXTERNAL (base);

+         if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE
+             && REFERENCE_REF_P (ref))
+           ref = TREE_OPERAND (ref, 0);
+
          SET_DECL_VALUE_EXPR (decl, ref);
          DECL_HAS_VALUE_EXPR_P (decl) = 1;

fixes the ICE, even
template <typename T>
int foo() {
  union {
    int a;
    int &b = a;
  };
  a = 1;
  auto c = b + 1;
  return c;
}
template <typename T>
T bar() {
  union {
    T a;
    T &b = a;
  };
  a = 1;
  auto c = b + 1;
  return c;
}
template <typename T, typename U>
T baz() {
  union {
    T a;
    U b = a;
  };
  a = 1;
  auto c = b + 1;
  return c;
}
int a = foo<int> ();
int b = bar<int> ();
int c = baz<int, int &> ();
compiles.  But it doesn't make sense to spend further time on this until it is
clear if it is valid or not.


More information about the Gcc-bugs mailing list