xref: /netbsd-src/tests/usr.bin/xlint/lint1/msg_329.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: msg_329.c,v 1.4 2022/06/17 06:59:16 rillig Exp $	*/
2 # 3 "msg_329.c"
3 
4 // Test for message: type '%s' is not a member of '%s' [329]
5 
6 union u {
7 	int i1;
8 	int i2;
9 	void *vp;
10 };
11 
12 void
13 example(void)
14 {
15 	/*
16 	 * A type cast to a union type is valid if the source type is any
17 	 * member type of the union.  Since all union members with the same
18 	 * type have the same representation, the name of the union member
19 	 * doesn't matter.
20 	 *
21 	 * XXX: could there be padding bits or other tricky details that are
22 	 * settable per-member?  These could make the type alone insufficient
23 	 * for determining the exact representation.
24 	 *
25 	 * C99 6.5.4 "Cast operators" does not mention a union cast.  On the
26 	 * contrary, it says that the type name shall specify a scalar type.
27 	 *
28 	 * C11 6.5.4 "Cast operators" differs from C99 but still requires
29 	 * scalar types for both the target type and the source value.
30 	 *
31 	 * This is a GCC extension.
32 	 * See https://gcc.gnu.org/onlinedocs/gcc/Cast-to-Union.html.
33 	 */
34 	union u u_i1 = (union u)3;
35 	union u u_vp = (union u)(void *)0;
36 	/* expect+1: error: type 'pointer to char' is not a member of 'union u' [329] */
37 	union u u_cp = (union u)(char *)0;
38 }
39