Skip to content

Commit d5d6586

Browse files
committed
[Sema] Emit a duplicate inheritance error for duplicate 'Any' conformances
Previously, for the following: struct S : Any, Any {} we would just emit two warnings. But in the interest of consistency, this commit changes the behaviour such that we instead emit a warning and an error for the duplicate conformance.
1 parent 8d0dadd commit d5d6586

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -409,25 +409,6 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
409409
auto isConstraint =
410410
isa<AbstractTypeParamDecl>(decl) || isa<ProtocolDecl>(decl);
411411

412-
// Check for a stated conformance to Any, which is redundant.
413-
// Ignore cases of ': Any' *constraints* that are parsed as inheritance
414-
// clauses, such as with protocol and placeholder decls e.g <T : Any>, as
415-
// these are handled by the generic signature builder along with redundant
416-
// constraints that appear in 'where' clauses.
417-
if (inheritedCanTy == Context.TheAnyType) {
418-
if (!isConstraint) {
419-
auto diagLoc = inherited.getSourceRange().Start;
420-
auto diag = diagnose(diagLoc, diag::redundant_conformance_warning,
421-
declInterfaceTy, inheritedTy);
422-
diag.highlight(inherited.getSourceRange());
423-
tryAddRemovalFixIt(diag, i);
424-
diag.flush();
425-
426-
diagnose(diagLoc, diag::all_types_implicitly_conform_to, inheritedTy);
427-
}
428-
continue;
429-
}
430-
431412
// Check whether we inherited from the same type twice.
432413
auto knownType = inheritedTypes.find(inheritedCanTy);
433414
if (knownType != inheritedTypes.end()) {
@@ -458,6 +439,25 @@ void TypeChecker::checkInheritanceClause(Decl *decl,
458439
}
459440
inheritedTypes[inheritedCanTy] = { i, inherited.getSourceRange() };
460441

442+
// Check for a stated conformance to Any, which is redundant.
443+
// Ignore cases of ': Any' *constraints* that are parsed as inheritance
444+
// clauses, such as with protocol and placeholder decls e.g <T : Any>, as
445+
// these are handled by the generic signature builder along with redundant
446+
// constraints that appear in 'where' clauses.
447+
if (inheritedCanTy == Context.TheAnyType) {
448+
if (!isConstraint) {
449+
auto diagLoc = inherited.getSourceRange().Start;
450+
auto diag = diagnose(diagLoc, diag::redundant_conformance_warning,
451+
declInterfaceTy, inheritedTy);
452+
diag.highlight(inherited.getSourceRange());
453+
tryAddRemovalFixIt(diag, i);
454+
diag.flush();
455+
456+
diagnose(diagLoc, diag::all_types_implicitly_conform_to, inheritedTy);
457+
}
458+
continue;
459+
}
460+
461461
if (inheritedTy->isExistentialType()) {
462462
auto layout = inheritedTy->getExistentialLayout();
463463

test/FixCode/fixits-redundant-any.swift.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ class C7 : Any, AnyObject {}
4343
// FIX-ME: Implement redundant Any fix-its for requirements parsed as inheritance clauses.
4444
struct S12<T : Any> {}
4545
protocol P2 : Any {}
46-
protocol P3 : Any, Any {}
46+
protocol P3 : Any {}
4747
protocol P4 : class, Any, AnyObject {} // Parse will re-arrange 'class' to be first.
4848
protocol P5 : Any, AnyObject {}
4949
protocol P6 : AnyObject, Any {}
5050

5151
protocol P7 {
5252
associatedtype X1 : Any
53-
associatedtype X2 : Any, Any
53+
associatedtype X2 : Any
5454
}

test/Generics/redundant_constraints.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protocol P4 {
7575
// expected-warning@-11 {{redundant conformance constraint 'Self.X3': 'Any'}} {{37-47=}}
7676
// expected-note@-12 {{all types implicitly conform to 'Any'}}
7777

78-
associatedtype X1 : Any, Any
78+
associatedtype X1 : Any, Any // expected-error {{duplicate inheritance from 'Any'}} {{26-31=}}
7979
associatedtype X2 where X2 : Any, X2 : Any
8080
associatedtype X3 where X3 : Any, X3 : Any, X3 : P1
8181
}
@@ -91,6 +91,7 @@ protocol P7 : Any, Any {}
9191
// expected-note@-2 {{all types implicitly conform to 'Any'}}
9292
// expected-warning@-3 {{redundant conformance constraint 'Self': 'Any'}}
9393
// expected-note@-4 {{all types implicitly conform to 'Any'}}
94+
// expected-error@-5 {{duplicate inheritance from 'Any'}} {{18-23=}}
9495

9596
func f1<T : Any>(_ x: T) {} // expected-warning {{redundant conformance constraint 'T': 'Any'}}
9697
// expected-note@-1 {{all types implicitly conform to 'Any'}}

test/decl/protocol/protocols.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ class X11 : AnyAnyAlias {} // expected-warning {{redundant conformance of 'X11'
110110
struct X12 : Any, Any {}
111111
// expected-warning@-1 {{redundant conformance of 'X12' to 'Any'}} {{14-19=}}
112112
// expected-note@-2 {{all types implicitly conform to 'Any'}}
113-
// expected-warning@-3 {{redundant conformance of 'X12' to 'Any'}} // FIX-ME(SR-8102): Add expected fix-it {{17-22=}}
114-
// expected-note@-4 {{all types implicitly conform to 'Any'}}
113+
// expected-error@-3 {{duplicate inheritance from 'Any'}} // FIX-ME(SR-8102): Add expected fix-it {{17-22=}}
115114

116115
// Explicit conformance checks (unsuccessful)
117116

0 commit comments

Comments
 (0)