@@ -125,16 +125,14 @@ func replaceTypeName(_ type: TypeSyntax, _ name: TokenSyntax) -> TypeSyntax {
125
125
return TypeSyntax ( idType. with ( \. name, name) )
126
126
}
127
127
128
- func getPointerMutability( text: String ) -> Mutability {
128
+ func getPointerMutability( text: String ) -> Mutability ? {
129
129
switch text {
130
130
case " UnsafePointer " : return . Immutable
131
131
case " UnsafeMutablePointer " : return . Mutable
132
132
case " UnsafeRawPointer " : return . Immutable
133
133
case " UnsafeMutableRawPointer " : return . Mutable
134
134
default :
135
- throw DiagnosticError (
136
- " expected Unsafe[Mutable][Raw]Pointer type for type \( prev) "
137
- + " - first type token is ' \( text) ' " , node: name)
135
+ return nil
138
136
}
139
137
}
140
138
@@ -166,8 +164,11 @@ func transformType(_ prev: TypeSyntax, _ variant: Variant, _ isSizedBy: Bool) th
166
164
throw DiagnosticError ( " raw pointers only supported for SizedBy " , node: name)
167
165
}
168
166
169
- let kind : Mutability =
170
- getPointerMutability ( text: text)
167
+ guard let kind: Mutability = getPointerMutability ( text: text) else {
168
+ throw DiagnosticError (
169
+ " expected Unsafe[Mutable][Raw]Pointer type for type \( prev) "
170
+ + " - first type token is ' \( text) ' " , node: name)
171
+ }
171
172
let token = getSafePointerName ( mut: kind, generateSpan: variant. generateSpan, isRaw: isSizedBy)
172
173
if isSizedBy {
173
174
return TypeSyntax ( IdentifierTypeSyntax ( name: token) )
@@ -543,7 +544,7 @@ public struct PointerBoundsMacro: PeerMacro {
543
544
let endParamIndexArg = try getArgumentByName ( argumentList, " end " )
544
545
let endParamIndex : Int = try getIntLiteralValue ( endParamIndexArg)
545
546
let nonescapingExprArg = getOptionalArgumentByName ( argumentList, " nonescaping " )
546
- let nonescaping = nonescapingExprArg != nil && try getBoolLiteralValue ( nonescapingExprArg!)
547
+ let nonescaping = try nonescapingExprArg != nil && getBoolLiteralValue ( nonescapingExprArg!)
547
548
return EndedBy (
548
549
pointerIndex: startParamIndex, endIndex: endParamIndex, nonescaping: nonescaping,
549
550
original: ExprSyntax ( enumConstructorExpr) )
@@ -615,10 +616,9 @@ public struct PointerBoundsMacro: PeerMacro {
615
616
let i = pointerArg. pointerIndex
616
617
if i < 1 || i > paramCount {
617
618
let noteMessage =
618
- paramCount > 0 ?
619
- " function \( funcDecl. name) has parameter indices 1.. \( paramCount) "
620
- :
621
- " function \( funcDecl. name) has no parameters "
619
+ paramCount > 0
620
+ ? " function \( funcDecl. name) has parameter indices 1.. \( paramCount) "
621
+ : " function \( funcDecl. name) has no parameters "
622
622
throw DiagnosticError (
623
623
" pointer index out of bounds " , node: pointerArg. original,
624
624
notes: [
@@ -670,12 +670,11 @@ public struct PointerBoundsMacro: PeerMacro {
670
670
} )
671
671
let newSignature = try builder. buildFunctionSignature ( [ : ] , variant)
672
672
let checks =
673
- variant. skipTrivialCount ?
674
- [ ] as [ CodeBlockItemSyntax ]
675
- :
676
- try builder. buildBoundsChecks ( variant) . map { e in
677
- CodeBlockItemSyntax ( leadingTrivia: " \n " , item: e)
678
- }
673
+ variant. skipTrivialCount
674
+ ? [ ] as [ CodeBlockItemSyntax ]
675
+ : try builder. buildBoundsChecks ( variant) . map { e in
676
+ CodeBlockItemSyntax ( leadingTrivia: " \n " , item: e)
677
+ }
679
678
let call = CodeBlockItemSyntax (
680
679
item: CodeBlockItemSyntax . Item (
681
680
ReturnStmtSyntax (
@@ -757,4 +756,4 @@ extension TypeSyntaxProtocol {
757
756
758
757
return false
759
758
}
760
- }
759
+ }
0 commit comments