feat: add arbitrary-length #magic macro using InlineArray - #39
Closed
kkaz79 wants to merge 3 commits into
Closed
Conversation
- Add new #magic macro supporting ASCII strings of any length - Extends beyond #magicNumber's 2/4/8 byte limitation - Uses InlineArray<N, UInt8> for compile-time optimization - Add _loadAndCheckInlineArrayBytes helper function - Add InlineArray Equatable conformance for UInt8 elements - Include comprehensive tests: macro expansion + end-to-end runtime - Maintains backward compatibility with existing #magicNumber
natecook1000
reviewed
Jan 8, 2026
natecook1000
left a comment
Member
There was a problem hiding this comment.
Apologies for the delay, @kkazuha7! A couple of minor requests below.
Comment on lines
+30
to
+44
| @available(macOS 26, iOS 26, watchOS 26, tvOS 26, visionOS 26, *) | ||
| extension InlineArray: @retroactive Equatable where Element == UInt8 { | ||
| @inlinable | ||
| public static func == ( | ||
| lhs: InlineArray<count, UInt8>, rhs: InlineArray<count, UInt8> | ||
| ) -> Bool { | ||
| for i in 0..<count { | ||
| if lhs[i] != rhs[i] { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
We don't want to add this conformance here, as it can cause problems if InlineArray actually gains its own Equatable conformance in the future. Let's just have an internal isEqual(to:) method to handle this kind of comparison.
Comment on lines
+51
to
+56
| // Note: \n is treated as literal backslash + n characters, not a newline | ||
| assertMacro { | ||
| #"try #magic("hello\nworld", parsing: &data)"# | ||
| } expansion: { | ||
| "try _loadAndCheckInlineArrayBytes(parsing: &data, expectedBytes: [104, 101, 108, 108, 111, 92, 110, 119, 111, 114, 108, 100])" | ||
| } |
Member
There was a problem hiding this comment.
I think this would be very surprising behavior for users. Instead of using the literal backslash, what should the compiler response be? Should we ban backslashes?
Also, how does the macro respond to a string literal like #"hello"world"#? Do interpolations get rejected?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Checklist
Scripts/format.shto correctly format my change