Skip to content

Sema: Diagnose self.init and super.init inside closures #15028

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
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
2 changes: 1 addition & 1 deletion include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
struct {
unsigned TestingEnabled : 1;
unsigned FailedToLoad : 1;
unsigned ResilienceStrategy : 2;
unsigned ResilienceStrategy : 1;
} Flags;

ModuleDecl(Identifier name, ASTContext &ctx);
Expand Down
11 changes: 3 additions & 8 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,10 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
.highlight(IOE->getSubExpr()->getSourceRange());
}

// Diagnose 'self.init' or 'super.init' nested in another expression.
// Diagnose 'self.init' or 'super.init' nested in another expression
// or closure.
if (auto *rebindSelfExpr = dyn_cast<RebindSelfInConstructorExpr>(E)) {
bool inDefer = false;
auto *innerDecl = DC->getInnermostDeclarationDeclContext();
if (auto *FD = dyn_cast_or_null<FuncDecl>(innerDecl)) {
inDefer = FD->isDeferBody();
}

if (!Parent.isNull() || !IsExprStmt || inDefer) {
if (!Parent.isNull() || !IsExprStmt || DC->getParent()->isLocalContext()) {
bool isChainToSuper;
(void)rebindSelfExpr->getCalledConstructor(isChainToSuper);
TC.diagnose(E->getLoc(), diag::init_delegation_nested,
Expand Down
24 changes: 24 additions & 0 deletions test/expr/postfix/dot/init_ref_delegation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,30 @@ class TestNestedExpr {
convenience init(i: Int) {
_ = ((), try! self.init(error: true)) // expected-error {{initializer delegation ('self.init') cannot be nested in another expression}}
}

convenience init(j: Int) throws {
_ = {
try self.init(error: true)
// expected-error@-1 {{initializer delegation ('self.init') cannot be nested in another expression}}
}

_ = {
do {
try self.init(error: true)
// expected-error@-1 {{initializer delegation ('self.init') cannot be nested in another expression}}
}
}

defer {
try! self.init(error: true)
// expected-error@-1 {{initializer delegation ('self.init') cannot be nested in another expression}}
}

func local() throws {
try self.init(error: true)
// expected-error@-1 {{initializer delegation ('self.init') cannot be nested in another expression}}
}
}
}

class TestNestedExprSub : TestNestedExpr {
Expand Down