(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
c2x-qual-5.c
       1  /* { dg-do compile } */
       2  /* { dg-options "-std=c2x -pedantic-errors" } */
       3  void tvoid(void* x);
       4  void transpose0(double* out, const double* in) { }
       5  void transpose1(double out[2][2], const double in[2][2]) { }
       6  void transpose2(double out[2][2][2], const double in[2][2][2]) { }
       7  // return
       8  int (*x2(const int x[3][3]))[3] { return x; } /* { dg-error "return discards" } */
       9  const int (*x3(int x[3][3]))[3] { return x; }
      10  void test(void)
      11  {
      12  	double x0[2];
      13  	double y0[2];
      14  	const double z0[4];
      15  	double x1[2][2];
      16  	double y1[2][2];
      17  	double o1[2][3];
      18  	const double z1[2][2];
      19  	double x2[2][2][2];
      20  	double y2[2][2][2];
      21  	double o2[2][2][3];
      22  	const double z2[2][2][2];
      23  	// void pointers
      24  	tvoid(z0); /* { dg-error "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
      25  	tvoid(z1); /* { dg-error "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
      26  	tvoid(z2); /* { dg-error "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
      27  	void* p;
      28  	const void* pc;
      29  	p = x0;
      30  	p = x1;
      31  	p = x2;
      32  	p = z0; /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      33  	p = z1; /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      34  	p = z2; /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      35  	pc = x0;
      36  	pc = x1;
      37  	pc = x2;
      38  	pc = z0;
      39  	pc = z1;
      40  	pc = z2;
      41  	transpose0(pc, p); /* { dg-error "passing argument 1 of 'transpose0' discards 'const' qualifier from pointer target type" } */
      42  	transpose1(pc, p); /* { dg-error "passing argument 1 of 'transpose1' discards 'const' qualifier from pointer target type" } */
      43  	transpose2(pc, p); /* { dg-error "passing argument 1 of 'transpose2' discards 'const' qualifier from pointer target type" } */
      44  	transpose0(p, pc);
      45  	transpose1(p, pc);
      46  	transpose2(p, pc);
      47  	// passing as arguments
      48  	transpose0(y0, x0);
      49  	transpose1(y1, o1); /* { dg-error "passing argument 2 of 'transpose1' from incompatible pointer type" } */
      50  	transpose1(y1, x1);
      51  	transpose2(y2, o2); /* { dg-error "passing argument 2 of 'transpose2' from incompatible pointer type" } */
      52  	transpose2(y2, x2);
      53  	// initialization
      54  	const double (*x0p) = x0;
      55  	const double (*x1p)[2] = x1;
      56  	const double (*x2p)[2][2] = x2;
      57  	double (*v0p) = z0; /* { dg-error "initialization discards 'const' qualifier from pointer target type" } */
      58  	double (*v1p)[2] = z1; /* { dg-error "initialization discards" } */
      59  	double (*v2p)[2][2] = z2; /* { dg-error "initialization discards" } */
      60  	// assignment
      61  	x0p = x0;
      62  	x1p = x1;
      63  	x2p = x2;
      64  	// subtraction
      65  	&(x0[1]) - &(z0[0]);
      66  	&(x1[1]) - &(z1[0]);
      67  	&(x2[1]) - &(z2[0]);
      68  	// comparison
      69  	x0 == z0;
      70  	x1 == z1;
      71  	x2 == z2;
      72  	x0 < z0;
      73  	x1 < z1;
      74  	x2 < z2;
      75  	x0 > z0;
      76  	x1 > z1;
      77  	x2 > z2;
      78  	// conditional expressions
      79  	(void)(1 ? x0 : z0);
      80  	(void)(1 ? x1 : z1);
      81  	(void)(1 ? x2 : z2);
      82  	(void)(1 ? x0 : x1); /* { dg-error "pointer type mismatch in conditional expression" } */
      83  	(void)(1 ? x1 : x2); /* { dg-error "pointer type mismatch in conditional expression" } */
      84  	(void)(1 ? x2 : x0); /* { dg-error "pointer type mismatch in conditional expression" } */
      85  	v0p = (1 ? z0 : v0p); /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      86  	v1p = (1 ? z1 : v1p); /* { dg-error "assignment discards" } */
      87  	v2p = (1 ? z2 : v2p); /* { dg-error "assignment discards" } */
      88  	v0p = (1 ? x0 : x0p); /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      89  	v1p = (1 ? x1 : x1p); /* { dg-error "assignment discards" } */
      90  	v2p = (1 ? x2 : x2p); /* { dg-error "assignment discards" } */
      91  	(1 ? x0 : z0)[0] = 1; /* { dg-error "assignment of read-only location" } */
      92  	(1 ? x1 : z1)[0][0] = 1; /* { dg-error "assignment of read-only location" } */
      93  	(1 ? x2 : z2)[0][0][0] = 1; /* { dg-error "assignment of read-only location" } */
      94  	v0p = (1 ? p : z0); /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      95  	v1p = (1 ? p : z1); /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      96  	v2p = (1 ? p : z2); /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      97  	v0p = (1 ? pc : x0); /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      98  	v1p = (1 ? pc : x1); /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
      99  	v2p = (1 ? pc : x2); /* { dg-error "assignment discards 'const' qualifier from pointer target type" } */
     100  }
     101