Skip to content

[AST] [Sema] [IDE] Fix places where we set bare arguments #17987

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

Merged
merged 4 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3825,11 +3825,17 @@ class ApplyExpr : public Expr {

/// The argument being passed to it, and whether it's a 'super' argument.
llvm::PointerIntPair<Expr *, 1, bool> ArgAndIsSuper;

/// Returns true if \c e could be used as the call's argument. For most \c ApplyExpr
/// subclasses, this means it is a \c ParenExpr, \c TupleExpr, or
/// \c TupleShuffleExpr.
bool validateArg(Expr *e) const;

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

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

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

Expand Down Expand Up @@ -5128,6 +5133,15 @@ inline bool Expr::isInfixOperator() const {
return isa<BinaryExpr>(this) || isa<IfExpr>(this) ||
isa<AssignExpr>(this) || isa<ExplicitCastExpr>(this);
}

inline bool ApplyExpr::validateArg(Expr *e) const {
if (isa<SelfApplyExpr>(this))
return true;
else if (isa<BinaryExpr>(this))
return isa<TupleExpr>(e);
else
return isa<ParenExpr>(e) || isa<TupleExpr>(e) || isa<TupleShuffleExpr>(e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth to add PrefixUnaryExpr/PostfixUnaryExpr -> ParenExpr check here? cc: @xedin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since arguments of such expressions are now implicitly wrapped into parens it shouldn't be required.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I thought we can add the check for it.

Copy link
Contributor Author

@beccadax beccadax Jul 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think we should. To be honest, I’d like to drop the BinaryExpr -> TupleExpr check too, because I want to support operator functions with extra defaulted parameters (for #line and friends), which will require TupleShuffleExpr args on BinaryExpr. But that will require other changes to the compiler, and I’d rather do one thing at a time.

}

inline Expr *const *CollectionExpr::getTrailingObjectsPointer() const {
if (auto ty = dyn_cast<ArrayExpr>(this))
Expand Down
5 changes: 4 additions & 1 deletion lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3283,7 +3283,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
// escape and there isn't a better way to allocate scratch Expr nodes.
UnresolvedDeclRefExpr UDRE(op->getName(), DeclRefKind::PostfixOperator,
DeclNameLoc(expr->getSourceRange().End));
PostfixUnaryExpr opExpr(&UDRE, expr);
ParenExpr parenExpr(expr->getSourceRange().Start, expr,
expr->getSourceRange().End,
/*hasTrailingClosure=*/false);
PostfixUnaryExpr opExpr(&UDRE, &parenExpr);
Expr *tempExpr = &opExpr;
ConcreteDeclRef referencedDecl;
if (auto T = getTypeOfCompletionContextExpr(
Expand Down
11 changes: 4 additions & 7 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7360,9 +7360,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
return cs.getType(E);
};

auto callSubExpr = CallExpr::create(tc.Context, body, escapable, {}, {},
/*trailing closure*/ false,
/*implicit*/ true, Type(), getType);
auto callSubExpr = CallExpr::createImplicit(tc.Context, body, {escapable}, {}, getType);
cs.cacheSubExprTypes(callSubExpr);
cs.setType(callSubExpr, resultType);

auto replacement = new (tc.Context)
Expand Down Expand Up @@ -7414,10 +7413,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
return cs.getType(E);
};

auto callSubExpr = CallExpr::create(tc.Context, body, opaqueValue, {},
{}, /*trailing closure*/ false,
/*implicit*/ true,
Type(), getType);
auto callSubExpr = CallExpr::createImplicit(tc.Context, body, {opaqueValue}, {}, getType);
cs.cacheSubExprTypes(callSubExpr);
cs.setType(callSubExpr, resultTy);

auto replacement = new (tc.Context)
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,10 @@ namespace {

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
// If this is a call, record the argument expression.
if (auto call = dyn_cast<CallExpr>(expr)) {
CallArgs.insert(call->getArg());
if (auto call = dyn_cast<ApplyExpr>(expr)) {
if (!isa<SelfApplyExpr>(expr)) {
CallArgs.insert(call->getArg());
}
}

// If this is an unresolved member with a call argument (e.g.,
Expand Down