TipKit @Parameter macro is very fragile, cannot do type inference and handle // comment

I have this:

import Foundation
import TipKit


struct CompletionToDeleteTip: Tip {
  @Parameter
  static var reachedThresholdParameter: Bool = false

  var title: Text {
    Text("Time to clean up")
  }
}

If I remove ": Bool" type annotation and try to use type inference, get "Unable to expand due to missing identifier pattern.":

  @Parameter
  static var reachedThresholdParameter = false

If I do this (add a trailing comment):

  @Parameter
  static var reachedThresholdParameter: Bool = false    // something something

the macro expansion failed to compile: "Expected ')' in expression list": it puts the comment in the middle of a function call in the expansion. Change the comment to /* ... */ fix the problem. But the macro should properly transform legal input and not do silly thing? Perhaps can it remove all comments or change // to /* ... */

Edit: later on even the /* … */ cause compile error. So there cannot be any comment at the macro call site.

It cannot use type inference and it include // comment in the expansion.

Can macro like @Parameter be improved, or is this the limitation of Swift macro?

1 Like

It's a limitation of macros, they're applied before type inference is completed, so you have to use explicit types.

2 Likes

I believe that trailing comments causing macro expansion to result in an infinite loop is a bug.