(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
c2x-enum-7.c
       1  /* Test C2x enumerations with fixed underlying type.  Invalid code.  */
       2  /* { dg-do compile } */
       3  /* { dg-options "-std=c2x -pedantic-errors" } */
       4  
       5  /* An enum type specifier may only be used when the enum is defined, or in a
       6     declaration of the form "enum name enum-type-specifier;".  */
       7  extern enum e1 : int; /* { dg-error "storage class specifier in empty declaration with 'enum' underlying type" } */
       8  _Thread_local enum e2 : short; /* { dg-error "'_Thread_local' in empty declaration with 'enum' underlying type" } */
       9  const enum e3 : long; /* { dg-error "type qualifier in empty declaration with 'enum' underlying type" } */
      10  alignas (8) enum e4 : long; /* { dg-error "'alignas' in empty declaration with 'enum' underlying type" } */
      11  inline enum e5 : unsigned; /* { dg-error "'inline' in empty declaration" } */
      12  _Noreturn enum e6 : unsigned; /* { dg-error "'_Noreturn' in empty declaration" } */
      13  auto enum e7 : unsigned; /* { dg-error "'auto' in file-scope empty declaration" } */
      14  register enum e8 : unsigned; /* { dg-error "'register' in file-scope empty declaration" } */
      15  
      16  /* When the enum is defined, some extra declaration specifiers are permitted,
      17     but diagnosed as useless.  */
      18  extern enum e9 : int { E9 }; /* { dg-warning "useless storage class specifier in empty declaration" } */
      19  _Thread_local enum e10 : short { E10 }; /* { dg-warning "useless '_Thread_local' in empty declaration" } */
      20  const enum e11 : long { E11 }; /* { dg-warning "useless type qualifier in empty declaration" } */
      21  alignas (8) enum e12 : long { E12 }; /* { dg-warning "useless '_Alignas' in empty declaration" } */
      22  
      23  /* Nothing else may be declared with an enum type specifier for an enum not
      24     being defined in that declaration.  */
      25  enum e13 : short x13; /* { dg-error "'enum' underlying type may not be specified here" } */
      26  enum e14 : short f14 (); /* { dg-error "'enum' underlying type may not be specified here" } */
      27  typeof (enum e15 : long) x15; /* { dg-error "'enum' underlying type may not be specified here" } */
      28  int f16 (enum e16 : char p); /* { dg-error "'enum' underlying type may not be specified here" } */
      29  /* { dg-warning "will not be visible outside of this definition or declaration" "warning" { target *-*-* } .-1 } */
      30  int f17 (enum e17 : char); /* { dg-error "'enum' underlying type may not be specified here" } */
      31  /* { dg-warning "will not be visible outside of this definition or declaration" "warning" { target *-*-* } .-1 } */
      32  struct s18 { enum e18 : int x; }; /* { dg-error "'enum' underlying type may not be specified here" } */
      33  
      34  /* But those are OK if the enum content is defined.  */
      35  enum e19 : short { E19 } x19;
      36  enum e20 : long { E20 } f20 ();
      37  typeof (enum e21 : long { E21 }) x21;
      38  int f22 (enum e22 : long long { E22 } p); /* { dg-warning "will not be visible outside of this definition or declaration" } */
      39  int f23 (enum e23 : long long { E23 } p); /* { dg-warning "will not be visible outside of this definition or declaration" } */
      40  struct s24 { enum e24 : int { E24 } x; };
      41  
      42  /* Incompatible kinds of tags in the same scope are errors.  */
      43  struct s25;
      44  enum s25 : int; /* { dg-error "wrong kind of tag" } */
      45  struct s26;
      46  enum s26 : int { E26 }; /* { dg-error "wrong kind of tag" } */
      47  struct s27 { int x; };
      48  enum s27 : int; /* { dg-error "wrong kind of tag" } */
      49  struct s28 { int x; };
      50  enum s28 : int { E28 }; /* { dg-error "wrong kind of tag" } */
      51  union u29;
      52  enum u29 : int; /* { dg-error "wrong kind of tag" } */
      53  union u30;
      54  enum u30 : int { E30 }; /* { dg-error "wrong kind of tag" } */
      55  union u31 { int x; };
      56  enum u31 : int; /* { dg-error "wrong kind of tag" } */
      57  union u32 { int x; };
      58  enum u32 : int { E32 }; /* { dg-error "wrong kind of tag" } */
      59  
      60  /* If an enum has a fixed underlying type, that must be given when defining the
      61     enum.  */
      62  enum e33 : short;
      63  enum e33 { E33 }; /* { dg-error "'enum' declared with but defined without fixed underlying type" } */
      64  
      65  /* An enum defined without a fixed underlying type cannot then be declared with
      66     one.  */
      67  enum e34 { E34A = -__INT_MAX__, E34B = __INT_MAX__ };
      68  enum e34 : int; /* { dg-error "'enum' declared both with and without fixed underlying type" } */
      69  
      70  /* An enum with a fixed underlying type cannot be declared with an incompatible
      71     fixed underlying type.  */
      72  enum e35 : int;
      73  enum e35 : unsigned int; /* { dg-error "'enum' underlying type incompatible with previous declaration" } */
      74  enum e36 : int;
      75  enum e36 : unsigned int { E36 }; /* { dg-error "'enum' underlying type incompatible with previous declaration" } */
      76  enum e37 : unsigned int { E37 };
      77  enum e37 : int; /* { dg-error "'enum' underlying type incompatible with previous declaration" } */
      78  
      79  /* Enumeration constants must fit in the fixed underlying type.  */
      80  enum e38 : unsigned char { E38 = (unsigned long long)((unsigned char) -1) + 1 }; /* { dg-error "enumerator value outside the range of underlying type" } */
      81  enum e39 : unsigned int { E39 = -1 }; /* { dg-error "enumerator value outside the range of underlying type" } */
      82  enum e40 : int { E40 = __INT_MAX__, E40A }; /* { dg-error "overflow in enumeration values" } */
      83  enum e41 : unsigned int { E41 = (unsigned int) -1, E41A }; /* { dg-error "overflow in enumeration values" } */
      84  enum e42 : bool { E42 = 2 }; /* { dg-error "enumerator value outside the range of underlying type" } */
      85  enum e43 : bool { E43 = 1, E43A }; /* { dg-error "overflow in enumeration values" } */
      86  
      87  /* The underlying type must be an integer type, not itself an enum (or
      88     bit-precise) type.  */
      89  enum e44 : double; /* { dg-error "invalid 'enum' underlying type" } */
      90  typedef int T;
      91  enum e45 : T;
      92  typedef int *TP;
      93  enum e46 : TP; /* { dg-error "invalid 'enum' underlying type" } */
      94  enum e47 : enum e45; /* { dg-error "invalid 'enum' underlying type" } */
      95  enum e48 : const; /* { dg-error "no 'enum' underlying type specified" } */
      96  /* 'restrict' is not valid on integer types.  */
      97  enum e49 : int restrict; /* { dg-error "invalid use of 'restrict'" } */