Skip to content

Commit

Permalink
Merge pull request #3047 from antlr/fix-swift-crasher
Browse files Browse the repository at this point in the history
work around Swift crash where reading hashValue on SemanticContext.AND
(merged by @ericvergnaud  since @ewanmellor is not able to look into it)
  • Loading branch information
ericvergnaud authored Jan 24, 2021
2 parents b050ac4 + 020333d commit 8227df1
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions runtime/Swift/Sources/Antlr4/atn/LookupDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,27 @@ public struct LookupDictionary {

private func hash(_ config: ATNConfig) -> Int {
if type == LookupDictionaryType.lookup {

var hashCode: Int = 7
hashCode = 31 * hashCode + config.state.stateNumber
hashCode = 31 * hashCode + config.alt
hashCode = 31 * hashCode + config.semanticContext.hashValue
return hashCode

/* migrating to XCode 12.3/Swift 5.3 introduced a very weird bug
where reading hashValue from a SemanticContext.AND instance woul:
call the AND empty constructor
NOT call AND.hash(into)
Could it be a Swift compiler bug ?
All tests pass when using Hasher.combine()
Keeping the old code for reference:
var hashCode: Int = 7
hashCode = 31 * hashCode + config.state.stateNumber
hashCode = 31 * hashCode + config.alt
hashCode = 31 * hashCode + config.semanticContext.hashValue // <- the crash would occur here
return hashCode
*/
var hasher = Hasher()
hasher.combine(7)
hasher.combine(config.state.stateNumber)
hasher.combine(config.alt)
hasher.combine(config.semanticContext)
return hasher.finalize()
} else {
//Ordered
return config.hashValue
Expand Down

0 comments on commit 8227df1

Please sign in to comment.