Skip to content

[lib/Parse] #58741 Do not suggest moving any or some to the beginning of a compositi… #58742

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
Show file tree
Hide file tree
Changes from all 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
15 changes: 11 additions & 4 deletions lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,13 +881,20 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
Tok.isContextualKeyword("any")) {
auto keyword = Tok.getText();
auto badLoc = consumeToken();

// Suggest moving `some` or `any` in front of the first type unless
// the first type is an opaque or existential type.
if (opaqueLoc.isValid() || anyLoc.isValid()) {
diagnose(badLoc, diag::opaque_mid_composition, keyword)
.fixItRemove(badLoc);
} else {
diagnose(badLoc, diag::opaque_mid_composition, keyword)
.fixItRemove(badLoc)
.fixItInsert(FirstTypeLoc, keyword.str() + " ");
}

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

diagnose(badLoc, diag::opaque_mid_composition, keyword)
.fixItRemove(badLoc)
.fixItInsert(FirstTypeLoc, keyword.str() + " ");

if (isAnyKeyword) {
if (anyLoc.isInvalid()) {
anyLoc = badLoc;
Expand Down
10 changes: 6 additions & 4 deletions test/type/explicit_existential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,17 @@ func anyAny() {

protocol P1 {}
protocol P2 {}
protocol P3 {}
do {
// Test that we don't accidentally misparse an 'any' type as a 'some' type
// and vice versa.
let _: P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}}
let _: any P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}}
let _: any P1 & some P2 // expected-error {{'some' should appear at the beginning of a composition}}
let _: P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}} {{15-19=}} {{10-10=any }}
let _: any P1 & any P2 // expected-error {{'any' should appear at the beginning of a composition}} {{19-23=}}
let _: any P1 & P2 & any P3 // expected-error {{'any' should appear at the beginning of a composition}} {{24-28=}}
let _: any P1 & some P2 // expected-error {{'some' should appear at the beginning of a composition}} {{19-24=}}
let _: some P1 & any P2
// expected-error@-1 {{'some' type can only be declared on a single property declaration}}
// expected-error@-2 {{'any' should appear at the beginning of a composition}}
// expected-error@-2 {{'any' should appear at the beginning of a composition}} {{20-24=}}
}

struct ConcreteComposition: P1, P2 {}
Expand Down
5 changes: 4 additions & 1 deletion test/type/opaque.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,11 @@ func foo_repl<S>(_ s: S) -> some Proto {

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

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