Comments (17)
@soranoba OK cool, it seems to be the same issue as @vgorloff's. Basically it happens if Self
is used inside let
or if/guard let
expressions.
from swiftformat.
Version 0.52.2 fixes this issue in my project. Thank you! 👍
from swiftformat.
Version 0.52.2 fixes my issue too. Thanks!!
from swiftformat.
@lhunath I think this is already fixed in 0.52.1
from swiftformat.
oh - I'll try again when brew
updates.
from swiftformat.
Hi, 0.52.1 is broken.
1> class A { static let defaultName = "A"; let name: String; init() { name = Self.defaultName } }
2> class A { static let defaultName = "A"; let name: String; init() { name = defaultName } }
expression failed to parse:
error: repl.swift:2:75: error: static member 'defaultName' cannot be used on instance of type 'A'
class A { static let defaultName = "A"; let name: String; init() { name = defaultName } }
^~~~~~~~~~~
A.
0.52.1 will format (1) to (2), so it failed to compile.
from swiftformat.
I'm seeing this too in our project (0.52.1 removing Self.
where it shouldn't), but I'm struggling to reproduce in a simple out-of-project file.
Will keep you updated if I can repro.
from swiftformat.
+1 on our side.
Formatting causes removal of the Self
in expression guard let value = Self.location(for: warnRegion) ...
which causes compile error.
Here is our code before formatting:
public class LocationDeeplink: Deeplink {
convenience init?(warnRegion: String) {
guard let value = Self.location(for: warnRegion) else {
return nil
}
self.init(location: value)
}
}
extension LocationDeeplink {
private static func location(for warnRegion: String) -> WCOMLocation? {
return ...
}
}
from swiftformat.
@soranoba I wasn't able to reproduce your issue with 0.52.1, but I was able to reproduce it with 0.52.0. Are you sure you're on the latest? Run swiftformat --version
if you aren't sure.
from swiftformat.
@vgorloff this is a different, but related bug. I'll fix it for the next release.
from swiftformat.
@guillaumealgis if you are able to repro, please open a separate ticket. If you are able to share the entire file or some significant chunk of it, that will be fine - it doesn't need to compile.
from swiftformat.
I use 0.52.1, but it does not happend to bug with this code. Sorry...
Attached is the code from when it actually happened.
public struct Attributes: OptionSet, Decodable {
public let rawValue: UInt
public init(rawValue: UInt) {
self.rawValue = rawValue
}
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
var result: Attributes = []
while !container.isAtEnd {
let name = try container.decode(String.self)
if let type = Self.mapping[name] { // <--- here
result.insert(type)
}
}
self = result
}
private static let mapping: [String: Attributes] = [
]
}
from swiftformat.
And it reproduced. The if statement may be suspicious.
public class Store {
private static let kUserName: String? = "USER_NAME"
@objc lazy var userName: String = {
if let name = Self.kUserName { // <---- here
return name
}
return ""
}
}
from swiftformat.
I also have a few cases where Self
is removed from my code. Here is an example:
protocol ContentDetectionConfiguration {
static var contentTypes: [ContentType] { get }
}
struct ContentDetectionConfigurationDelegate: ContentDetectionConfiguration {
private static var configurator = ContentTypeConfigurator()
func isEnabled(at index: Int) -> Bool {
let contentType = Self.contentTypes[index] // <-- here
return Self.configurator.isEnabled(contentType: contentType)
}
}
This only happens if I specify a Swift version, e.g. swiftformat --swiftversion 5.8 file.swift
swiftformat --version
0.52.1
from swiftformat.
Version 0.52.2 fixes this issue in our project. Thank you! 👍
from swiftformat.
Still have a few cases creating issues on 0.52.2.
Made a small example to showcase it, where we use swift-composable-architecture
and xctest-dynamic-overlay
.
public func XCTUnimplemented<Result>(
_ path: KeyPath<some Any, some Any>,
file: StaticString = #file,
fileID: StaticString = #fileID,
line: UInt = #line
) -> @Sendable () -> Result {
return { fatalError("don't care") }
}
public struct Environment {
let test: () -> Void
init(test: @escaping () -> Void) {
self.test = test
}
static var unimplemented: Self {
.init(test: XCTUnimplemented(\Self.test)) // this `Self` is removed by SwiftFormat, but is necessary
}
}
from swiftformat.
Hi,
We are also seeing few extra cases where Self
is still in 0.5.2 being removed incorrectly:
class A {
static var p: Int = 0
}
class B: A {
var test: Int {
return Self.p.bitWidth // Still being removed
}
}
from swiftformat.
Related Issues (20)
- enumNamespaces overwrite imports HOT 5
- conditionalAssignment bug with compiler directives (#if) HOT 3
- Parsing error with raw strings inside tuples HOT 2
- Feature request: rule to convert if statements to guard where appropriate
- Hidden protocol requirements of DistributedActorSystem HOT 5
- How can I begin to diagnose slow swift format runs? HOT 5
- Unexpected static keyword HOT 2
- genericExtensions has no effect for me HOT 5
- Swift Package Manager Xcode Command Plugin does not works on 0.52.9 and later HOT 10
- add `length then alpha` option to `--sortImports` HOT 1
- Computed property `set` is being formatted to `self.set` HOT 2
- Inheriting rules in a subdirectory HOT 2
- Error with conditionalAssignment HOT 3
- Reindent comments HOT 1
- contribution questions HOT 2
- Private Properties At The Top Of Class Definition HOT 1
- Extension of Internal struct cannot be public HOT 3
- `preferForLoop` rule causes build error HOT 4
- Duplicate header generated HOT 1
- Question about Wrap HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftformat.