Skip to content
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

swift-format fails to parse valid swift code #2648

Closed
rauhul opened this issue May 8, 2024 · 4 comments · Fixed by #2660
Closed

swift-format fails to parse valid swift code #2648

rauhul opened this issue May 8, 2024 · 4 comments · Fixed by #2660

Comments

@rauhul
Copy link
Contributor

rauhul commented May 8, 2024

The following snippet fails to parse in swift-format but swiftc is able to compile it:

func foo<E: Error, T>(a: T, b: T, body: (T, T) throws(E) -> T) throws(E) -> T {
  try body(a, b)
}

struct S: Error { }

let x = try foo(a: 0, b: 1) { a, b throws(S) in 2 }
print(x)

repro:

# compile
$ xcrun -toolchain org.swift.59202405011a swiftc test.swift
$ ./test
2

# format
$ xcrun -toolchain org.swift.59202405011a swift-format format test.swift
/Users/rauhul/Desktop/test.swift:7:32: error: unexpected code ', b throws(S) in 2' in closure
@rauhul rauhul added the bug Something isn't working label May 8, 2024
@ahoppen
Copy link
Collaborator

ahoppen commented May 8, 2024

Synced to Apple’s issue tracker as rdar://127750606

@allevato
Copy link
Collaborator

allevato commented May 8, 2024

This code is using typed throws, which isn't supported by any released Xcode compiler that I'm aware of (it's still an experimental feature last time I checked). Do the open-source builds enable it by default?

Since we just use swift-syntax to parse, the error is coming out of there. We might need to provide a command line flag to pass experimental features through to swift-syntax's parser if we want to support pre-release syntax.

@bnbarham
Copy link
Contributor

bnbarham commented May 8, 2024

Typed throws isn't experimental in main or 6.0, I assume this is just a swift-syntax parsing bug - will move to there.

@bnbarham bnbarham removed the bug Something isn't working label May 8, 2024
@bnbarham bnbarham transferred this issue from apple/swift-format May 8, 2024
bnbarham added a commit to bnbarham/swift-syntax that referenced this issue May 18, 2024
Lookahead was not consuming typed throw's error argument during
lookahead for closures. `atFunctionTypeArrow` did mostly handle it, but
only consumed a single token and thus wouldn't match `throws(any
Error)`. Handle this in `consumeEffectsSpecifiers` and then use this for
both.

Fixes apple#2648.
Resolves rdar://127750606.
bnbarham added a commit to bnbarham/swift-syntax that referenced this issue May 21, 2024
Lookahead was not consuming typed throw's error argument during
lookahead for closures. `atFunctionTypeArrow` did mostly handle it, but
only consumed a single token and thus wouldn't match `throws(any
Error)`. Handle this in `consumeEffectsSpecifiers` and then use this for
both.

Fixes apple#2648.
Resolves rdar://127750606.

(cherry picked from commit 5887f40)
@rauhul
Copy link
Contributor Author

rauhul commented May 21, 2024

Thanks for fixing this!

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 a pull request may close this issue.

4 participants