Skip to content

Circular reference resolving attached macro 'xxx' #70096

@ssly1997

Description

@ssly1997

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.circular referenceBug → internal error: Circular referencecompilerThe Swift compiler itselfinternal errorBug: Internal errornested typesFeature: nested typesswift 5.9swift macroFeature → declarations: Swift `macro` declarationstype checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions