This patch resolves DR 1512 (and, by turn, DR 583). This entails:
1) Relational pointer comparisons against null pointer constants have
been made ill-formed:
void f(char *p) {
if (p > 0)
// ...
}
was always invalid in C but was -- accidentally -- allowed in C++.
2) This was ill-formed:
bool foo(int** x, const int** y) {
return x < y;
}
because 'int**' couldn't be converted to 'const int**'. This was
fixed by re-defining a generic composite pointer type. The composite
type of these two pointers will be 'const int *const *', to which
both pointers can be converted.
3) The overload descriptions for built-in operators were adjusted,
because objects of type std::nullptr_t cannot be used with relational
operators any more.
I fixed 1) by adjusting cp_build_binary_op; we already had a warning
for it so made it a hard error now.
Then 2) required tweaking composite_pointer_type_r. [expr.type] defines
the composite pointer type by using the "cv-combined type." We didn't
implement the [conv.qual]/3.3 part; previously the composite type of
'int**' and 'const int**' was 'const int**', so this didn't compile:
void f(const int **p, int **q) {
true ? p : q;
}
I wrote a more extensive test for this which uses decltype and some
template magic to check the composite type, see composite-ptr-type.C.
We still don't handle everything that [expr.type] requires us to,
but it's pretty close.
And finally 3) was handled in add_builtin_candidate. Turned out we
weren't creating built-in operator candidates when the type was
std::nullptr_t at all. We should, for == and !=. Tested in builtin4.C.
In passing, I'm fixing some of the comments too.
DR 1512
PR c++/87699
* call.c (add_builtin_candidate) <case EQ_EXPR>: Create candidate
operator functions when type is std::nullptr_t for ==/!=.
* typeck.c (composite_pointer_type_r): Add bool a * parameter. Use it
to maybe add "const" to the pointer type.
(composite_pointer_type): Update the call to composite_pointer_type_r.
(cp_build_binary_op): Turn two warning_at into error_at. Print the
types.
* g++.dg/cpp0x/constexpr-array-ptr10.C: Change dg-warning to dg-error
and adjust the expected messages in dg-error.
* g++.dg/expr/composite-ptr-type.C: New test.
* g++.dg/expr/ptr-comp1.C: New test.
* g++.dg/expr/ptr-comp2.C: New test.
* g++.dg/expr/ptr-comp3.C: New test.
* g++.dg/overload/builtin4.C: New test.
* g++.dg/warn/Wextra-3.C: Change dg-warning to dg-error.
112 lines
4.3 KiB
C
112 lines
4.3 KiB
C
// PR c++/67376 - [5/6 regression] Comparison with pointer to past-the-end
|
|
// of array fails inside constant expression
|
|
// This test verifies the aspect of the bug raised in comment #10,
|
|
// specifically comparing pointers to null. The basic regression test
|
|
// is in g++.dg/cpp0x/constexpr-67376.C.
|
|
// Note also that while the description of the bug talks about pointers
|
|
// pointing past the end of arrays but the prolem is more general than
|
|
// that and involves all constexpr object pointers.
|
|
|
|
// { dg-do compile { target c++11 } }
|
|
// { dg-additional-options "-Wall -Wextra -fdelete-null-pointer-checks" }
|
|
|
|
namespace A {
|
|
|
|
extern int i;
|
|
|
|
constexpr int *p0 = &i;
|
|
|
|
constexpr bool b0 = p0; // { dg-warning "address of .A::i." }
|
|
constexpr bool b1 = p0 == 0; // { dg-warning "address of .A::i." }
|
|
constexpr bool b2 = p0 != 0; // { dg-warning "address of .A::i." }
|
|
constexpr bool b3 = p0 < 0; // { dg-error "25:ordered comparison" }
|
|
constexpr bool b4 = p0 <= 0; // { dg-error "25:ordered comparison" }
|
|
constexpr bool b5 = p0 > 0; // { dg-error "25:ordered comparison" }
|
|
constexpr bool b6 = p0 >= 0; // { dg-error "25:ordered comparison" }
|
|
|
|
constexpr bool b7 = !p0; // { dg-warning "address of .A::i." }
|
|
constexpr bool b8 = 0 == p0; // { dg-warning "address of .A::i." }
|
|
constexpr bool b9 = 0 != p0; // { dg-warning "address of .A::i." }
|
|
constexpr bool b10 = 0 < p0; // { dg-error "24:ordered comparison" }
|
|
constexpr bool b11 = 0 <= p0; // { dg-error "24:ordered comparison" }
|
|
constexpr bool b12 = 0 > p0; // { dg-error "24:ordered comparison" }
|
|
constexpr bool b13 = 0 >= p0; // { dg-error "24:ordered comparison" }
|
|
|
|
}
|
|
|
|
namespace B {
|
|
|
|
// PR c++/70172 - incorrect reinterpret_cast from integer to pointer
|
|
// error on invalid constexpr initialization
|
|
|
|
struct S { int a, b[1]; } s;
|
|
|
|
constexpr S *p0 = &s;
|
|
|
|
constexpr int *q0 = p0->b; // { dg-bogus "reinterpret_cast from integer to pointer" }
|
|
|
|
}
|
|
|
|
namespace WeakRefTest1 {
|
|
|
|
extern __attribute__ ((weak)) int i;
|
|
|
|
constexpr int *p0 = &i;
|
|
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wextra"
|
|
// Suppress warning: ordered comparison of pointer with integer zero
|
|
|
|
constexpr bool b0 = p0; // { dg-error "not a constant expression" }
|
|
constexpr bool b1 = p0 == 0; // { dg-error "not a constant expression" }
|
|
constexpr bool b2 = p0 != 0; // { dg-error "not a constant expression" }
|
|
constexpr bool b4 = p0 <= 0; // { dg-error "ordered comparison" }
|
|
constexpr bool b5 = p0 > 0; // { dg-error "ordered comparison" }
|
|
|
|
constexpr bool b7 = !p0; // { dg-error "not a constant expression" }
|
|
constexpr bool b8 = 0 == p0; // { dg-error "not a constant expression" }
|
|
constexpr bool b9 = 0 != p0; // { dg-error "not a constant expression" }
|
|
constexpr bool b10 = 0 < p0; // { dg-error "ordered comparison" }
|
|
constexpr bool b13 = 0 >= p0; // { dg-error "ordered comparison" }
|
|
|
|
constexpr bool b3 = p0 < 0; // { dg-error "ordered comparison" }
|
|
constexpr bool b6 = p0 >= 0; // { dg-error "ordered comparison" }
|
|
constexpr bool b11 = 0 <= p0; // { dg-error "ordered comparison" }
|
|
constexpr bool b12 = 0 > p0; // { dg-error "ordered comparison" }
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
}
|
|
|
|
namespace WeakRefTest2 {
|
|
|
|
extern __attribute__ ((weak)) int i;
|
|
|
|
constexpr int *p1 = &i + 1;
|
|
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wextra"
|
|
// Suppress warning: ordered comparison of pointer with integer zero
|
|
|
|
constexpr bool b0 = p1; // { dg-error "not a constant expression" }
|
|
constexpr bool b1 = p1 == 0; // { dg-error "not a constant expression" }
|
|
constexpr bool b2 = p1 != 0; // { dg-error "not a constant expression" }
|
|
constexpr bool b4 = p1 <= 0; // { dg-error "ordered comparison" }
|
|
constexpr bool b5 = p1 > 0; // { dg-error "ordered comparison" }
|
|
|
|
constexpr bool b7 = !p1; // { dg-error "not a constant expression" }
|
|
constexpr bool b8 = 0 == p1; // { dg-error "not a constant expression" }
|
|
constexpr bool b9 = 0 != p1; // { dg-error "not a constant expression" }
|
|
constexpr bool b10 = 0 < p1; // { dg-error "ordered comparison" }
|
|
constexpr bool b13 = 0 >= p1; // { dg-error "ordered comparison" }
|
|
|
|
// The following are accepted as constant expressions due to bug c++/70196.
|
|
// constexpr bool b3 = p1 < 0;
|
|
// constexpr bool b6 = p1 >= 0;
|
|
// constexpr bool b11 = 0 <= p1;
|
|
// constexpr bool b12 = 0 > p1;
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
}
|