Skip to content

Commit f89a438

Browse files
committed
Add another test case from review feedback
1 parent b6ef700 commit f89a438

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

clang/test/AST/ByteCode/literals.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,19 @@ namespace CompoundLiterals {
886886
}
887887
constexpr int g = f2(); // Should evaluate to 1?
888888
static_assert(g == 1, "");
889+
890+
// This example should be rejected because the lifetime of the compound
891+
// literal assigned into x is that of the full expression, which is the
892+
// parenthesized assignment operator. So the return statement is using a
893+
// dangling pointer. FIXME: the note saying it's a read of a dereferenced
894+
// null pointer suggests we're doing something odd during constant expression
895+
// evaluation: I think it's still taking 'x' as being null from the call to
896+
// f3() rather than tracking the assignment happening in the VLA.
897+
constexpr int f3(int *x, int (*y)[*(x=(int[]){1,2,3})]) { // both-warning {{object backing the pointer x will be destroyed at the end of the full-expression}}
898+
return x[0]; // both-note {{read of dereferenced null pointer is not allowed in a constant expression}}
899+
}
900+
constexpr int h = f3(0,0); // both-error {{constexpr variable 'h' must be initialized by a constant expression}} \
901+
both-note {{in call to 'f3(nullptr, nullptr)'}}
889902
}
890903

891904
namespace TypeTraits {

0 commit comments

Comments
 (0)