Skip to content

feat: add arbitrary-length #magic macro using InlineArray - #39

Closed
kkaz79 wants to merge 3 commits into
apple:mainfrom
kkaz79:main
Closed

feat: add arbitrary-length #magic macro using InlineArray#39
kkaz79 wants to merge 3 commits into
apple:mainfrom
kkaz79:main

Conversation

@kkaz79

@kkaz79 kkaz79 commented Sep 25, 2025

Copy link
Copy Markdown
Contributor
  • 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

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've run Scripts/format.sh to correctly format my change
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

  - 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 natecook1000 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])"
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@kkaz79 kkaz79 closed this Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants