Skip to content

Sema: Improve redeclaration error for synthesized inits #24090

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 3 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,9 @@ ERROR(reserved_member_name,none,
" 'foo.%1' expression", (DeclName, StringRef))

ERROR(invalid_redecl,none,"invalid redeclaration of %0", (DeclName))
ERROR(invalid_redecl_init,none,
"invalid redeclaration of synthesized %select{|memberwise }1%0",
(DeclName, bool))
WARNING(invalid_redecl_swift5_warning,none,
"redeclaration of %0 is deprecated and will be an error in Swift 5",
(DeclName))
Expand Down
17 changes: 15 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,21 @@ static void checkRedeclaration(TypeChecker &tc, ValueDecl *current) {
current->getFullName());
tc.diagnose(other, diag::invalid_redecl_prev, other->getFullName());
} else {
tc.diagnose(current, diag::invalid_redecl, current->getFullName());
tc.diagnose(other, diag::invalid_redecl_prev, other->getFullName());
const auto *otherInit = dyn_cast<ConstructorDecl>(other);
// Provide a better description for implicit initializers.
if (otherInit && otherInit->isImplicit()) {
// Skip conflicts with inherited initializers, which only happen
// when the current declaration is within an extension. The override
// checker should have already taken care of emitting a more
// productive diagnostic.
if (!other->getOverriddenDecl())
tc.diagnose(current, diag::invalid_redecl_init,
current->getFullName(),
otherInit->isMemberwiseInitializer());
} else {
tc.diagnose(current, diag::invalid_redecl, current->getFullName());
tc.diagnose(other, diag::invalid_redecl_prev, other->getFullName());
}
markInvalid();
}

Expand Down
24 changes: 24 additions & 0 deletions test/decl/init/basic_init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,32 @@ class C {
typealias t = t // expected-error {{type alias 't' references itself}}
// expected-note@-1{{type declared here}}

extension Foo {
convenience init() {} // expected-error{{invalid redeclaration of synthesized 'init()'}}
}

class InitClass {
init(arg: Bool) {} // expected-note{{add '@objc' to make this declaration overridable}}
@objc init(baz: Int) {} // expected-note{{overridden declaration is here}}
@objc dynamic init(bar: Int) {}
}
class InitSubclass: InitClass {}
// expected-note@-1 {{'init(bar:)' previously overridden here}}
// expected-note@-2 {{'init(baz:)' previously overridden here}}
extension InitSubclass {
convenience init(arg: Bool) {} // expected-error{{overriding non-@objc declarations from extensions is not supported}}
convenience override init(baz: Int) {}
// expected-error@-1 {{cannot override a non-dynamic class declaration from an extension}}
// expected-error@-2 {{'init(baz:)' has already been overridden}}
convenience override init(bar: Int) {} // expected-error{{'init(bar:)' has already been overridden}}
}

struct InitStruct {
let foo: Int
}
extension InitStruct {
init(foo: Int) {} // expected-error{{invalid redeclaration of synthesized memberwise 'init(foo:)'}}
}

// <rdar://problem/17564699> QoI: Structs should get convenience initializers
struct MyStruct {
Expand Down