Skip to content

[Parse] Minor code improvement in function-type parsing #5721

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 3 commits into from
Jan 13, 2017

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Nov 11, 2016

  • Eliminate backtracking.
  • Code cleanup.

In addition:

  • Add fix-it for rethrows in function type.

(Tok.is(tok::kw_throw) && peekToken().is(tok::arrow))) {
if (Tok.is(tok::kw_throw)) {
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows, tok::kw_throw) &&
peekToken().is(tok::arrow)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is definitely a behavior change for the recovery in a partial line:

let fn: () throws

Can you add tests for this case, and see if it's a net benefit?

Copy link
Member Author

@rintaro rintaro Nov 11, 2016

Choose a reason for hiding this comment

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

I'm not sure. What you mention is Don't consume 'throws', if next token is not '->'?
Swift 3.0.1 and this PR both emits:

test.swift:1:11: error: consecutive statements on a line must be separated by ';'
let fn: () throws
          ^
          ;
test.swift:1:12: error: expected expression
let fn: () throws
           ^

Actually, this behavior is from the original code:
https://github.com/apple/swift/blob/71364d0d/lib/Parse/ParseType.cpp#L222-L224

@rintaro
Copy link
Member Author

rintaro commented Nov 14, 2016

@jrose-apple Anyway, I agree about bad recovery here.
But for now, I don't have good idea to fix that
(while keeping behavior for rdar://problem/20857518 6951e46)

Added the test case with FIXME.

"only function declarations may be marked 'rethrows'", ())
ERROR(rethrowing_function_type,none,
"only function declarations may be marked 'rethrows'; "
"did you mean 'throws'?", ())
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't make sense to me. The fact that rethrows isn't allowed in a function type (which I didn't realize until seeing this PR) seems like an unnecessary and surprising limitation. I would guess users will expect to be able to write rethrows function types, so suggesting did you mean 'throws'? will just be confusing.

Copy link
Member Author

@rintaro rintaro Nov 22, 2016

Choose a reason for hiding this comment

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

Hmm. Maybe, rejecting rethrows is just a bug.

From the language reference

Function types that can throw an error must be marked with the throws keyword, and function types that can rethrow an error must be marked with the rethrows keyword.

GRAMMAR OF A FUNCTION TYPE

function-typeattributes­opt ­function-type-argument-clause ­throws ­opt­ ->­ type­
function-typeattributes­opt ­function-type-argument-clause ­rethrows ­-> ­type­

Copy link
Contributor

Choose a reason for hiding this comment

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

Summoning @rjmccall. A rethrows function type is useful, but it also makes the language more complicated, and I thought we decided not to do it.

@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

@slavapestov slavapestov self-assigned this Jan 9, 2017
@slavapestov
Copy link
Contributor

@rintaro Can you please rebase?

@jrose-apple
Copy link
Contributor

For the record, this was waiting on comments from @rjmccall about rethrows.

@slavapestov
Copy link
Contributor

@jrose-apple I pinged John about this but haven't got a response yet. Since this patch is not changing the behavior of anything, can we go ahead and merge it?

@jrose-apple
Copy link
Contributor

Fair point, sure.

@slavapestov slavapestov merged commit 48ef1ad into swiftlang:master Jan 13, 2017
@rjmccall
Copy link
Contributor

That was definitely not a sneaky attempt to add rethrows function types to the language. That would be a major addition.

@rintaro
Copy link
Member Author

rintaro commented Jan 16, 2017

Sorry for delayed response, I was on vacation.
Thank you for merging this.

So, should I file a radar about "GRAMMAR OF A FUNCTION TYPE" documentation issue?

@rintaro rintaro deleted the parse-functy branch January 16, 2017 03:33
@jrose-apple
Copy link
Contributor

Seems like a good idea to me. It's not a critical issue, since the grammar generally allows more productions than the language, but it seems easy enough for the Apple DevPubs folks to just delete that line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants