Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular reference resolving attached macro 'xxx' #70096

Closed
ssly1997 opened this issue Nov 29, 2023 · 4 comments
Closed

Circular reference resolving attached macro 'xxx' #70096

ssly1997 opened this issue Nov 29, 2023 · 4 comments
Labels
swift macro Feature → declarations: Swift `macro` declarations

Comments

@ssly1997
Copy link

Description

When defining a new type within a structure, the macro throws an error 'Circular reference resolving attached macro 'xxx'

@entity // ERROR: Circular reference resolving attached macro 'entity'
struct NewDBTest { // ERROR: Circular reference
  let a: String
}

extension NewDBTest {
  struct Test { }
}

extension NewDBTest.Test { // ERROR: 'Test' is not a member type of struct 'IcarusClient.NewDBTest'
  
}

@entity is a member macro of a generating constructor

public struct EntityMacro: MemberMacro {
  public static func expansion(
    of node: AttributeSyntax,
    providingMembersOf declaration: some DeclGroupSyntax,
    in context: some MacroExpansionContext
  ) throws -> [DeclSyntax] {
    guard let declaration = declaration.as(StructDeclSyntax.self)
    else { throw IcarusTrackerError.invalidInputType }
    
    let arguments = declaration.storedProperties()
      .filter { !$0.isStatic && !$0.isConstant }
      .compactMap { property -> (name: String, type: TypeSyntax)? in
        guard let name = property.name, let type = property.type
        else { return nil }
        return (name, type)
      }
    
    let initDecl = try InitializerDeclSyntax("public init(\(raw: arguments.map { "\($0.name): \($0.type)" }.joined(separator: ", ")))") {
      for argument in arguments {
        ExprSyntax(stringLiteral: "self.\(argument.name) = \(argument.name)")
      }
    }
    
    return [
      DeclSyntax(initDecl)
    ]
  }
}

image

Steps to Reproduce

No response

@ssly1997 ssly1997 added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Nov 29, 2023
@ssly1997
Copy link
Author

This error occurs only when writing extensions for this new type

@ahoppen
Copy link
Contributor

ahoppen commented Nov 29, 2023

Tracked in Apple’s issue tracker as rdar://118929243

@ahoppen ahoppen removed the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Nov 29, 2023
@ahoppen ahoppen transferred this issue from apple/swift-syntax Nov 29, 2023
@ahoppen ahoppen added the swift macro Feature → declarations: Swift `macro` declarations label Nov 29, 2023
@ynnadrules
Copy link

Related: #66450

@hborla
Copy link
Member

hborla commented Feb 6, 2024

This is fixed by #71365

@hborla hborla closed this as completed Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift macro Feature → declarations: Swift `macro` declarations
Projects
None yet
Development

No branches or pull requests

4 participants