Skip to content

Commit 5c9244f

Browse files
committed
C: diagnose pointer conversions that add qualifiers below the first level [GH #514]
In C, an implicit conversion such as "int **" to "const int *const *" is valid in C++ but violates the constraints of simple assignment, because the types pointed to are not compatible (C23 6.5.17.2), so a diagnostic is required (C23 5.1.1.3). The front end accepted it as an extension without one. Issue the caller's default warning when no more specific warning applies; the conversion is still accepted.
1 parent 8fa21be commit 5c9244f

9 files changed

Lines changed: 140 additions & 1 deletion

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[GH #514]
2+
C: pointer conversions adding qualifiers below the first level
3+
4+
In C, the front end accepted an implicit conversion between pointer types
5+
that adds type qualifiers below the first level, such as "int **" to
6+
"const int *const *", without a diagnostic. Such a conversion is valid in
7+
C++, but in C the types pointed to are not compatible, so it violates a
8+
constraint and requires a diagnostic (C23 6.5.17.2). A warning is now
9+
issued; the conversion is still accepted. For example:
10+
11+
void f(int **p) {
12+
const int *const *q;
13+
q = p; // Now a warning.
14+
}

‎src/types.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11682,11 +11682,15 @@ operators), 3.3.15 (?: operator), and 3.3.16.1 (simple assignment).
1168211682
/* Allow conversion between pointers where type qualifiers are
1168311683
being added at levels other than the first, e.g.,
1168411684
"int **" -> "const int * const *". These are the const-safe
11685-
cases. This is an extension in C mode. */
11685+
cases. In C mode, this is an extension, and a diagnostic is
11686+
issued since the types pointed to are not "compatible". */
1168611687
okay = TRUE;
1168711688
std_conv->nontrivial_conversion = FALSE;
1168811689
std_conv->type_qualifiers_added = qualifiers_added;
1168911690
std_conv->warning_suggested = warning_suggested;
11691+
if (C_mode() && warning_suggested == ec_no_error) {
11692+
std_conv->warning_suggested = default_warning_code;
11693+
} /* if */
1169011694
qualifiers_checked = TRUE;
1169111695
} else if ((!suppress_extensions || any_cfront_mode()) &&
1169211696
same_type_with_added_qualifiers(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++20 --strict Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c --c23 --strict Test_name.c
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fe_only -DTEST_NUMBER=1 --c --c23 --no_strict_gnu --gnu_version=130300 Test_name.c
2+
"Test_name.c", line 16: warning: a value of type "int **" cannot be assigned to an entity of type "const int *const *"
3+
q = p;
4+
^
5+
6+
"Test_name.c", line 22: warning: a value of type "int **" cannot be used to initialize an entity of type "const int *const *"
7+
const int *const *q = p;
8+
^
9+
10+
"Test_name.c", line 29: warning: argument of type "int **" is incompatible with parameter of type "const int *const *"
11+
take(p);
12+
^
13+
14+
"Test_name.c", line 34: warning: return value type does not match the function type
15+
return p;
16+
^
17+
18+
"Test_name.c", line 39: warning: a value of type "int ***" cannot be used to initialize an entity of type "const int *const *const *"
19+
const int *const *const *q = p;
20+
^
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fe_only -DTEST_NUMBER=2 --c --c23 --strict_warnings Test_name.c
2+
"Test_name.c", line 16: warning: a value of type "int **" cannot be assigned to an entity of type "const int *const *"
3+
q = p;
4+
^
5+
6+
"Test_name.c", line 22: warning: a value of type "int **" cannot be used to initialize an entity of type "const int *const *"
7+
const int *const *q = p;
8+
^
9+
10+
"Test_name.c", line 29: warning: argument of type "int **" is incompatible with parameter of type "const int *const *"
11+
take(p);
12+
^
13+
14+
"Test_name.c", line 34: warning: return value type does not match the function type
15+
return p;
16+
^
17+
18+
"Test_name.c", line 39: warning: a value of type "int ***" cannot be used to initialize an entity of type "const int *const *const *"
19+
const int *const *const *q = p;
20+
^
21+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//type:fp
2+
//options_all:--c++20 --strict
3+
//remark:C++: impl. ptr conv. w/ qualifiers below first level
4+
5+
// The const-safe conversions that need a diagnostic in C are standard
6+
// qualification conversions in C++.
7+
8+
void take(const int *const *);
9+
void take3(const int *const *const *);
10+
11+
void add_deep_const(int **p, int ***p3)
12+
{
13+
const int *const *q = p;
14+
15+
take(q);
16+
take(p);
17+
take3(p3);
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//type:fp
2+
//options_all:--c --c23 --strict
3+
//remark:C: impl. ptr conv. adding qualifiers at the first level only
4+
5+
void add_first_level_const(int **p)
6+
{
7+
int *const* q = p;
8+
9+
(void)q;
10+
}
11+
12+
void explicit_deep_qualification(int **p)
13+
{
14+
const int *const* q = (const int *const* )p;
15+
16+
(void)q;
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//type:fp
2+
//options_all:--c --c23
3+
//options:--no_strict_gnu --gnu_version=130300:--strict_warnings
4+
//remark:C: impl. ptr conv. w/ qualifiers below first level
5+
6+
// C++ permits these conversions, but in C the types pointed to are not
7+
// compatible, so each one requires a diagnostic.
8+
9+
void take(const int *const *);
10+
void take3(const int *const *const *);
11+
12+
void assignment(int **p)
13+
{
14+
const int *const *q;
15+
16+
q = p;
17+
take(q);
18+
}
19+
20+
void initialization(int **p)
21+
{
22+
const int *const *q = p;
23+
24+
take(q);
25+
}
26+
27+
void argument(int **p)
28+
{
29+
take(p);
30+
}
31+
32+
const int *const *return_value(int **p)
33+
{
34+
return p;
35+
}
36+
37+
void three_levels(int ***p)
38+
{
39+
const int *const *const *q = p;
40+
41+
take3(q);
42+
}

0 commit comments

Comments
 (0)