Skip to content

[Parse] Ignore '(' on newline after attribute names #83497

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ERROR(pound_diagnostic_expected_string,none,
"expected string literal in %select{#warning|#error}0 directive",(bool))
ERROR(pound_diagnostic_expected,none,
"expected '%0' in %select{#warning|#error}1 directive",(StringRef,bool))
ERROR(pound_diagnostic_expected_parens,none,
ERROR(pound_diagnostic_expected_parens,PointsToFirstBadToken,
"%select{#warning|#error}0 directive requires parentheses",(bool))
ERROR(pound_diagnostic_interpolation,none,
"string interpolation is not allowed in %select{#warning|#error}0 directives",(bool))
Expand Down
34 changes: 20 additions & 14 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2503,7 +2503,7 @@ static std::optional<Identifier> parseSingleAttrOptionImpl(
};
bool isDeclModifier = DeclAttribute::isDeclModifier(DK);

if (!P.Tok.is(tok::l_paren)) {
if (!P.Tok.isFollowingLParen()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated to this change but I've always disliked the name isFollowingLParen, to me it reads as "is token following an l-paren", when really it's "is token a following-l-paren". Maybe we should just call it isSameLineLParen?

if (allowOmitted)
return Identifier();

Expand Down Expand Up @@ -2883,7 +2883,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
.Case("public", AccessLevel::Public)
.Case("open", AccessLevel::Open);

if (!Tok.is(tok::l_paren)) {
if (!Tok.isFollowingLParen()) {
// Normal access control attribute.
AttrRange = Loc;

Expand Down Expand Up @@ -3443,7 +3443,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
}
case DeclAttrKind::PrivateImport: {
// Parse the leading '('.
if (Tok.isNot(tok::l_paren)) {
if (!Tok.isFollowingLParen()) {
diagnose(Loc, diag::attr_expected_lparen, AttrName,
DeclAttribute::isDeclModifier(DK));
return makeParserSuccess();
Expand Down Expand Up @@ -3492,7 +3492,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
}
case DeclAttrKind::ObjC: {
// Unnamed @objc attribute.
if (Tok.isNot(tok::l_paren)) {
if (!Tok.isFollowingLParen()) {
auto attr = ObjCAttr::createUnnamed(Context, AtLoc, Loc);
Attributes.add(attr);
break;
Expand Down Expand Up @@ -3560,7 +3560,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,

case DeclAttrKind::DynamicReplacement: {
// Parse the leading '('.
if (Tok.isNot(tok::l_paren)) {
if (!Tok.isFollowingLParen()) {
diagnose(Loc, diag::attr_expected_lparen, AttrName,
DeclAttribute::isDeclModifier(DK));
return makeParserSuccess();
Expand Down Expand Up @@ -3611,7 +3611,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,

case DeclAttrKind::TypeEraser: {
// Parse leading '('
if (Tok.isNot(tok::l_paren)) {
if (!Tok.isFollowingLParen()) {
diagnose(Loc, diag::attr_expected_lparen, AttrName,
DeclAttribute::isDeclModifier(DK));
return makeParserSuccess();
Expand Down Expand Up @@ -3641,7 +3641,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,

case DeclAttrKind::Specialize:
case DeclAttrKind::Specialized: {
if (Tok.isNot(tok::l_paren)) {
if (!Tok.isFollowingLParen()) {
diagnose(Loc, diag::attr_expected_lparen, AttrName,
DeclAttribute::isDeclModifier(DK));
return makeParserSuccess();
Expand Down Expand Up @@ -3870,7 +3870,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
break;
}
case DeclAttrKind::RawLayout: {
if (Tok.isNot(tok::l_paren)) {
if (!Tok.isFollowingLParen()) {
diagnose(Loc, diag::attr_expected_lparen, AttrName,
DeclAttribute::isDeclModifier(DK));
return makeParserSuccess();
Expand Down Expand Up @@ -4172,7 +4172,7 @@ ParserResult<CustomAttr> Parser::parseCustomAttribute(SourceLoc atLoc) {
// Parse a custom attribute.
auto type = parseType(diag::expected_type, ParseTypeReason::CustomAttribute);
if (type.hasCodeCompletion() || type.isNull()) {
if (Tok.is(tok::l_paren) && isCustomAttributeArgument())
if (Tok.isFollowingLParen() && isCustomAttributeArgument())
skipSingle();

return ParserResult<CustomAttr>(ParserStatus(type));
Expand Down Expand Up @@ -4354,7 +4354,7 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
SourceLoc attrLoc = consumeToken();

// @warn_unused_result with no arguments.
if (Tok.isNot(tok::l_paren)) {
if (!Tok.isFollowingLParen()) {
diagnose(AtLoc, diag::attr_warn_unused_result_removed)
.fixItRemove(SourceRange(AtLoc, attrLoc));

Expand Down Expand Up @@ -4438,7 +4438,7 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,

// Recover by eating @foo(...) when foo is not known.
consumeToken();
if (Tok.is(tok::l_paren))
if (Tok.isFollowingLParen())
skipSingle();

return makeParserError();
Expand Down Expand Up @@ -5910,7 +5910,7 @@ bool Parser::isStartOfSwiftDecl(bool allowPoundIfAttributes,
// If it might be, we do some more digging.

// If this is 'unowned', check to see if it is valid.
if (Tok.getText() == "unowned" && Tok2.is(tok::l_paren)) {
if (Tok.getText() == "unowned" && Tok2.isFollowingLParen()) {
Parser::BacktrackingScope Backtrack(*this);
if (consumeIfParenthesizedUnowned(*this)) {
return isStartOfSwiftDecl(/*allowPoundIfAttributes=*/false,
Expand All @@ -5919,7 +5919,7 @@ bool Parser::isStartOfSwiftDecl(bool allowPoundIfAttributes,
}

// If this is 'nonisolated', check to see if it is valid.
if (Tok.isContextualKeyword("nonisolated") && Tok2.is(tok::l_paren)) {
if (Tok.isContextualKeyword("nonisolated") && Tok2.isFollowingLParen()) {
BacktrackingScope backtrack(*this);
if (consumeIfParenthesizedNonisolated(*this)) {
return isStartOfSwiftDecl(/*allowPoundIfAttributes=*/false,
Expand Down Expand Up @@ -7262,6 +7262,12 @@ ParserStatus Parser::parseDeclPoundDiagnostic() {
bool isError = Tok.is(tok::pound_error);
consumeToken(isError ? tok::pound_error : tok::pound_warning);

if (Tok.isAtStartOfLine()) {
diagnose(Tok, diag::pound_diagnostic_expected_parens, isError)
.fixItInsertAfter(PreviousLoc, "(\"<#message#>\")");
return makeParserSuccess();
}

SourceLoc lParenLoc = Tok.getLoc();
bool hadLParen = consumeIf(tok::l_paren);

Expand Down Expand Up @@ -7358,7 +7364,7 @@ ParserStatus Parser::parseLineDirective(bool isLine) {

unsigned StartLine = 0;
std::optional<StringRef> Filename;
if (!isLine) {
if (!isLine && !Tok.isAtStartOfLine()) {
// #sourceLocation()
// #sourceLocation(file: "foo", line: 42)
if (parseToken(tok::l_paren, diag::sourceLocation_expected, "("))
Expand Down
11 changes: 11 additions & 0 deletions test/Parse/attribute_spacing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ struct MyPropertyWrapper {
struct PropertyWrapperTest {
@MyPropertyWrapper (param: 2) // expected-warning {{extraneous whitespace between attribute name and '('; this is an error in the Swift 6 language mode}}
var x: Int

@MyPropertyWrapper
(param: 2) // expected-error {{expected 'var' keyword in property declaration}} expected-error {{property declaration does not bind any variables}} expected-error {{expected pattern}}
var y: Int
}

let closure1 = { @MainActor (a, b) in // expected-warning {{extraneous whitespace between attribute name and '('; this is an error in the Swift 6 language mode}}
Expand All @@ -31,3 +35,10 @@ let closure2 = { @MainActor
@
MainActor
func mainActorFunc() {}


@inline // expected-error {{expected '(' in 'inline' attribute}}
(never) func neverInline() {} // expected-error {{expected declaration}}

@objc
(whatever) func whateverObjC() {} // expected-error {{expected declaration}}
3 changes: 3 additions & 0 deletions test/Parse/line-directive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ class I55049 {
// expected-error@+1 {{#line directive was renamed to #sourceLocation}}
#line 1_000 "issue-55049.swift"
class I55049_1 {}

#sourceLocation
(file: "nextLine.swift", line: 400) // expected-warning {{expression of type '(file: String, line: Int)' is unused}}
3 changes: 3 additions & 0 deletions test/Sema/pound_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ protocol MyProtocol {
""") // expected-warning @-2 {{warnings support multi-line string literals}}

#warning(#"warnings support \(custom string delimiters)"#) // expected-warning {{warnings support \\(custom string delimiters)}}

#warning // expected-error {{#warning directive requires parentheses}} {{9-9=("<#message#>")}}
("message") // expected-warning {{string literal is unused}}