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 and compiler crash on memberAttribute macro expansion #78154

Open
li3zhen1 opened this issue Dec 13, 2024 · 0 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@li3zhen1
Copy link
Contributor

li3zhen1 commented Dec 13, 2024

Description

No response

Reproduction

public protocol G {
    associatedtype T: Hashable // Doesn't crash if T has no constraints

    init(t: [T])
}

@Noop             // A member Attribute macro that return an empty list of syntax node.
extension G {
    public static var empty: Self {
        .init(t: [])
    }
}

Macro:

public struct NoopMacro: MemberAttributeMacro {
    public static func expansion(
        of node: SwiftSyntax.AttributeSyntax,
        attachedTo declaration: some SwiftSyntax.DeclGroupSyntax,
        providingAttributesFor member: some SwiftSyntax.DeclSyntaxProtocol,
        in context: some SwiftSyntaxMacros.MacroExpansionContext
    ) throws -> [SwiftSyntax.AttributeSyntax] {
        return []
    }
}
@attached(memberAttribute)
public macro Noop() = #externalMacro(module: "...", type: "NoopMacro")

Stack dump

144 | 
145 | 
146 | public protocol G {
    |                 |- error: circular reference
    |                 |- note: through reference here
    |                 |- note: through reference here
    |                 |- note: through reference here
    |                 `- note: through reference here
147 |     associatedtype T: Hashable
    |                       |- note: while resolving type 'Hashable'
    |                       `- note: through reference here
148 | 
149 |     init(t: [T])
    :
151 | 
152 | @Noop
153 | extension G {
    | `- note: through reference here
154 |     public static var empty: Self {
    |                       `- note: circular reference expanding member attribute macros on 'empty'
155 |         .init(t: [])
156 |     }

/Users/lizhen/Projects/GraphEssentials/Sources/GraphEssentials/CompactGraph.swift:146:17: error: circular reference
144 | 
145 | 
146 | public protocol G {
    |                 |- error: circular reference
    |                 |- note: through reference here
    |                 |- note: through reference here
    |                 |- note: through reference here
    |                 |- note: through reference here
    |                 `- note: through reference here
147 |     associatedtype T: Hashable
    |                       |- note: while resolving type 'Hashable'
    |                       `- note: through reference here
148 | 
149 |     init(t: [T])
    :
151 | 
152 | @Noop
153 | extension G {
    | `- note: through reference here
154 |     public static var empty: Self {
    |                       `- note: circular reference expanding member attribute macros on 'empty'
155 |         .init(t: [])
156 |     }

Redundant conformance requirements in signature <Self where Self : G>:
Ours:
- Copyable
- Escapable
- Hashable
Theirs:
- Hashable
All requirements:
(requirement "Self" conforms_to "Copyable")
(requirement "Self" conforms_to "Escapable")
(requirement "Self.[G]T" conforms_to "Copyable")
(requirement "Self.[G]T" conforms_to "Escapable")
(requirement "Self.[G]T" conforms_to "Hashable")
Requirement machine for <τ_0_0 where τ_0_0 : G>
Rewrite system: {
- [G].[G] => [G] [permanent]
- [G].T => [G:T] [permanent]
- [G].[Copyable] => [G] [explicit]
- [G].[Escapable] => [G] [explicit]
- [G:T].[Copyable] => [G:T] [explicit]
- [G:T].[Escapable] => [G:T] [explicit]
- [G].[G:T] => [G:T]
- τ_0_0.[G] => τ_0_0 [explicit]
- τ_0_0.T => τ_0_0.[G:T]
- τ_0_0.[Copyable] => τ_0_0
- τ_0_0.[Escapable] => τ_0_0
}
Property map: {
  [G] => { conforms_to: [G Copyable Escapable] }
  [G:T] => { conforms_to: [Copyable Escapable] }
  τ_0_0 => { conforms_to: [G Copyable Escapable] }
}
Conformance paths: {
}
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.      Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-11-09-a.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/lizhen/Projects/GraphEssentials/Sources/GraphEssentials/Algorithms/DepthFirstSearch.swift /Users/lizhen/Projects/GraphEssentials/Sources/GraphEssentials/Algorithms/GreedyFAS.swift /Users/lizhen/Projects/GraphEssentials/Sources/GraphEssentials/CompactGraph.swift /Users/lizhen/Projects/GraphEssentials/Sources/GraphEssentials/CompactGraphProtocol.swift -target arm64-apple-macosx15.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -I /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/Modules -I /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-11-09-a.xctoolchain/usr/lib/swift/macosx/testing -I /Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -debug-info-format=dwarf -dwarf-version=5 -module-cache-path /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 6 -Onone -D SWIFT_PACKAGE -D DEBUG -load-plugin-executable /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/MakeEverythingInlinableMacros-tool#MakeEverythingInlinableMacros -empty-abi-descriptor -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-11-09-a.xctoolchain/usr/lib/swift/host/plugins/testing -resource-dir /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-11-09-a.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /Users/lizhen/Projects/GraphEssentials -Xcc -fmodule-map-file=/Users/lizhen/Projects/GraphEssentials/.build/checkouts/swift-syntax/Sources/_SwiftSyntaxCShims/include/module.modulemap -Xcc -I -Xcc /Users/lizhen/Projects/GraphEssentials/.build/checkouts/swift-syntax/Sources/_SwiftSyntaxCShims/include -Xcc -isysroot -Xcc /Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -Xcc -F -Xcc /Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC -Xcc -g -module-name GraphEssentials -package-name graphessentials -in-process-plugin-server-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-11-09-a.xctoolchain/usr/lib/swift/host/libSwiftInProcPluginServer.dylib -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-11-09-a.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-11-09-a.xctoolchain/usr/local/lib/swift/host/plugins -target-sdk-version 15.2 -target-sdk-name macosx15.2 -external-plugin-path /Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Users/lizhen/Downloads/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -emit-module-doc-path /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/Modules/GraphEssentials.swiftdoc -emit-module-source-info-path /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/Modules/GraphEssentials.swiftsourceinfo -emit-objc-header-path /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/GraphEssentials.build/GraphEssentials-Swift.h -emit-dependencies-path /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/GraphEssentials.build/GraphEssentials.emit-module.d -parse-as-library -o /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/Modules/GraphEssentials.swiftmodule -emit-abi-descriptor-path /Users/lizhen/Projects/GraphEssentials/.build/arm64-apple-macosx/debug/Modules/GraphEssentials.abi.json
1.      Apple Swift version 6.1-dev (LLVM 5c0d283c37132bf, Swift b5af518638c9a16)
2.      Compiling with the current language version
3.      While evaluating request TypeCheckSourceFileRequest(source_file "/Users/lizhen/Projects/GraphEssentials/Sources/GraphEssentials/CompactGraph.swift")
4.      While type-checking 'G' (at /Users/lizhen/Projects/GraphEssentials/Sources/GraphEssentials/CompactGraph.swift:146:8)
5.      While checking generic signature <τ_0_0 where τ_0_0 : G>
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000106c3f0f8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106c3d7dc llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000106c3f754 SignalHandler(int) + 304
3  libsystem_platform.dylib 0x000000018fc5ae04 _sigtramp + 56
4  libsystem_pthread.dylib  0x000000018fc23f70 pthread_kill + 288
5  libsystem_c.dylib        0x000000018fb30908 abort + 128
6  swift-frontend           0x0000000102996c58 swift::GenericSignature::verify(llvm::ArrayRef<swift::Requirement>) const::$_7::operator()() const + 152
7  swift-frontend           0x0000000102996104 swift::GenericSignature::verify(llvm::ArrayRef<swift::Requirement>) const + 2600
8  swift-frontend           0x00000001024b6850 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 8848
9  swift-frontend           0x00000001024b45ac swift::TypeChecker::typeCheckDecl(swift::Decl*) + 136
10 swift-frontend           0x00000001025942c8 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 164
11 swift-frontend           0x0000000102595e84 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 232
12 swift-frontend           0x00000001025941fc swift::performTypeChecking(swift::SourceFile&) + 84
13 swift-frontend           0x000000010139e980 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_8>(long, swift::SourceFile&) + 16
14 swift-frontend           0x00000001013979f4 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 156
15 swift-frontend           0x0000000101397938 swift::CompilerInstance::performSema() + 76
16 swift-frontend           0x0000000101150f58 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 60
17 swift-frontend           0x000000010114693c performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 716
18 swift-frontend           0x0000000101146008 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2328
19 swift-frontend           0x0000000100f1d540 swift::mainEntry(int, char const**) + 3100
20 dyld                     0x000000018f8a4274 start + 2840

Expected behavior

Compiles.

Environment

Apple Swift version 6.1-dev (LLVM 5c0d283c37132bf, Swift b5af518)
Target: arm64-apple-macosx15.0

Additional information

The same crash happens if @Noop is replaced with @Observable from Observation.

@li3zhen1 li3zhen1 added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant