Skip to content

Commit 177515f

Browse files
Merge pull request #59097 from LucianoPAlmeida/58741-to5-7
[5.7] [Cherry-pick] Do not suggest moving `any` or `some` to the beginning of a composition if the first type is already an existential or opaque type
2 parents 77783a4 + 8e4a1c1 commit 177515f

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

lib/Parse/ParseType.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,13 +881,20 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
881881
Tok.isContextualKeyword("any")) {
882882
auto keyword = Tok.getText();
883883
auto badLoc = consumeToken();
884+
885+
// Suggest moving `some` or `any` in front of the first type unless
886+
// the first type is an opaque or existential type.
887+
if (opaqueLoc.isValid() || anyLoc.isValid()) {
888+
diagnose(badLoc, diag::opaque_mid_composition, keyword)
889+
.fixItRemove(badLoc);
890+
} else {
891+
diagnose(badLoc, diag::opaque_mid_composition, keyword)
892+
.fixItRemove(badLoc)
893+
.fixItInsert(FirstTypeLoc, keyword.str() + " ");
894+
}
884895

885896
const bool isAnyKeyword = keyword.equals("any");
886897

887-
diagnose(badLoc, diag::opaque_mid_composition, keyword)
888-
.fixItRemove(badLoc)
889-
.fixItInsert(FirstTypeLoc, keyword.str() + " ");
890-
891898
if (isAnyKeyword) {
892899
if (anyLoc.isInvalid()) {
893900
anyLoc = badLoc;

test/type/explicit_existential.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,17 @@ func anyAny() {
156156

157157
protocol P1 {}
158158
protocol P2 {}
159+
protocol P3 {}
159160
do {
160161
// Test that we don't accidentally misparse an 'any' type as a 'some' type
161162
// and vice versa.
162-
let _: P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}}
163-
let _: any P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}}
164-
let _: any P1 & some P2 // expected-error {{'some' should appear at the beginning of a composition}}
163+
let _: P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}} {{15-19=}} {{10-10=any }}
164+
let _: any P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}} {{19-23=}}
165+
let _: any P1 & P2 & any P3 // expected-error {{'any' should appear at the beginning of a composition}} {{24-28=}}
166+
let _: any P1 & some P2 // expected-error {{'some' should appear at the beginning of a composition}} {{19-24=}}
165167
let _: some P1 & any P2
166168
// expected-error@-1 {{'some' type can only be declared on a single property declaration}}
167-
// expected-error@-2 {{'any' should appear at the beginning of a composition}}
169+
// expected-error@-2 {{'any' should appear at the beginning of a composition}} {{20-24=}}
168170
}
169171

170172
struct ConcreteComposition: P1, P2 {}

test/type/opaque.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,11 @@ func foo_repl<S>(_ s: S) -> some Proto {
488488

489489
protocol SomeProtocolA {}
490490
protocol SomeProtocolB {}
491-
struct SomeStructC: SomeProtocolA, SomeProtocolB {}
491+
protocol SomeProtocolC {}
492+
struct SomeStructC: SomeProtocolA, SomeProtocolB, SomeProtocolC {}
492493
let someProperty: SomeProtocolA & some SomeProtocolB = SomeStructC() // expected-error {{'some' should appear at the beginning of a composition}}{{35-40=}}{{19-19=some }}
494+
let someOtherProperty: some SomeProtocolA & some SomeProtocolB = SomeStructC() // expected-error {{'some' should appear at the beginning of a composition}}{{45-50=}}
495+
let someThirdProperty: some SomeProtocolA & SomeProtocolB & some SomeProtocolC = SomeStructC() // expected-error {{'some' should appear at the beginning of a composition}}{{61-66=}}
493496

494497
// An opaque result type on a protocol extension member effectively
495498
// contains an invariant reference to 'Self', and therefore cannot

0 commit comments

Comments
 (0)