Skip to content

Commit 6cfcd4c

Browse files
committed
[AST] Don’t allow application of most bare exprs
Most ApplyExpr subclasses will now require a ParenExpr, TupleExpr, or TupleShuffleExpr around their argument lists. The exceptions are BinaryExpr, which requires a TupleExpr, and SelfApplyExpr and its subclasses, which allow anything (and only ever have one argument). This change doesn’t fix any of the places where we actually generate these.
1 parent 183a1ae commit 6cfcd4c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

include/swift/AST/Expr.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,11 +3825,17 @@ class ApplyExpr : public Expr {
38253825

38263826
/// The argument being passed to it, and whether it's a 'super' argument.
38273827
llvm::PointerIntPair<Expr *, 1, bool> ArgAndIsSuper;
3828+
3829+
/// Returns true if \c e could be used as the call's argument. For most \c ApplyExpr
3830+
/// subclasses, this means it is a \c ParenExpr, \c TupleExpr, or
3831+
/// \c TupleShuffleExpr.
3832+
bool validateArg(Expr *e) const;
38283833

38293834
protected:
38303835
ApplyExpr(ExprKind Kind, Expr *Fn, Expr *Arg, bool Implicit, Type Ty = Type())
38313836
: Expr(Kind, Implicit, Ty), Fn(Fn), ArgAndIsSuper(Arg, false) {
38323837
assert(classof((Expr*)this) && "ApplyExpr::classof out of date");
3838+
assert(validateArg(Arg) && "Arg is not a permitted expr kind");
38333839
Bits.ApplyExpr.ThrowsIsSet = false;
38343840
}
38353841

@@ -3840,8 +3846,7 @@ class ApplyExpr : public Expr {
38403846

38413847
Expr *getArg() const { return ArgAndIsSuper.getPointer(); }
38423848
void setArg(Expr *e) {
3843-
assert((getKind() != ExprKind::Binary || isa<TupleExpr>(e)) &&
3844-
"BinaryExprs must have a TupleExpr as the argument");
3849+
assert(validateArg(e) && "Arg is not a permitted expr kind");
38453850
ArgAndIsSuper = {e, ArgAndIsSuper.getInt()};
38463851
}
38473852

@@ -5128,6 +5133,15 @@ inline bool Expr::isInfixOperator() const {
51285133
return isa<BinaryExpr>(this) || isa<IfExpr>(this) ||
51295134
isa<AssignExpr>(this) || isa<ExplicitCastExpr>(this);
51305135
}
5136+
5137+
inline bool ApplyExpr::validateArg(Expr *e) const {
5138+
if (isa<SelfApplyExpr>(this))
5139+
return true;
5140+
else if (isa<BinaryExpr>(this))
5141+
return isa<TupleExpr>(e);
5142+
else
5143+
return isa<ParenExpr>(e) || isa<TupleExpr>(e) || isa<TupleShuffleExpr>(e);
5144+
}
51315145

51325146
inline Expr *const *CollectionExpr::getTrailingObjectsPointer() const {
51335147
if (auto ty = dyn_cast<ArrayExpr>(this))

0 commit comments

Comments
 (0)