swiftlang / swift-format

Formatting technology for Swift source code
Apache License 2.0
2.44k stars 222 forks source link

Formatted code can be invalid Swift syntax #439

Open mbrandonw opened 1 year ago

mbrandonw commented 1 year ago

The following valid Swift code:

import SwiftUI
func foo() {
  @Environment(\.colorScheme) var colorScheme;
  print(colorScheme)
}

…gets formatted like this (notice the semi-colon is removed):

import SwiftUI
func foo() {
  @Environment(\.colorScheme) var colorScheme
  print(colorScheme)
}

This code is not valid Swift code. I'm not sure if this is a bug in the Swift parser, or perhaps this code cannot be unambiguously parsed (I filed a bug).

Should semi-colons be left in in order to work around this issue?

allevato commented 1 year ago

I'm definitely interested in knowing if this is intended behavior or not for the parser in general; thanks for filing the apple/swift issue.

But if the Swift compiler today requires the semicolon, then we'll have to make an exception to not remove it in that case.

Does this only happen for local variables with a property wrapper and without an explicit type annotation or initializer? Are there other cases that break in the same way?

mbrandonw commented 1 year ago

Does this only happen for local variables with a property wrapper and without an explicit type annotation or initializer? Are there other cases that break in the same way?

Local variables with an inferred type is the only way we've come across it so far. Supplying an explicit type is the workaround we use when we come across this bug on CI.

mbrandonw commented 1 year ago

@allevato This may have been fixed a little over a month ago (https://github.com/apple/swift/pull/61036), so maybe this won't be an issue in the next Swift release.

ahoppen commented 4 months ago

Tracked in Apple’s issue tracker as rdar://126948232