Skip to content

Commit 9dad512

Browse files
Merge pull request swiftlang#78356 from AnthonyLatsis/tuber-melanosporum
Sema: Minor QoI improvements for existential `any` diagnosis
2 parents efe8aeb + 56e2e19 commit 9dad512

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

lib/AST/Decl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6835,10 +6835,14 @@ bool ProtocolDecl::existentialConformsToSelf() const {
68356835
}
68366836

68376837
bool ProtocolDecl::hasSelfOrAssociatedTypeRequirements() const {
6838+
// Because we will have considered all the protocols in a cyclic hierarchy by
6839+
// the time the cycle is hit.
6840+
const bool resultForCycle = false;
6841+
68386842
return evaluateOrDefault(getASTContext().evaluator,
68396843
HasSelfOrAssociatedTypeRequirementsRequest{
68406844
const_cast<ProtocolDecl *>(this)},
6841-
true);
6845+
resultForCycle);
68426846
}
68436847

68446848
bool ProtocolDecl::existentialRequiresAny() const {

lib/Sema/TypeCheckType.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5907,6 +5907,10 @@ TypeResolver::resolveExistentialType(ExistentialTypeRepr *repr,
59075907
diagnose(repr->getLoc(), diag::incorrect_optional_any,
59085908
constraintType)
59095909
.fixItReplace(repr->getSourceRange(), fix);
5910+
5911+
// Recover by returning the intended type, but mark the type
5912+
// representation as invalid to prevent it from being diagnosed elsewhere.
5913+
repr->setInvalid();
59105914
return constraintType;
59115915
}
59125916

@@ -6348,13 +6352,6 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {
63486352
continue;
63496353
}
63506354

6351-
// Look through '?' and '!' too; `any P?` et al. is diagnosed in the
6352-
// type resolver.
6353-
if (isa<OptionalTypeRepr>(*it) ||
6354-
isa<ImplicitlyUnwrappedOptionalTypeRepr>(*it)) {
6355-
continue;
6356-
}
6357-
63586355
break;
63596356
}
63606357

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 5 -verify-additional-prefix no-explicit-any-
2+
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-upcoming-feature ExistentialAny -verify-additional-prefix explicit-any-
3+
4+
// REQUIRES: swift_feature_ExistentialAny
5+
6+
// 'HasSelfOrAssociatedTypeRequirementsRequest' should evaluate to false in
7+
// the event of a cycle because we will have considered all the protocols in a
8+
// cyclic hierarchy by the time the cycle is hit.
9+
do {
10+
do {
11+
protocol P1 : P2 {}
12+
// expected-no-explicit-any-note@-1 2 {{protocol 'P1' declared here}}
13+
// expected-explicit-any-note@-2 1 {{protocol 'P1' declared here}}
14+
protocol P2 : P1 {}
15+
// expected-no-explicit-any-error@-1 2 {{protocol 'P2' refines itself}}
16+
// expected-explicit-any-error@-2 1 {{protocol 'P2' refines itself}}
17+
18+
// Diagnosed only with the feature enabled, as a protocol without
19+
// "HasSelfOrAssociatedTypeRequirements" should.
20+
let _: P2
21+
// expected-explicit-any-error@-1 {{use of protocol 'P2' as a type must be written 'any P2'}}
22+
}
23+
do {
24+
protocol P0 { associatedtype A }
25+
protocol P1 : P2, P0 {}
26+
// expected-no-explicit-any-note@-1 2 {{protocol 'P1' declared here}}
27+
// expected-explicit-any-note@-2 1 {{protocol 'P1' declared here}}
28+
protocol P2 : P1 {}
29+
// expected-no-explicit-any-error@-1 2 {{protocol 'P2' refines itself}}
30+
// expected-explicit-any-error@-2 1 {{protocol 'P2' refines itself}}
31+
32+
let _: P2
33+
// expected-error@-1 {{use of protocol 'P2' as a type must be written 'any P2'}}
34+
}
35+
}

0 commit comments

Comments
 (0)