swift-syntax icon indicating copy to clipboard operation
swift-syntax copied to clipboard

[AccessorMacro] 'let' declarations cannot be computed properties

Open vanvoorden opened this issue 1 year ago • 7 comments

Description

The following Swift code fails to compile:

let x: Int {  //  'let' declarations cannot be computed properties
  1
}

It looks like our current AccessorMacro implementation adds computed getters to let variables without transforming the let to var:

extension AccessorMacroTests {
  func testLetDeclarations() {
    assertMacroExpansion(
      """
      @constantOne
      let x: Int = 1
      """,
      expandedSource: """
        var x: Int {
          get {
            return 1
          }
        }
        """,
      macros: ["constantOne": ConstantOneGetter.self],
      indentationWidth: indentationWidth
    )

    // Bit of an odd case, compiler has the type but we don't know it in `MacroSystem`
    assertMacroExpansion(
      """
      @constantOne
      let x = 1
      """,
      expandedSource: """
        var x {
          get {
            return 1
          }
        }
        """,
      macros: ["constantOne": ConstantOneGetter.self],
      indentationWidth: indentationWidth
    )
  }
}

This test fails:

error: -[SwiftSyntaxMacroExpansionTest.AccessorMacroTests testLetDeclarations] : failed - Macro expansion did not produce the expected expanded source
–var x: Int {
+let x: Int {
   get {
     return 1
   }
 }

Actual expanded source:

let x: Int {
  get {
    return 1
  }
}
error: -[SwiftSyntaxMacroExpansionTest.AccessorMacroTests testLetDeclarations] : failed - Macro expansion did not produce the expected expanded source
–var x {
+let x {
   get {
     return 1
   }
 }

Actual expanded source:

let x {
  get {
    return 1
  }
}

Should we update the AccessorMacro implementation to produce Swift more consistent with what engineers are able to compile? Should we be transforming that let to a var?

Steps to Reproduce

No response

vanvoorden avatar Feb 10 '24 03:02 vanvoorden