Skip to content

Commit 3deca25

Browse files
committed
C: diagnose pointer conversions that add qualifiers below the first level []
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 ad4cfc4 commit 3deca25

11 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[GH #472]
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11687,6 +11687,11 @@ operators), 3.3.15 (?: operator), and 3.3.16.1 (simple assignment).
1168711687
std_conv->nontrivial_conversion = FALSE;
1168811688
std_conv->type_qualifiers_added = qualifiers_added;
1168911689
std_conv->warning_suggested = warning_suggested;
11690+
if (C_mode() && warning_suggested == ec_no_error) {
11691+
/* Even const-safe deep qualification conversions require a
11692+
diagnostic in C: the pointed-to types are incompatible. */
11693+
std_conv->warning_suggested = default_warning_code;
11694+
} /* if */
1169011695
qualifiers_checked = TRUE;
1169111696
} else if ((!suppress_extensions || any_cfront_mode()) &&
1169211697
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 --c23 --strict Test_name.c
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fe_only -DTEST_NUMBER=1 --c --c23 --no_strict_gnu --gnu_version=130300 Test_name.c
2+
"Test_name.c", line 11: warning: a value of type "int **" cannot be assigned to an entity of type "const int *const *"
3+
q = p;
4+
^
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Matches for "(?is)warning.*cannot be assigned to an entity of type":
2+
<-----------------------------------------------------------------------------
3+
warning: a value of type "int **" cannot be assigned to an entity of type
4+
----------------------------------------------------------------------------->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fe_only -DTEST_NUMBER=2 --c --c23 --strict_warnings Test_name.c
2+
"Test_name.c", line 11: warning: a value of type "int **" cannot be assigned to an entity of type "const int *const *"
3+
q = p;
4+
^
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Matches for "(?is)warning.*cannot be assigned to an entity of type":
2+
<-----------------------------------------------------------------------------
3+
warning: a value of type "int **" cannot be assigned to an entity of type
4+
----------------------------------------------------------------------------->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fe_only -DTEST_NUMBER=1 --c++20 --strict Test_name.c
2+
"Test_name.c", line 6: warning: variable "q" was set but never used
3+
const int *const* q = p;
4+
^
5+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//type:fp
2+
//options_all:--c --c23 --strict
3+
4+
void add_first_level_const(int **p)
5+
{
6+
int *const* q = p;
7+
8+
(void)q;
9+
}
10+
11+
void explicit_deep_qualification(int **p)
12+
{
13+
const int *const* q = (const int *const* )p;
14+
15+
(void)q;
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//type:fp
2+
//options_all:--c --c23
3+
//options:--no_strict_gnu --gnu_version=130300:--strict_warnings
4+
//match_regex:(?is)warning.*cannot be assigned to an entity of type
5+
6+
void deep_pointer_qualification(int **p)
7+
{
8+
const int *const* q;
9+
10+
/* C++ permits this conversion, but C requires a diagnostic. */
11+
q = p;
12+
(void)q;
13+
}

0 commit comments

Comments
 (0)