From e0c1bb37a5b6313c44f9a14c2682671d1872dd75 Mon Sep 17 00:00:00 2001 From: Daniel Duan Date: Wed, 8 Jul 2026 22:38:13 -0700 Subject: [PATCH] Fix single value coding key context --- .../TOMLSingleValueDecodingContainer.Generated.swift | 11 ++++++++++- .../gyb/TOMLSingleValueDecodingContainer.swift.gyb | 11 ++++++++++- Tests/TOMLDecoderTests/TOMLDecoderTests.swift | 10 ++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Sources/TOMLDecoder/TOMLSingleValueDecodingContainer.Generated.swift b/Sources/TOMLDecoder/TOMLSingleValueDecodingContainer.Generated.swift index c4edc9da..9b0e765a 100644 --- a/Sources/TOMLDecoder/TOMLSingleValueDecodingContainer.Generated.swift +++ b/Sources/TOMLDecoder/TOMLSingleValueDecodingContainer.Generated.swift @@ -5,7 +5,16 @@ import Foundation extension _TOMLDecoder: SingleValueDecodingContainer { var context: TOMLKey { - codingPath.last as! TOMLKey + guard let key = codingPath.last else { + return TOMLKey.super + } + if let key = key as? TOMLKey { + return key + } + if let intValue = key.intValue { + return TOMLKey(intValue: intValue) + } + return TOMLKey(stringValue: key.stringValue) } @inline(__always) diff --git a/Sources/TOMLDecoder/gyb/TOMLSingleValueDecodingContainer.swift.gyb b/Sources/TOMLDecoder/gyb/TOMLSingleValueDecodingContainer.swift.gyb index 605a38c7..88010b7a 100644 --- a/Sources/TOMLDecoder/gyb/TOMLSingleValueDecodingContainer.swift.gyb +++ b/Sources/TOMLDecoder/gyb/TOMLSingleValueDecodingContainer.swift.gyb @@ -17,7 +17,16 @@ import Foundation extension _TOMLDecoder: SingleValueDecodingContainer { var context: TOMLKey { - codingPath.last as! TOMLKey + guard let key = codingPath.last else { + return TOMLKey.super + } + if let key = key as? TOMLKey { + return key + } + if let intValue = key.intValue { + return TOMLKey(intValue: intValue) + } + return TOMLKey(stringValue: key.stringValue) } % for native_type in native_types: diff --git a/Tests/TOMLDecoderTests/TOMLDecoderTests.swift b/Tests/TOMLDecoderTests/TOMLDecoderTests.swift index 55663ce4..329d80e1 100644 --- a/Tests/TOMLDecoderTests/TOMLDecoderTests.swift +++ b/Tests/TOMLDecoderTests/TOMLDecoderTests.swift @@ -144,6 +144,16 @@ struct TOMLDecoderTests { } } + @Test func optionalScalarArrayMismatchThrowsWithoutTrapping() throws { + struct Probe: Decodable { + let b: Bool? + } + + #expect(throws: (any Error).self) { + try TOMLDecoder().decode(Probe.self, from: "b = [1, 2, 3]\n") + } + } + @Test func foundationDateDecoding() throws { struct Player: Codable, Equatable {