Skip to content
Open
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
10 changes: 7 additions & 3 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2163,11 +2163,15 @@ static Expr* constructCallToSuperInit(ConstructorDecl *ctor,
/// \returns true if an error occurred.
static bool checkSuperInit(ConstructorDecl *fromCtor,
ApplyExpr *apply, bool implicitlyGenerated) {
auto Callee = apply->getSemanticFn();
if (auto dotCall = dyn_cast<DotSyntaxCallExpr>(Callee))
Callee = dotCall->getSemanticFn();

// Make sure we are referring to a designated initializer.
auto otherCtorRef = dyn_cast<OtherConstructorDeclRefExpr>(
apply->getSemanticFn());
if (!otherCtorRef)
auto otherCtorRef = dyn_cast<OtherConstructorDeclRefExpr>(Callee);
if (!otherCtorRef) {
return false;
}

auto ctor = otherCtorRef->getDecl();
if (!ctor->isDesignatedInit()) {
Expand Down
34 changes: 33 additions & 1 deletion test/decl/func/complete_object_init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Declaration of complete object initializers and basic semantic checking
// ---------------------------------------------------------------------------
class A {
convenience init(int i: Int) { // expected-note{{convenience initializer is declared here}}
convenience init(int i: Int) { // expected-note 5 {{convenience initializer is declared here}}
self.init(double: Double(i))
}

Expand Down Expand Up @@ -39,6 +39,38 @@ class DerivesA : A {
}
}

// Fixing https://github.com/swiftlang/swift/issues/80311
class DerivesAWithLet : A {
let str: String
init(int i: Int) {
str = "foo"
super.init(int: i) // expected-error{{must call a designated initializer of the superclass 'A'}}
}
}

class DerivesAWithVar : A {
var str: String
init(int i: Int) {
str = "foo"
super.init(int: i) // expected-error{{must call a designated initializer of the superclass 'A'}}
}
}

class DerivesAWithDefaultLet : A {
let str: String = "foo"
init(int i: Int) {
super.init(int: i) // expected-error{{must call a designated initializer of the superclass 'A'}}
}
}

class DerivesAWithDefaultVar : A {
var str: String = "foo"
init(int i: Int) {
super.init(int: i) // expected-error{{must call a designated initializer of the superclass 'A'}}
}
}


struct S {
convenience init(int i: Int) { // expected-error{{initializers in structs are not marked with 'convenience'}}
self.init(double: Double(i))
Expand Down