Skip to content

[Sema] Dig out other constructor if call is wrapped in a Dot-Call #80334

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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