-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[Clang][Sema] Reject array prvalue operands #140702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11333,6 +11333,13 @@ QualType Sema::CheckAdditionOperands(ExprResult &LHS, ExprResult &RHS, | |
if (!IExp->getType()->isIntegerType()) | ||
return InvalidOperands(Loc, LHS, RHS); | ||
|
||
if (OriginalOperand Orig(PExp); | ||
Orig.getType()->isArrayType() && Orig.Orig->isPRValue()) { | ||
Diag(Loc, diag::err_typecheck_array_prvalue_operand) | ||
<< PExp->getSourceRange(); | ||
return QualType(); | ||
} | ||
|
||
// Adding to a null pointer results in undefined behavior. | ||
if (PExp->IgnoreParenCasts()->isNullPointerConstant( | ||
Context, Expr::NPC_ValueDependentIsNotNull)) { | ||
|
@@ -11429,6 +11436,18 @@ QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, | |
return compType; | ||
} | ||
|
||
OriginalOperand OrigLHS(LHS.get()), OrigRHS(RHS.get()); | ||
bool LHSArrP = OrigLHS.getType()->isArrayType() && OrigLHS.Orig->isPRValue(); | ||
bool RHSArrP = OrigRHS.getType()->isArrayType() && OrigRHS.Orig->isPRValue(); | ||
if (LHSArrP || RHSArrP) { | ||
auto &&diag = Diag(Loc, diag::err_typecheck_array_prvalue_operand); | ||
if (LHSArrP) | ||
diag << LHS.get()->getSourceRange(); | ||
if (RHSArrP) | ||
diag << RHS.get()->getSourceRange(); | ||
return QualType(); | ||
} | ||
|
||
// Either ptr - int or ptr - ptr. | ||
if (LHS.get()->getType()->isAnyPointerType()) { | ||
QualType lpointee = LHS.get()->getType()->getPointeeType(); | ||
|
@@ -15840,6 +15859,11 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, | |
InputExpr->getType()->isSpecificBuiltinType(BuiltinType::Dependent)) { | ||
resultType = Context.DependentTy; | ||
} else { | ||
if (Opc == UO_Deref || Opc == UO_Plus) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels a bit weird to use an |
||
if (InputExpr->getType()->isArrayType() && InputExpr->isPRValue()) | ||
return ExprError(Diag(OpLoc, diag::err_typecheck_array_prvalue_operand) | ||
<< InputExpr->getSourceRange()); | ||
} | ||
switch (Opc) { | ||
case UO_PreInc: | ||
case UO_PreDec: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify %s | ||
// expected-no-diagnostics | ||
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 | ||
|
||
int a0; | ||
const volatile int a1 = 2; | ||
|
@@ -16,4 +15,13 @@ int main() | |
f0(a1); | ||
f1(a2); | ||
f2(a3); | ||
|
||
using IA = int[]; | ||
void(+IA{ 1, 2, 3 }); // expected-error {{array prvalue}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some tests here to make sure that we don’t complain about array glvalues? |
||
void(*IA{ 1, 2, 3 }); // expected-error {{array prvalue}} | ||
void(IA{ 1, 2, 3 } + 0); // expected-error {{array prvalue}} | ||
void(IA{ 1, 2, 3 } - 0); // expected-error {{array prvalue}} | ||
void(0 + IA{ 1, 2, 3 }); // expected-error {{array prvalue}} | ||
void(0 - IA{ 1, 2, 3 }); // expected-error {{array prvalue}} | ||
void(IA{ 1, 2, 3 } - IA{ 1, 2, 3 }); // expected-error {{array prvalue}} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it’s a bit clearer if we phrase it like this and also print out the operator rather than just saying ‘X is not permitted’.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the operator is pointed at by
^
, likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, in that case, maybe just ‘operand cannot be an array prvalue’. I would prefer at least including the word ‘operand’ so it’s clear that the problem is that you’re passing an array prvalue to this operator, because at the moment the diagnostic makes it sound like array prvalues aren’t permitted at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part can be removed by using
-fno-caret-diagnostics
command line option. In this case, the diagnostic is as followed:Users may get confused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zwuis is
"operand cannot be an array prvalue"
ok? Comparing to an existing error:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're not particularly relevant because you don't directly mix Java and C++ code in the same way you do with C and C++ as happens in header files.
I was incorrectly remembering the behavior from returning a
char
and not having it promote to anint
. I can confirm we decay the array: https://godbolt.org/z/zhW1fnsej but you can hit the same concern I had via atypedef
orusing
, where the type information is actually slightly helpful in understanding the issue:using foo = int[10]; foo{} + 0;
(keeping in mind thatfoo{}
could also be behind a macro where it's not easy for the user to spot the{}
and realize there's a temporary involved). So I still find a formulation that includes the type information a bit more user-friendly, but that could be included in a new diagnostic that talks about temporary arrays.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @AaronBallman and @Sirraide that using
err_typecheck_invalid_operands
is the better approach here.The "prvalue" bit is only relevant as far as array decay is concerned (eg : array-to-pointer does not apply).
But ultimately,
int[]
+int
is what we should diagnose, not how we got here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt that presenting exact array type vs. just saying "array" would help much in this case. There is
note: expanded from macro
to point that something comes from macro expansion. (Not shown forerr_typecheck_invalid_operands
, BTW!)The types are not the issue, why give misleading diagnostics?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, I think it is either "in binary expression" or "to binary operator"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
‘an operand to sth.’ is a common turn of phrase that’s been around for a long time (not just in Clang).