Update Swift template to silence warnings about unmodified class reference #3387
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When generating language parsers in Swift, the resulting code triggers some warnings due to a variable not being modified in the generated function bodies.
Example generated code:
Compiler warning
Solution
All possible types asignable to `_localctx` are classes that inherit from [`RuleContext`](https://github.com/antlr/antlr4/blob/7da07c73a3dc40d4ed24d90c6ac52702050295b1/runtime/Swift/Sources/Antlr4/RuleContext.swift#L58), which conforms to protocol [`Tree`](https://github.com/antlr/antlr4/blob/7da07c73a3dc40d4ed24d90c6ac52702050295b1/runtime/Swift/Sources/Antlr4/tree/Tree.swift#L11). This protocol conforms to `class`/`AnyObject` which requires any conforming types to be Swift classes (reference types). Because of this, it is safe to use `let` instead of `var` in the generated code.Class/protocol hierarchy:
ParserRuleContext
->RuleContext
->RuleNode
->ParseTree
->SyntaxTree
->Tree
->AnyObject
This pull requests updates the Swift template to use
let
instead ofvar
, in order to silence this warning.Swift template has been updated to break this statements into two. By doing this, we prevent the Swift compiler from raising warnings when the variable is not modified in the generated function body.
The code from the above example would now look like this: