nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.97k stars 638 forks source link

[Crash] Class override causes illegal hardware instruction #832

Closed victorBaro closed 3 weeks ago

victorBaro commented 3 years ago

When running swiftformat on files containing the following code, swift format crashes producing 3483 illegal hardware instruction.

// Crash
class override var layerClass: AnyClass { /*...*/ }

// Fix
override class var layerClass: AnyClass {  /*...*/ }
nicklockwood commented 3 years ago

@victorBaro I wasn't able to reproduce this crash.

Are you running the latest version (0.47.8)? Can you share the rules/options you are using?

victorBaro commented 3 years ago

I am running the latest. A colleague using Catalina couldn't reproduce it either (not sure if that will make any difference, I am use MacOS Big Sur 11.0.1).

Rules:

# format options
--allman false
--indent 4

# file options
--exclude Pods

#disabled rules
--disable blankLinesAroundMark, redundantReturn, redundantrawvalues, wrapMultilineStatementBraces, preferkeypath, enumnamespaces

# rules
--binarygrouping none 
--decimalgrouping none 
--elseposition next-line 
--hexgrouping none 
--ifdef no-indent 
--nospaceoperators ...,..< 
--octalgrouping none 
--patternlet inline 
--self init-only
--semicolons never 
--stripunusedargs closure-only
--funcattributes prev-line
--typeattributes prev-line
--varattributes prev-line
--guardelse same-line
--wrapparameters after-first
--closingparen same-line
--xcodeindentation true
--wrapcollections before-first

also .swift-version is set to 5.3

nicklockwood commented 3 years ago

@victorBaro I wasn't able to repo on Big Sur. Maybe it depends on some other code in the same file? If you can provide a standalone source file that reproduces the issue consistently that would be a big help.

podkovyrin commented 3 years ago

I got the same crash recently and was able to reproduce it in the debugger. SwiftFormat commit is 4590ac0cc44fd8bcb5df1eb513909451b9b66015. macOS BigSur 11.1, Xcode 12.3 (12C33)

# format options

--allman false
--binarygrouping 4,8
--commas always
--comments indent
--decimalgrouping 3,6
--elseposition next-line
--empty void
--exponentcase lowercase
--exponentgrouping disabled
--fractiongrouping disabled
--header ignore
--hexgrouping 4,8
--hexliteralcase uppercase
--ifdef indent
--indent 4
--indentcase false
--importgrouping testable-bottom
--linebreaks lf
--octalgrouping 4,8
--operatorfunc spaced
--patternlet hoist
--ranges spaced
--self remove
--semicolons never
--trimwhitespace always
--wraparguments preserve
--wrapcollections preserve

# rules

--disable andOperator,unusedArguments,wrapMultilineStatementBraces
--enable isEmpty

File.swift

import Foundation

final class MyClass: BaseClass {
    var a: Int?

    class override var b: String { "c" }
}
Screenshot 2020-12-27 at 02 19 53

Btw, it worked fine for me a few SwiftFormat versions ago

nicklockwood commented 3 years ago

@podkovyrin thanks! that helped a lot.

nicklockwood commented 3 years ago

@victorBaro @podkovyrin fixed in 0.47.10

balavor commented 3 years ago

@nicklockwood

I was able to reproduce an issue in 0.47.10.

I was running a command in my terminal and I couldn't see any errors in the terminal, but eventually, I found out the code that breaks the entire swiftformat command without any logs:

@IBDesignable class GradientView: UIView {
  //...
  class override var layerClass: AnyClass {
    return CAGradientLayer.self
  }
  //...
}

The rewritten example below works fine:

@IBDesignable class GradientView: UIView {
  //...
  static override var layerClass: AnyClass {
    return CAGradientLayer.self
  }
  //...
}

My list of rules:

--symlinks ignore
--allman false
--binarygrouping none
--closingparen balanced
--conflictmarkers reject
--decimalgrouping 3,6
--elseposition same-line
--exponentgrouping disabled
--fractiongrouping disabled
--fragment false
--funcattributes prev-line
--hexgrouping 4,8
--hexliteralcase uppercase
--ifdef indent
--importgrouping alphabetized
--markextensions always
--nospaceoperators 
--nowrapoperators 
--octalgrouping 4,8
--operatorfunc spaced
--patternlet hoist
--specifierorder public, private(set), private, fileprivate, internal, open, static, class, convenience, override, lazy, weak
--swiftversion 5.3
--tabwidth unspecified
--trailingclosures 
--typeattributes prev-line
--xcodeindentation enabled
nicklockwood commented 3 years ago

@RomanTysiachnik using a file containing just the code you posted, and the options you specified, I wasn't able to reproduce the bug in 0.47.10.

balavor commented 3 years ago

Let me share an extended one... But if you won't catch it, I can't even imagine how to reproduce that. It doesn't block me at all, but it was such a pain in the... well... I don't want someone else to waste so much time in that case )

--symlinks ignore
--allman false
--binarygrouping none
--closingparen balanced
--commas inline
--comments indent
--conflictmarkers reject
--decimalgrouping 3,6
--elseposition same-line
--empty void
--exponentcase lowercase
--exponentgrouping disabled
--fractiongrouping disabled
--fragment false
--funcattributes prev-line
--hexgrouping 4,8
--hexliteralcase uppercase
--ifdef indent
--importgrouping alphabetized
--indent 2
--indentcase false
--linebreaks lf
--maxwidth 120
--markextensions always
--nospaceoperators 
--nowrapoperators 
--octalgrouping 4,8
--operatorfunc spaced
--patternlet hoist
--self remove
--selfrequired 
--semicolons inline
--specifierorder public, private(set), private, fileprivate, internal, open, static, class, convenience, override, lazy, weak
--stripunusedargs always
--swiftversion 5.3
--tabwidth unspecified
--trailingclosures 
--trimwhitespace nonblank-lines
--typeattributes prev-line
--wraparguments before-first
--wrapparameters before-first
--wrapcollections before-first
--wraparguments before-first
--xcodeindentation enabled

# Make sure those rules are enabled

--enable anyObjectProtocol
--enable duplicateImports
--enable blankLinesBetweenScopes
--enable consecutiveBlankLines
--enable consecutiveSpaces
--enable emptyBraces
--enable isEmpty
--enable preferKeyPath
--enable redundantBreak
--enable redundantExtensionACL
--enable redundantGet
--enable redundantLet
--enable redundantLetError
--enable redundantInit 
--enable redundantNilInit
--enable redundantParens 
--enable redundantPattern
--enable redundantRawValues
--enable redundantReturn 
--enable redundantVoidReturnType
--enable sortedImports
--enable spaceAroundBraces 
--enable spaceAroundBrackets 
--enable spaceAroundGenerics
--enable spaceAroundOperators 
--enable spaceInsideBraces
--enable spaceInsideGenerics
--enable strongOutlets
--enable todos
--enable trailingCommas
--enable trailingSpace
--enable typeSugar
--enable unusedArguments
--enable void
--enable redundantFileprivate
nicklockwood commented 3 years ago

@RomanTysiachnik that still isn't reproducing for me. I suspect the issue is not the rules/options, but the code itself. Are you able to reproduce the issue with a source file that only contains the code you posted?

balavor commented 3 years ago

@nicklockwood yeah, my bad. I've found what causing a crash, but not entirely... Here you go:

@IBDesignable
class GradientView: UIView {
  @IBInspectable var angle: CGFloat = 0 {
    didSet { setNeedsLayout() }
  }

  class override var layerClass: AnyClass {
    CAGradientLayer.self
  }
  //...
}
nicklockwood commented 3 years ago

@RomanTysiachnik this also doesn't crash for me.