Skip to content

Commit 0171e56

Browse files
authored
[clang][bytecode] Fix rejecting invalid sizeof expressions (#123332)
Emit the invalid note instead of nothing.
1 parent eb7dea8 commit 0171e56

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2066,7 +2066,7 @@ bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
20662066
Size = CharUnits::One();
20672067
else {
20682068
if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
2069-
return false;
2069+
return this->emitInvalid(E);
20702070

20712071
if (Kind == UETT_SizeOf)
20722072
Size = ASTCtx.getTypeSizeInChars(ArgType);

clang/test/AST/ByteCode/literals.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,31 +261,26 @@ namespace SizeOf {
261261
}
262262

263263
#if __cplusplus >= 201402L
264-
constexpr int IgnoredRejected() { // ref-error {{never produces a constant expression}}
264+
constexpr int IgnoredRejected() { // both-error {{never produces a constant expression}}
265265
int n = 0;
266266
sizeof(int[n++]); // both-warning {{expression result unused}} \
267-
// ref-note 2{{subexpression not valid in a constant expression}}
267+
// both-note 2{{subexpression not valid in a constant expression}}
268268
return n;
269269
}
270-
/// FIXME: This is rejected because the parameter so sizeof() is not constant.
271-
/// produce a proper diagnostic.
272270
static_assert(IgnoredRejected() == 0, ""); // both-error {{not an integral constant expression}} \
273-
// ref-note {{in call to 'IgnoredRejected()'}}
271+
// both-note {{in call to 'IgnoredRejected()'}}
274272
#endif
275273

276274

277275
#if __cplusplus >= 202002L
278276
/// FIXME: The following code should be accepted.
279-
consteval int foo(int n) { // ref-error {{consteval function never produces a constant expression}}
280-
return sizeof(int[n]); // ref-note 3{{not valid in a constant expression}}
281-
}
282-
constinit int var = foo(5); // ref-error {{not a constant expression}} \
283-
// ref-note 2{{in call to}} \
284-
// ref-error {{does not have a constant initializer}} \
285-
// ref-note {{required by 'constinit' specifier}} \
286-
// expected-error {{is not a constant expression}} \
287-
// expected-error {{does not have a constant initializer}} \
288-
// expected-note {{required by 'constinit' specifier}} \
277+
consteval int foo(int n) { // both-error {{consteval function never produces a constant expression}}
278+
return sizeof(int[n]); // both-note 3{{not valid in a constant expression}}
279+
}
280+
constinit int var = foo(5); // both-error {{not a constant expression}} \
281+
// both-note 2{{in call to}} \
282+
// both-error {{does not have a constant initializer}} \
283+
// both-note {{required by 'constinit' specifier}}
289284

290285
#endif
291286
};

0 commit comments

Comments
 (0)