Skip to content

Fix parsing of @_unavailableFromAsync #78313

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
merged 1 commit into from
Feb 18, 2025
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
1 change: 1 addition & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ IDENTIFIER(AsyncIterator)
IDENTIFIER(load)
IDENTIFIER(main)
IDENTIFIER_WITH_NAME(MainEntryPoint, "$main")
IDENTIFIER(message)
IDENTIFIER(next)
IDENTIFIER_(nsErrorDomain)
IDENTIFIER(objectAtIndexedSubscript)
Expand Down
5 changes: 4 additions & 1 deletion include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,10 @@ class Parser {
/// \param name The parsed name of the label (empty if it doesn't exist, or is
/// _)
/// \param loc The location of the label (empty if it doesn't exist)
void parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc);
/// \param isAttr True if this is an argument label for an attribute (allows, but deprecates, use of
/// \c '=' instead of \c ':').
void parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc,
bool isAttr = false);

enum class DeclNameFlag : uint8_t {
/// If passed, operator basenames are allowed.
Expand Down
67 changes: 46 additions & 21 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3780,35 +3780,60 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
case DeclAttrKind::UnavailableFromAsync: {
StringRef message;
if (consumeIfAttributeLParen()) {
if (!Tok.is(tok::identifier)) {
llvm_unreachable("Flag must start with an identifier");
}

StringRef flag = Tok.getText();
auto tokMayBeArgument = [&]() -> bool {
return Tok.isNot(tok::r_paren, tok::comma) &&
!isKeywordPossibleDeclStart(Context.LangOpts, Tok);
};

if (flag != "message") {
diagnose(Tok.getLoc(), diag::attr_unknown_option, flag, AttrName);
return makeParserError();
}
consumeToken();
if (!consumeIf(tok::colon)) {
if (!Tok.is(tok::equal)) {
diagnose(Tok.getLoc(), diag::attr_expected_colon_after_label, flag);
return makeParserSuccess();
Identifier label;
SourceLoc labelLoc;
parseOptionalArgumentLabel(label, labelLoc, /*isAttr=*/true);

if (label.empty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering (_: "message", using labelLoc is safer.

Suggested change
if (label.empty()) {
if (labelLoc.isInvalid()) {

Same for the newCode check below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added more test cases covering these combos, reworked the code to behave more nicely.

// If we have the identifier 'message', assume the user forgot the
// colon.
if (Tok.isContextualKeyword("message")) {
labelLoc = consumeToken();
auto diag = diagnose(Tok, diag::attr_expected_colon_after_label,
"message");
if (Tok.is(tok::string_literal))
diag.fixItInsertAfter(labelLoc, ":");
else
return makeParserError();
}
diagnose(Tok.getLoc(), diag::replace_equal_with_colon_for_value)
.fixItReplace(Tok.getLoc(), ": ");
consumeToken();
// If the argument list just abruptly cuts off, handle that as a
// missing argument (below). Otherwise, diagnose the missing label.
else if (tokMayBeArgument()) {
if (labelLoc.isValid())
// The user wrote an explicitly omitted label (`_:`).
diagnose(labelLoc, diag::attr_expected_label, "message", AttrName)
.fixItReplace(labelLoc, "message");
else
diagnose(Tok, diag::attr_expected_label, "message", AttrName)
.fixItInsert(Tok.getLoc(), "message: ");
}
// Fall through to parse the argument.
} else if (label != Context.Id_message) {
diagnose(labelLoc, diag::attr_unknown_option, label.str(), AttrName)
.fixItReplace(labelLoc, "message");
return makeParserError();
}

if (!Tok.is(tok::string_literal)) {
diagnose(Tok.getLoc(), diag::attr_expected_string_literal, AttrName);
return makeParserSuccess();
// If this token looks like an argument, replace it; otherwise insert
// before it.
auto endLoc = tokMayBeArgument() ? peekToken().getLoc() : Tok.getLoc();

diagnose(Tok, diag::attr_expected_string_literal, AttrName)
.fixItReplaceChars(Tok.getLoc(), endLoc, "\"<#error message#>\"");

return makeParserError();
}

std::optional<StringRef> value =
getStringLiteralIfNotInterpolated(Tok.getLoc(), flag);
getStringLiteralIfNotInterpolated(Tok.getLoc(), "message");
if (!value)
return makeParserSuccess();
return makeParserError();
Token stringTok = Tok;
consumeToken();
message = *value;
Expand Down
29 changes: 23 additions & 6 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2129,9 +2129,14 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
AppendingExpr));
}

void Parser::parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc) {
void Parser::parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc,
bool isAttr) {
/// A token that has the same meaning as colon, but is deprecated, if one exists for this call.
auto altColon = isAttr ? tok::equal : tok::NUM_TOKENS;

// Check to see if there is an argument label.
if (Tok.canBeArgumentLabel() && peekToken().is(tok::colon)) {
if (Tok.canBeArgumentLabel() && peekToken().isAny(tok::colon, altColon)) {
// Label found, including colon.
auto text = Tok.getText();

// If this was an escaped identifier that need not have been escaped, say
Expand All @@ -2149,11 +2154,23 @@ void Parser::parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc) {
}

loc = consumeArgumentLabel(name, /*diagnoseDollarPrefix=*/false);
consumeToken(tok::colon);
} else if (Tok.is(tok::colon)) {
diagnose(Tok, diag::expected_label_before_colon);
consumeToken(tok::colon);
} else if (Tok.isAny(tok::colon, altColon)) {
// Found only the colon.
diagnose(Tok, diag::expected_label_before_colon)
.fixItInsert(Tok.getLoc(), "<#label#>");
} else {
// No label here.
return;
}

// If we get here, we ought to be on the colon.
assert(Tok.isAny(tok::colon, altColon));

if (Tok.is(altColon))
diagnose(Tok, diag::replace_equal_with_colon_for_value)
.fixItReplace(Tok.getLoc(), ": ");

consumeToken();
}

static bool tryParseArgLabelList(Parser &P, Parser::DeclNameOptions flags,
Expand Down
35 changes: 32 additions & 3 deletions test/Concurrency/unavailable_from_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,54 @@ func asyncFunc() async { // expected-error{{asynchronous global function 'asyncF

// Parsing tests

// expected-error@+1 {{expected label 'message:' in '@_unavailableFromAsync' attribute}} {{24-24=message: }}
@_unavailableFromAsync("almost right, but not quite")
func blarp0() {}

// expected-error@+1 {{expected label 'message:' in '@_unavailableFromAsync' attribute}} {{24-25=message}}
@_unavailableFromAsync(_: "almost right, but not quite")
func blarp0a() {}

// expected-error@+2 {{expected declaration}}
// expected-error@+1:24{{unknown option 'nope' for attribute '_unavailableFromAsync'}}
// expected-error@+1:24{{unknown option 'nope' for attribute '_unavailableFromAsync'}} {{24-28=message}}
@_unavailableFromAsync(nope: "almost right, but not quite")
func blarp1() {}

// expected-error@+2 {{expected declaration}}
// expected-error@+1 {{expected ':' after label 'message'}}
// expected-error@+1 {{expected ':' after label 'message'}} {{none}}
@_unavailableFromAsync(message; "almost right, but not quite")
func blarp2() {}

// expected-error@+1 {{expected ':' after label 'message'}} {{31-31=:}}
@_unavailableFromAsync(message "almost right, but not quite")
func blarp2a() {}

// expected-error@+1:31 {{'=' has been replaced with ':' in attribute arguments}}{{31-32=: }}
@_unavailableFromAsync(message="almost right, but not quite")
func blarp3() {}

// expected-error@+2 {{expected declaration}}
// expected-error@+1 {{expected string literal in '_unavailableFromAsync' attribute}}
// expected-error@+1 {{expected string literal in '_unavailableFromAsync' attribute}} {{33-35="<#error message#>"}}
@_unavailableFromAsync(message: 32)
func blarp4() {}

// expected-error@+2 {{expected declaration}}
// expected-error@+1:24{{unknown option 'fnord' for attribute '_unavailableFromAsync'}} {{24-29=message}}
@_unavailableFromAsync(fnord: 32)
func blarp4a() {}

// expected-error@+3 {{expected declaration}}
// expected-error@+2 {{expected label 'message:' in '@_unavailableFromAsync' attribute}} {{24-25=message}}
// expected-error@+1 {{expected string literal in '_unavailableFromAsync' attribute}} {{27-29="<#error message#>"}}
@_unavailableFromAsync(_: 32)
func blarp4b() {}

// expected-error@+3 {{expected declaration}}
// expected-error@+2 {{expected string literal in '_unavailableFromAsync' attribute}} {{24-26="<#error message#>"}}
// expected-error@+1 {{expected label 'message:' in '@_unavailableFromAsync' attribute}} {{24-24=message: }}
@_unavailableFromAsync(32)
func blarp4c() {}

// expected-error@+2 {{expected declaration}}
// expected-error@+1 {{message cannot be an interpolated string}}
@_unavailableFromAsync(message: "blarppy blarp \(31 + 10)")
Expand Down