Skip to content

[SR-14761] Improve diagnostic for implicitly overridden init() #38365

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
4 changes: 3 additions & 1 deletion lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,9 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current) const {
// Thwart attempts to override the same declaration more than once.
const auto *currentOverride = current->getOverriddenDecl();
const auto *otherOverride = other->getOverriddenDecl();
if (currentOverride && currentOverride == otherOverride) {
const auto *otherInit = dyn_cast<ConstructorDecl>(other);
if (currentOverride && currentOverride == otherOverride &&
!(otherInit && otherInit->isImplicit())) {
current->diagnose(diag::multiple_override, current->getName());
other->diagnose(diag::multiple_override_prev, other->getName());
current->setInvalid();
Expand Down
8 changes: 4 additions & 4 deletions test/decl/init/basic_init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class InitClass {
@objc dynamic init(bar: Int) {}
}
class InitSubclass: InitClass {}
// expected-note@-1{{'init(baz:)' previously overridden here}}
// expected-note@-2{{'init(bar:)' previously overridden here}}
// expected-note@-1{{implicit initializer 'init(baz:)' declared here}}
// expected-note@-2{{implicit initializer 'init(bar:)' declared here}}
extension InitSubclass {
convenience init(arg: Bool) {} // expected-error{{non-@objc initializer 'init(arg:)' declared in 'InitClass' cannot be overridden from extension}}
convenience override init(baz: Int) {}
// expected-error@-1 {{'init(baz:)' has already been overridden}}
// expected-error@-1 {{initializer 'init(baz:)' with Objective-C selector 'initWithBaz:' conflicts with implicit initializer 'init(baz:)' with the same Objective-C selector}}
// expected-error@-2 {{cannot override a non-dynamic class declaration from an extension}}
convenience override init(bar: Int) {}
// expected-error@-1 {{'init(bar:)' has already been overridden}}
// expected-error@-1 {{initializer 'init(bar:)' with Objective-C selector 'initWithBar:' conflicts with implicit initializer 'init(bar:)' with the same Objective-C selector}}
}

struct InitStruct {
Expand Down
6 changes: 6 additions & 0 deletions test/decl/objc_redeclaration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,11 @@ extension DummyClass {
@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}}
}

open class MyObject: NSObject {} // expected-note{{implicit initializer 'init()' declared here}}

extension MyObject {
public override convenience init() {} // expected-error{{initializer 'init()' with Objective-C selector 'init' conflicts with implicit initializer 'init()' with the same Objective-C selector}}
}

// FIXME: Remove -verify-ignore-unknown.
// <unknown>:0: error: unexpected note produced: 'nsstringProperty2' previously declared here