Skip to content
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
4 changes: 4 additions & 0 deletions include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ class BraceStmt final : public Stmt,

ASTNode findAsyncNode();

/// Whether the body contains an explicit `return` statement. This computation
/// is cached.
bool hasExplicitReturnStmt(ASTContext &ctx) const;

/// If this brace contains a single ASTNode, or a \c #if that has a single active
/// element, returns it. This will always be the last element of the brace.
/// Otherwise returns \c nullptr.
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ ASTNode BraceStmt::findAsyncNode() {
return asyncFinder.getAsyncNode();
}

bool BraceStmt::hasExplicitReturnStmt(ASTContext &ctx) const {
return evaluateOrDefault(ctx.evaluator,
BraceHasExplicitReturnStmtRequest{this}, false);
}

static bool hasSingleActiveElement(ArrayRef<ASTNode> elts) {
return elts.size() == 1;
}
Expand Down
4 changes: 1 addition & 3 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,9 +1289,7 @@ bool AnyFunctionRef::bodyHasExplicitReturnStmt() const {
return false;
}

auto &ctx = getAsDeclContext()->getASTContext();
return evaluateOrDefault(ctx.evaluator,
BraceHasExplicitReturnStmtRequest{body}, false);
return body->hasExplicitReturnStmt(getAsDeclContext()->getASTContext());
}

void AnyFunctionRef::getExplicitReturnStmts(
Expand Down
7 changes: 5 additions & 2 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,13 @@ static bool isViableElement(ASTNode element,
// Skip if we're doing completion for a SingleValueStmtExpr, and have a
// brace that doesn't involve a single expression, and doesn't have a
// code completion token, as it won't contribute to the type of the
// SingleValueStmtExpr.
// SingleValueStmtExpr. We also need to skip if the body has a ReturnStmt,
// which isn't something that's currently allowed, but is necessary to
// correctly infer the contextual type without leaving it unbound.
if (isForSingleValueStmtCompletion &&
!SingleValueStmtExpr::hasResult(braceStmt) &&
!cs.containsIDEInspectionTarget(braceStmt)) {
!cs.containsIDEInspectionTarget(braceStmt) &&
!braceStmt->hasExplicitReturnStmt(cs.getASTContext())) {
return false;
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/IDE/complete_singlevaluestmt_return.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %batch-code-completion

struct S {
var str: String
}

_ = {
let k = if .random() {
return ""
} else {
S()
}
// Make sure we can still infer 'k' here.
return k.#^COMPLETE_ON_SVE_WITH_RET^#
// COMPLETE_ON_SVE_WITH_RET: Decl[InstanceVar]/CurrNominal/TypeRelation[Convertible]: str[#String#]; name=str
}

9 changes: 9 additions & 0 deletions validation-test/IDE/crashers_fixed/7dfeb692d885c8c5.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// {"kind":"complete","original":"0bd7af1f","signature":"swift::constraints::TypeVarRefCollector::walkToStmtPre(swift::Stmt*)","signatureAssert":"Assertion failed: (result), function getClosureType"}
// RUN: %target-swift-ide-test -code-completion -batch-code-completion -skip-filecheck -code-completion-diagnostics -source-filename %s
{
let a =
if <#expression#> {
return
}
return #^^#
}