Skip to content

Accessor macro on subscript cannot find instance members #70368

Open
@hamishknight

Description

@hamishknight

Description

No response

Reproduction

For e.g:

import SwiftSyntax
import SwiftSyntaxMacros

public struct SetterMacro: AccessorMacro, Macro {
  public static func expansion(
    of node: AttributeSyntax,
    providingAccessorsOf declaration: some DeclSyntaxProtocol,
    in context: some MacroExpansionContext
  ) throws -> [AccessorDeclSyntax] {
    return [
      """
        set {
          _storage = newValue
        }
      """,
    ]
  }
}

and:

@attached(accessor)
macro setterMacro() =
    #externalMacro(module: "MacroDefinition", type: "SetterMacro")

struct S {
  var _storage = 0

  @setterMacro
  var x: Int {
    get { _storage }
  }

  @setterMacro
  subscript() -> Int {
    get { _storage }
  }
}

you get the error for its use with the subscript:

@__swiftmacro_4main1SV9subscript11setterMacrofMa_.swift:2:9: error: instance member '_storage' cannot be used on type 'S'; did you mean to use a value of this type instead? [instance_member_use_on_type]
        _storage = newValue
        ^~~~~~~~
main.swift:145:3: note: in expansion of macro 'setterMacro' on subscript 'subscript()' here [in_macro_expansion]
  @setterMacro
  ^~~~~~~~~~~~

Expected behavior

Given this works for the computed property, it seems like this ought to work for the subscript too.

Environment

Swift version 5.11-dev (LLVM 572a5a4fbafb69e, Swift 484fc77)
Target: arm64-apple-macosx14.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfname lookupArea → compiler → type checker: Name lookupswift macroFeature → declarations: Swift `macro` declarations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions