Skip to content

Improve diagnostics for effect specifiers #1260

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
Jan 26, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,62 @@ public let COMMON_NODES: [Node] = [
requiresLeadingNewline: true)
]),

Node(name: "DeclEffectSpecifiers",
nameForDiagnostics: "effect specifiers",
kind: "Syntax",
traits: [
"EffectSpecifiers"
],
children: [
Child(name: "AsyncSpecifier",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"async",
"reasync"
]),
Child(name: "ThrowsSpecifier",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"throws",
"rethrows"
])
]),

Node(name: "TypeEffectSpecifiers",
nameForDiagnostics: "effect specifiers",
kind: "Syntax",
traits: [
"EffectSpecifiers"
],
children: [
Child(name: "AsyncSpecifier",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"async"
]),
Child(name: "ThrowsSpecifier",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"throws"
])
]),

Node(name: "UnexpectedNodes",
nameForDiagnostics: nil,
description: "A collection of syntax nodes that occurred in the source code butcould not be used to form a valid syntax tree.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,9 @@ public let DECL_NODES: [Node] = [
children: [
Child(name: "Input",
kind: "ParameterClause"),
Child(name: "AsyncOrReasyncKeyword",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"async",
"reasync"
]),
Child(name: "ThrowsOrRethrowsKeyword",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"throws",
"rethrows"
]),
Child(name: "EffectSpecifiers",
kind: "DeclEffectSpecifiers",
isOptional: true),
Child(name: "Output",
kind: "ReturnClause",
isOptional: true)
Expand Down Expand Up @@ -1090,25 +1073,9 @@ public let DECL_NODES: [Node] = [
Child(name: "Parameter",
kind: "AccessorParameter",
isOptional: true),
Child(name: "AsyncKeyword",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"async"
]),
Child(name: "ThrowsKeyword",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"throws",
"rethrows"
]),
Child(name: "EffectSpecifiers",
kind: "DeclEffectSpecifiers",
isOptional: true),
Child(name: "Body",
kind: "CodeBlock",
isOptional: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,8 @@ public let EXPR_NODES: [Node] = [
nameForDiagnostics: nil,
kind: "Expr",
children: [
Child(name: "AsyncKeyword",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"async"
]),
Child(name: "ThrowsToken",
kind: "ThrowsToken",
Child(name: "EffectSpecifiers",
kind: "TypeEffectSpecifiers",
isOptional: true),
Child(name: "ArrowToken",
kind: "ArrowToken",
Expand Down Expand Up @@ -796,24 +787,9 @@ public let EXPR_NODES: [Node] = [
Child(name: "Input",
kind: "ParameterClause")
]),
Child(name: "AsyncKeyword",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"async"
]),
Child(name: "ThrowsTok",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"throws"
]),
Child(name: "EffectSpecifiers",
kind: "TypeEffectSpecifiers",
isOptional: true),
Child(name: "Output",
kind: "ReturnClause",
isOptional: true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ public let TRAITS: [Trait] = [
Child(name: "Statements", kind: "CodeBlockItemList"),
]
),
Trait(traitName: "EffectSpecifiers",
children: [
Child(name: "UnexpectedBeforeAsyncSpecifier", kind: "UnexpectedNodes", isOptional: true),
Child(name: "AsyncSpecifier", kind: "KeywordToken", isOptional: true),
Child(name: "UnexpectedBetweenAsyncSpecifierAndThrowsSpecifier", kind: "UnexpectedNodes", isOptional: true),
Child(name: "ThrowsSpecifier", kind: "KeywordToken", isOptional: true),
Child(name: "UnexpectedAfterThrowsSpecifier", kind: "UnexpectedNodes", isOptional: true),
]
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -330,30 +330,11 @@ public let TYPE_NODES: [Node] = [
tokenChoices: [
"RightParen"
]),
Child(name: "AsyncKeyword",
kind: "KeyworkToken",
isOptional: true,
textChoices: [
"async"
]),
Child(name: "ThrowsOrRethrowsKeyword",
kind: "KeywordToken",
isOptional: true,
tokenChoices: [
"Keyword"
],
textChoices: [
"throws",
"rethrows",
"throw"
]),
Child(name: "Arrow",
kind: "ArrowToken",
tokenChoices: [
"Arrow"
]),
Child(name: "ReturnType",
kind: "Type")
Child(name: "EffectSpecifiers",
kind: "TypeEffectSpecifiers",
isOptional: true),
Child(name: "Output",
kind: "ReturnClause")
]),

Node(name: "AttributedType",
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_swift_host_library(SwiftParser
RawTokenKindMatch.swift
RawTokenKindSubset.swift
Recovery.swift
Specifiers.swift
Statements.swift
StringLiterals.swift
SyntaxUtils.swift
Expand Down
92 changes: 16 additions & 76 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1217,26 +1217,25 @@ extension Parser {
}

/// If a `throws` keyword appears right in front of the `arrow`, it is returned as `misplacedThrowsKeyword` so it can be synthesized in front of the arrow.
@_spi(RawSyntax)
public mutating func parseFunctionReturnClause() -> (returnClause: RawReturnClauseSyntax, misplacedThrowsKeyword: RawTokenSyntax?) {
mutating func parseFunctionReturnClause<S: RawEffectSpecifiersTrait>(effectSpecifiers: inout S?, allowNamedOpaqueResultType: Bool) -> RawReturnClauseSyntax {
let (unexpectedBeforeArrow, arrow) = self.expect(.arrow)
var misplacedThrowsKeyword: RawTokenSyntax? = nil
let unexpectedBeforeReturnType: RawUnexpectedNodesSyntax?
if let throwsKeyword = self.consume(ifAny: [.keyword(.rethrows), .keyword(.throws)]) {
misplacedThrowsKeyword = throwsKeyword
unexpectedBeforeReturnType = RawUnexpectedNodesSyntax(elements: [RawSyntax(throwsKeyword)], arena: self.arena)
let unexpectedBeforeReturnType = self.parseMisplacedEffectSpecifiers(&effectSpecifiers)
let result: RawTypeSyntax
if allowNamedOpaqueResultType {
result = self.parseResultType()
} else {
unexpectedBeforeReturnType = nil
result = self.parseType()
}
let result = self.parseResultType()
let unexpectedAfterReturnType = self.parseMisplacedEffectSpecifiers(&effectSpecifiers)
let returnClause = RawReturnClauseSyntax(
unexpectedBeforeArrow,
arrow: arrow,
unexpectedBeforeReturnType,
returnType: result,
unexpectedAfterReturnType,
arena: self.arena
)
return (returnClause, misplacedThrowsKeyword)
return returnClause
}
}

Expand Down Expand Up @@ -1318,32 +1317,18 @@ extension Parser {
public mutating func parseFunctionSignature() -> RawFunctionSignatureSyntax {
let input = self.parseParameterClause(for: .functionParameters)

let async: RawTokenSyntax?
if let asyncTok = self.consume(if: .keyword(.async)) {
async = asyncTok
} else if let reasync = self.consume(if: .keyword(.reasync)) {
async = reasync
} else {
async = nil
}

var throwsKeyword = self.consume(ifAny: [.keyword(.throws), .keyword(.rethrows)])
var effectSpecifiers = self.parseDeclEffectSpecifiers()

let output: RawReturnClauseSyntax?
if self.at(.arrow) {
Copy link
Contributor

@bnbarham bnbarham Jan 24, 2023

Choose a reason for hiding this comment

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

I imagine we wouldn't be recovering well from random junk before the arrow right now? Could this use canRecoverTo instead?

EDIT: With a low precedence so we don't walk past eg. func etc.

let result = self.parseFunctionReturnClause()
output = result.returnClause
if let misplacedThrowsKeyword = result.misplacedThrowsKeyword, throwsKeyword == nil {
throwsKeyword = RawTokenSyntax(missing: misplacedThrowsKeyword.tokenKind, arena: self.arena)
}
output = self.parseFunctionReturnClause(effectSpecifiers: &effectSpecifiers, allowNamedOpaqueResultType: true)
} else {
output = nil
}

return RawFunctionSignatureSyntax(
input: input,
asyncOrReasyncKeyword: async,
throwsOrRethrowsKeyword: throwsKeyword,
effectSpecifiers: effectSpecifiers,
output: output,
arena: self.arena
)
Expand Down Expand Up @@ -1384,7 +1369,8 @@ extension Parser {

let indices = self.parseParameterClause(for: .indices)

let result = self.parseFunctionReturnClause().returnClause
var misplacedEffectSpecifiers: RawDeclEffectSpecifiersSyntax?
let result = self.parseFunctionReturnClause(effectSpecifiers: &misplacedEffectSpecifiers, allowNamedOpaqueResultType: true)

// Parse a 'where' clause if present.
let genericWhereClause: RawGenericWhereClauseSyntax?
Expand Down Expand Up @@ -1588,41 +1574,6 @@ extension Parser {
)
}

@_spi(RawSyntax)
public mutating func parseEffectsSpecifier() -> RawTokenSyntax? {
// 'async'
if let async = self.consume(if: .keyword(.async)) {
return async
}

// 'reasync'
if let reasync = self.consume(if: .keyword(.reasync)) {
return reasync
}

// 'throws'/'rethrows'
if let throwsRethrows = self.consume(ifAny: [.keyword(.throws), .keyword(.rethrows)]) {
return throwsRethrows
}

// diagnose 'throw'/'try'.
if let throwTry = self.consume(ifAny: [.keyword(.throw), .keyword(.try)], allowTokenAtStartOfLine: false) {
return throwTry
}

return nil
}

@_spi(RawSyntax)
public mutating func parseEffectsSpecifiers() -> [RawTokenSyntax] {
var specifiers = [RawTokenSyntax]()
var loopProgress = LoopProgressCondition()
while let specifier = self.parseEffectsSpecifier(), loopProgress.evaluate(currentToken) {
specifiers.append(specifier)
}
return specifiers
}

/// Parse an accessor.
mutating func parseAccessorDecl() -> RawAccessorDeclSyntax {
let forcedHandle = TokenConsumptionHandle(tokenKind: .keyword(.get), missing: true)
Expand Down Expand Up @@ -1669,26 +1620,15 @@ extension Parser {
parameter = nil
}

// Next, parse effects specifiers. While it's only valid to have them
// on 'get' accessors, we also emit diagnostics if they show up on others.
let asyncKeyword: RawTokenSyntax?
let throwsKeyword: RawTokenSyntax?
if self.at(anyIn: EffectsSpecifier.self) != nil {
asyncKeyword = self.parseEffectsSpecifier()
throwsKeyword = self.parseEffectsSpecifier()
} else {
asyncKeyword = nil
throwsKeyword = nil
}
let effectSpecifiers = self.parseDeclEffectSpecifiers()

let body = self.parseOptionalCodeBlock()
return RawAccessorDeclSyntax(
attributes: introducer.attributes,
modifier: introducer.modifier,
accessorKind: introducer.token,
parameter: parameter,
asyncKeyword: asyncKeyword,
throwsKeyword: throwsKeyword,
effectSpecifiers: effectSpecifiers,
body: body,
arena: self.arena
)
Expand Down
Loading