-
Notifications
You must be signed in to change notification settings - Fork 440
Disallow whitespace between @
, attribute name and (
#2466
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,7 +176,14 @@ extension Parser { | |
argumentMode: AttributeArgumentMode, | ||
parseArguments: (inout Parser) -> RawAttributeSyntax.Arguments | ||
) -> RawAttributeListSyntax.Element { | ||
let (unexpectedBeforeAtSign, atSign) = self.expect(.atSign) | ||
var (unexpectedBeforeAtSign, atSign) = self.expect(.atSign) | ||
if atSign.trailingTriviaByteLength > 0 || self.currentToken.leadingTriviaByteLength > 0 { | ||
let diagnostic = TokenDiagnostic( | ||
self.swiftVersion < .v6 ? .extraneousTrailingWhitespaceWarning : .extraneousTrailingWhitespaceError, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t think that’s realistically possible. We need to have a different case in |
||
byteOffset: atSign.leadingTriviaByteLength + atSign.tokenText.count | ||
) | ||
atSign = atSign.tokenView.withTokenDiagnostic(tokenDiagnostic: diagnostic, arena: self.arena) | ||
} | ||
let attributeName = self.parseAttributeName() | ||
let shouldParseArgument: Bool | ||
switch argumentMode { | ||
|
@@ -190,7 +197,14 @@ extension Parser { | |
shouldParseArgument = false | ||
} | ||
if shouldParseArgument { | ||
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen) | ||
var (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen) | ||
if unexpectedBeforeLeftParen == nil && (attributeName.raw.trailingTriviaByteLength > 0 || leftParen.leadingTriviaByteLength > 0) { | ||
let diagnostic = TokenDiagnostic( | ||
self.swiftVersion < .v6 ? .extraneousLeadingWhitespaceWarning : .extraneousLeadingWhitespaceError, | ||
byteOffset: 0 | ||
) | ||
leftParen = leftParen.tokenView.withTokenDiagnostic(tokenDiagnostic: diagnostic, arena: self.arena) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand why you did this , but the inconsistency of the diagnosing token between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it truly is. But I think everything else would be even more of a pain… |
||
} | ||
let argument = parseArguments(&self) | ||
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen) | ||
return .attribute( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1283,20 +1283,18 @@ extension Parser { | |
flavor: ExprFlavor | ||
) -> RawMacroExpansionExprSyntax { | ||
var (unexpectedBeforePound, pound) = self.expect(.pound) | ||
if pound.trailingTriviaByteLength != 0 { | ||
if pound.trailingTriviaByteLength > 0 || currentToken.leadingTriviaByteLength > 0 { | ||
// If there are whitespaces after '#' diagnose. | ||
unexpectedBeforePound = RawUnexpectedNodesSyntax(combining: unexpectedBeforePound, pound, arena: self.arena) | ||
pound = self.missingToken(.pound) | ||
let diagnostic = TokenDiagnostic( | ||
.extraneousTrailingWhitespaceError, | ||
byteOffset: pound.leadingTriviaByteLength + pound.tokenText.count | ||
) | ||
pound = pound.tokenView.withTokenDiagnostic(tokenDiagnostic: diagnostic, arena: self.arena) | ||
Comment on lines
-1288
to
+1292
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why you decided to go with TokenDiagnostic instead of the previous "unexpected" route? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the unexpected route didn’t have any way of representing warnings, just errors. If there are unexpected or missing tokens in the syntax tree, we always consider the tree as having an error (which makes sense to me) and |
||
} | ||
var unexpectedBeforeMacroName: RawUnexpectedNodesSyntax? | ||
var macroName: RawTokenSyntax | ||
let unexpectedBeforeMacroName: RawUnexpectedNodesSyntax? | ||
let macroName: RawTokenSyntax | ||
if !self.atStartOfLine { | ||
(unexpectedBeforeMacroName, macroName) = self.expectIdentifier(allowKeywordsAsIdentifier: true) | ||
if macroName.leadingTriviaByteLength != 0 { | ||
// If there're whitespaces after '#' diagnose. | ||
unexpectedBeforeMacroName = RawUnexpectedNodesSyntax(combining: unexpectedBeforeMacroName, macroName, arena: self.arena) | ||
pound = self.missingToken(.identifier, text: macroName.tokenText) | ||
} | ||
} else { | ||
unexpectedBeforeMacroName = nil | ||
macroName = self.missingToken(.identifier) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension Parser { | ||
/// A Swift language version. | ||
public enum SwiftVersion: Comparable { | ||
case v4 | ||
case v5 | ||
case v6 | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.