mirror of
https://github.com/krzysztofzablocki/Sourcery.git
synced 2026-04-07 19:17:40 +00:00
786f40feb9
Root cause:
- A trailing comma inside a generic argument list can produce an empty generic argument, for example:
`OrderedDictionary<String, (status: Result<Int, Error>, messages: [String]),>`
- During composition, `ParserResultsComposed.resolveType` force-unwrapped
`typeName.name.split(separator: " ").first!` when generic requirements were present.
- If the generic parameter was empty, this force unwrap crashed with
`Unexpectedly found nil while unwrapping an Optional value`.
What changed:
- Hardened `ParserResultsComposed.resolveType` to skip generic-requirement lookup
when the lookup token is empty (no force unwrap).
- Filtered out empty/whitespace generic arguments in SwiftSyntax parsing
(`GenericType+SwiftSyntax`).
- Filtered out empty/whitespace generic arguments in string-based type inference
(`String+TypeInference`).
- Added a regression spec in `ComposerSpec` that covers a constrained generic type
containing a trailing comma in a generic argument list and asserts only two valid
generic parameters are produced.
Repro script:
```bash
cat > /tmp/Repro.swift <<'SWIFT'
struct Example<A: RandomAccessCollection, B: RandomAccessCollection>
where A.Element == String,
B.Element == String
{
let asyncTaskData: OrderedDictionary<
String,
(status: Result<Int, Error>, messages: [String]),
>
}
SWIFT
cat > /tmp/inspect.stencil <<'STENCIL'
{% for type in types.structs %}
{% for variable in type.variables %}
{% if variable.name == "asyncTaskData" and variable.typeName.generic %}
GENERIC_COUNT={{ variable.typeName.generic.typeParameters.count }}
{% for p in variable.typeName.generic.typeParameters %}
PARAM={{ p.typeName.name }}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
STENCIL
# Before this fix: crashes with Trace/BPT trap.
# After this fix:
# GENERIC_COUNT=2
# PARAM=String
# PARAM=(status: Result<Int, Error>, messages: [String])
sourcery --disableCache --sources /tmp/Repro.swift --templates /tmp/inspect.stencil --output /tmp/out.txt
cat /tmp/out.txt
```