Skip to content

Commit cc61a3b

Browse files
committed
[Typechecker] Do not diagnose redeclarations when both declarations are implicit
1 parent 17d1ce9 commit cc61a3b

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -712,48 +712,44 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current) const {
712712
current->getName(),
713713
otherInit->isMemberwiseInitializer());
714714
} else if (current->isImplicit() || other->isImplicit()) {
715-
// If both declarations are implicit, we diagnose the nearest
716-
// non-implicit DC because it is likely that we do not have a
717-
// valid source location for the declaration and we want to
718-
// avoid emitting it at an unknown location.
715+
// If both declarations are implicit, we do not diagnose anything
716+
// as it would lead to misleading diagnostics and it's likely that
717+
// there's nothing actionable about it due to its implicit nature.
718+
// One special case for this is property wrappers.
719719
//
720720
// Otherwise, if 'current' is implicit, then we diagnose 'other'
721721
// since 'other' is a redeclaration of 'current'. Similarly, if
722722
// 'other' is implicit, we diagnose 'current'.
723-
const auto *declToDiagnose = currentDC->getAsDecl();
723+
const Decl *declToDiagnose = nullptr;
724724
if (current->isImplicit() && other->isImplicit()) {
725725
// If 'current' is a property wrapper backing storage property
726726
// or projected value property, then diagnose the wrapped
727-
// property instead of the nearest non-implicit DC.
727+
// property.
728728
if (auto VD = dyn_cast<VarDecl>(current)) {
729729
if (auto originalWrappedProperty =
730730
VD->getOriginalWrappedProperty()) {
731731
declToDiagnose = originalWrappedProperty;
732732
}
733-
} else {
734-
// Get the nearest non-implicit decl context
735-
while (declToDiagnose && declToDiagnose->isImplicit() &&
736-
declToDiagnose->getDeclContext()) {
737-
declToDiagnose = declToDiagnose->getDeclContext()->getAsDecl();
738-
}
739733
}
740734
} else {
741735
declToDiagnose = current->isImplicit() ? other : current;
742736
}
743737

744-
// Figure out if the the declaration we've redeclared is a synthesized
745-
// witness for a protocol requirement.
746-
bool isProtocolRequirement = false;
747-
if (auto VD = dyn_cast<ValueDecl>(current->isImplicit() ? current
748-
: other)) {
749-
isProtocolRequirement = llvm::any_of(
750-
VD->getSatisfiedProtocolRequirements(), [&](ValueDecl *req) {
751-
return req->getName() == VD->getName();
752-
});
738+
if (declToDiagnose) {
739+
// Figure out if the the declaration we've redeclared is a
740+
// synthesized witness for a protocol requirement.
741+
bool isProtocolRequirement = false;
742+
if (auto VD = dyn_cast<ValueDecl>(current->isImplicit() ? current
743+
: other)) {
744+
isProtocolRequirement = llvm::any_of(
745+
VD->getSatisfiedProtocolRequirements(), [&](ValueDecl *req) {
746+
return req->getName() == VD->getName();
747+
});
748+
}
749+
declToDiagnose->diagnose(diag::invalid_redecl_implicit,
750+
current->getDescriptiveKind(),
751+
isProtocolRequirement, other->getName());
753752
}
754-
declToDiagnose->diagnose(diag::invalid_redecl_implicit,
755-
current->getDescriptiveKind(),
756-
isProtocolRequirement, other->getName());
757753

758754
// Emit a specialized note if the one of the declarations is
759755
// the backing storage property ('_foo') or projected value

test/decl/protocol/special/coding/struct_codable_simple.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,19 @@ let _ = SimpleStruct.encode(to:)
3131
let _ = SimpleStruct.CodingKeys.self // expected-error {{'CodingKeys' is inaccessible due to 'private' protection level}}
3232

3333
// rdar://problem/59655704
34-
struct SR_12248_1: Codable {
35-
// expected-error@-1 {{type 'SR_12248_1' does not conform to protocol 'Encodable'}}
36-
// expected-error@-2 {{invalid redeclaration of synthesized enum case 'x'}}
37-
34+
struct SR_12248_1: Codable { // expected-error {{type 'SR_12248_1' does not conform to protocol 'Encodable'}}
3835
var x: Int // expected-note {{'x' previously declared here}}
3936
var x: Int // expected-error {{invalid redeclaration of 'x'}}
4037
// expected-note@-1 {{cannot automatically synthesize 'Encodable' because '<<error type>>' does not conform to 'Encodable'}}
4138
// expected-note@-2 {{cannot automatically synthesize 'Encodable' because '<<error type>>' does not conform to 'Encodable'}}
4239
}
4340

44-
struct SR_12248_2: Decodable {
45-
// expected-error@-1 {{invalid redeclaration of synthesized enum case 'x'}}
41+
struct SR_12248_2: Decodable {
4642
var x: Int // expected-note {{'x' previously declared here}}
4743
var x: Int // expected-error {{invalid redeclaration of 'x'}}
4844
}
4945

50-
struct SR_12248_3: Encodable {
51-
// expected-error@-1 {{invalid redeclaration of synthesized enum case 'x'}}
46+
struct SR_12248_3: Encodable {
5247
var x: Int // expected-note {{'x' previously declared here}}
5348
var x: Int // expected-error {{invalid redeclaration of 'x'}}
5449
}

0 commit comments

Comments
 (0)