Skip to content

[5.7][CSClosure] Mark partially inferred external declarations as invalid #58717

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 2 commits into from
May 10, 2022
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
26 changes: 25 additions & 1 deletion lib/Sema/CSClosure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,32 @@ class TypeVariableRefFinder : public ASTWalker {

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
if (auto *DRE = dyn_cast<DeclRefExpr>(expr)) {
if (auto type = CS.getTypeIfAvailable(DRE->getDecl()))
auto *decl = DRE->getDecl();

if (auto type = CS.getTypeIfAvailable(DRE->getDecl())) {
// If this is not one of the closure parameters which
// is inferrable from the body, let's replace type
// variables with errors to avoid bringing external
// information to the element component.
if (type->hasTypeVariable() && !isa<ParamDecl>(decl)) {
// If there are type variables left in the simplified version,
// it means that this is an invalid external declaration
// relative to this element's context.
if (CS.simplifyType(type)->hasTypeVariable()) {
auto transformedTy = type.transform([&](Type type) {
if (auto *typeVar = type->getAs<TypeVariableType>()) {
return ErrorType::get(CS.getASTContext());
}
return type;
});

CS.setType(decl, transformedTy);
return {true, expr};
}
}

inferVariables(type);
}
}

return {true, expr};
Expand Down
40 changes: 37 additions & 3 deletions lib/Sema/CSStep.h
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,9 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
class SolverSnapshot {
ConstraintSystem &CS;

/// The conjunction this snapshot belongs to.
Constraint *Conjunction;

Optional<llvm::SaveAndRestore<DeclContext *>> DC = None;

llvm::SetVector<TypeVariableType *> TypeVars;
Expand All @@ -794,8 +797,9 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {

public:
SolverSnapshot(ConstraintSystem &cs, Constraint *conjunction)
: CS(cs), TypeVars(std::move(cs.TypeVariables)) {
auto *locator = conjunction->getLocator();
: CS(cs), Conjunction(conjunction),
TypeVars(std::move(cs.TypeVariables)) {
auto *locator = Conjunction->getLocator();
// If this conjunction represents a closure, we need to
// switch declaration context over to it.
if (locator->directlyAt<ClosureExpr>()) {
Expand All @@ -820,7 +824,7 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
IsolationScope = std::make_unique<Scope>(CS);

// Apply solution inferred for the conjunction.
CS.applySolution(solution);
applySolution(solution);

// Add constraints to the graph after solution
// has been applied to make sure that all type
Expand Down Expand Up @@ -855,6 +859,36 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
for (auto &constraint : CS.InactiveConstraints)
CG.addConstraint(&constraint);
}

void applySolution(const Solution &solution) {
CS.applySolution(solution);

if (!CS.shouldAttemptFixes())
return;

// If inference succeeded, we are done.
auto score = solution.getFixedScore();
if (score.Data[SK_Fix] == 0)
return;

// If this conjunction represents a closure and inference
// has failed, let's bind all of unresolved type variables
// in its interface type to holes to avoid extraneous
// fixes produced by outer context.

auto locator = Conjunction->getLocator();
if (locator->directlyAt<ClosureExpr>()) {
auto closureTy =
CS.getClosureType(castToExpr<ClosureExpr>(locator->getAnchor()));

CS.simplifyType(closureTy).visit([&](Type componentTy) {
if (auto *typeVar = componentTy->getAs<TypeVariableType>()) {
CS.assignFixedType(
typeVar, PlaceholderType::get(CS.getASTContext(), typeVar));
}
});
}
}
};

/// Best solution solver reached so far.
Expand Down
21 changes: 21 additions & 0 deletions test/expr/closure/multi_statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,24 @@ func test_unknown_refs_in_tilde_operator() {
}
}
}

// rdar://92347054 - crash during conjunction processing
func test_no_crash_with_circular_ref_due_to_error() {
struct S { // expected-note {{did you mean 'S'?}}
var x: Int?
}

func test(v: Int?, arr: [S]) -> Int { // expected-note {{did you mean 'v'?}}
// There is missing `f` here which made body of the
// `if` a multiple statement closure instead that uses
// `next` inside.
i let x = v, let next = arr.first?.x { // expected-error {{cannot find 'i' in scope}}
// expected-error@-1 {{consecutive statements on a line must be separated by ';'}}
// expected-error@-2 {{'let' cannot appear nested inside another 'var' or 'let' pattern}}
// expected-error@-3 {{cannot call value of non-function type 'Int?'}}
print(next)
return x
}
return 0
}
}