Skip to content

Commit b63c541

Browse files
authored
Merge pull request #38365 from HannaYakusevych/fix-diagnostic-implicitly-overriden-init
[SR-14761] Improve diagnostic for implicitly overridden init()
2 parents 1369382 + c0ea9b2 commit b63c541

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,9 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current) const {
626626
// Thwart attempts to override the same declaration more than once.
627627
const auto *currentOverride = current->getOverriddenDecl();
628628
const auto *otherOverride = other->getOverriddenDecl();
629-
if (currentOverride && currentOverride == otherOverride) {
629+
const auto *otherInit = dyn_cast<ConstructorDecl>(other);
630+
if (currentOverride && currentOverride == otherOverride &&
631+
!(otherInit && otherInit->isImplicit())) {
630632
current->diagnose(diag::multiple_override, current->getName());
631633
other->diagnose(diag::multiple_override_prev, other->getName());
632634
current->setInvalid();

test/decl/init/basic_init.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ class InitClass {
2222
@objc dynamic init(bar: Int) {}
2323
}
2424
class InitSubclass: InitClass {}
25-
// expected-note@-1{{'init(baz:)' previously overridden here}}
26-
// expected-note@-2{{'init(bar:)' previously overridden here}}
25+
// expected-note@-1{{implicit initializer 'init(baz:)' declared here}}
26+
// expected-note@-2{{implicit initializer 'init(bar:)' declared here}}
2727
extension InitSubclass {
2828
convenience init(arg: Bool) {} // expected-error{{non-@objc initializer 'init(arg:)' declared in 'InitClass' cannot be overridden from extension}}
2929
convenience override init(baz: Int) {}
30-
// expected-error@-1 {{'init(baz:)' has already been overridden}}
30+
// expected-error@-1 {{initializer 'init(baz:)' with Objective-C selector 'initWithBaz:' conflicts with implicit initializer 'init(baz:)' with the same Objective-C selector}}
3131
// expected-error@-2 {{cannot override a non-dynamic class declaration from an extension}}
3232
convenience override init(bar: Int) {}
33-
// expected-error@-1 {{'init(bar:)' has already been overridden}}
33+
// expected-error@-1 {{initializer 'init(bar:)' with Objective-C selector 'initWithBar:' conflicts with implicit initializer 'init(bar:)' with the same Objective-C selector}}
3434
}
3535

3636
struct InitStruct {

test/decl/objc_redeclaration.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,11 @@ extension DummyClass {
6060
@objc func nsstringProperty2() -> Int { return 0 } // expected-error{{method 'nsstringProperty2()' with Objective-C selector 'nsstringProperty2' conflicts with getter for 'nsstringProperty2' with the same Objective-C selector}}
6161
}
6262

63+
open class MyObject: NSObject {} // expected-note{{implicit initializer 'init()' declared here}}
64+
65+
extension MyObject {
66+
public override convenience init() {} // expected-error{{initializer 'init()' with Objective-C selector 'init' conflicts with implicit initializer 'init()' with the same Objective-C selector}}
67+
}
68+
6369
// FIXME: Remove -verify-ignore-unknown.
6470
// <unknown>:0: error: unexpected note produced: 'nsstringProperty2' previously declared here

0 commit comments

Comments
 (0)