@@ -796,6 +796,7 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) {
796796// /
797797// / type-composition:
798798// / 'some'? type-simple
799+ // / 'any'? type-simple
799800// / type-composition '&' type-simple
800801ParserResult<TypeRepr>
801802Parser::parseTypeSimpleOrComposition (Diag<> MessageID, ParseTypeReason reason) {
@@ -818,8 +819,9 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
818819 SomeTypeContext.setTransparent ();
819820 }
820821
821- auto applyOpaque = [&](TypeRepr *type) -> TypeRepr* {
822- if (opaqueLoc.isValid ()) {
822+ auto applyOpaque = [&](TypeRepr *type) -> TypeRepr * {
823+ if (opaqueLoc.isValid () &&
824+ (anyLoc.isInvalid () || SourceMgr.isBeforeInBuffer (opaqueLoc, anyLoc))) {
823825 type = new (Context) OpaqueReturnTypeRepr (opaqueLoc, type);
824826 } else if (anyLoc.isValid ()) {
825827 type = new (Context) ExistentialTypeRepr (anyLoc, type);
@@ -879,14 +881,18 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
879881 auto keyword = Tok.getText ();
880882 auto badLoc = consumeToken ();
881883
884+ const bool isAnyKeyword = keyword.equals (" any" );
885+
882886 diagnose (badLoc, diag::opaque_mid_composition, keyword)
883887 .fixItRemove (badLoc)
884888 .fixItInsert (FirstTypeLoc, keyword.str () + " " );
885889
886- if (opaqueLoc.isInvalid ()) {
890+ if (isAnyKeyword) {
891+ if (anyLoc.isInvalid ()) {
892+ anyLoc = badLoc;
893+ }
894+ } else if (opaqueLoc.isInvalid ()) {
887895 opaqueLoc = badLoc;
888- } else if (anyLoc.isInvalid ()) {
889- anyLoc = badLoc;
890896 }
891897 }
892898
0 commit comments