Skip to content

Commit a8690f0

Browse files
committed
[Sema] Treat literal minus as a unary operator for apex precedence group purposes
1 parent 35d4a9d commit a8690f0

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3386,7 +3386,7 @@ ERROR(construct_protocol_by_name,none,
33863386
ERROR(unknown_binop,none,
33873387
"operator is not a known binary operator", ())
33883388
ERROR(unordered_adjacent_unary_operator,none,
3389-
"operator in precedence group %0 and adjacent unary operator have"
3389+
"operator in precedence group %0 and adjacent unary operator have "
33903390
"undefined relative precedence; use grouping parentheses",
33913391
(Identifier))
33923392
ERROR(non_associative_adjacent_operators,none,

lib/Sema/TypeCheckExpr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ static Expr *makeBinOp(ASTContext &Ctx, Expr *Op, Expr *LHS, Expr *RHS,
291291
opPrecedence->getName())
292292
.highlight(prefixUnary->getFn()->getSourceRange());
293293
}
294+
// Prefix '-' (but not '+') is lexed as part of a number literal rather than
295+
// as a standalone operator.
296+
else if (auto *numberLiteral = dyn_cast<NumberLiteralExpr>(LHS)) {
297+
if (numberLiteral->isNegative()) {
298+
Ctx.Diags.diagnose(Op->getLoc(),
299+
diag::unordered_adjacent_unary_operator,
300+
opPrecedence->getName())
301+
.highlight(numberLiteral->getMinusLoc());
302+
}
303+
}
294304
if (auto *postfixUnary = dyn_cast<PostfixUnaryExpr>(RHS)) {
295305
Ctx.Diags.diagnose(Op->getLoc(),
296306
diag::unordered_adjacent_unary_operator,

0 commit comments

Comments
 (0)