Skip to content

[6.0][IRGen] Fetch the correct IGM for synthesized files #74825

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

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
}

if (auto *synthSFU = File->getSynthesizedFile()) {
CurrentIGMPtr IGM = irgen.getGenModule(synthSFU);
CurrentIGMPtr IGM = irgen.getGenModule(&synthSFU->getFileUnit());
IGM->emitSynthesizedFileUnit(*synthSFU);
}
} else {
Expand Down
61 changes: 61 additions & 0 deletions test/Macros/extension_emission.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// REQUIRES: swift_swift_parser

// This test ensures that code generated by extension macros gets emitted into the correct LLVM module
// rdar://128870792

// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %t/macro.swift -g -no-toolchain-stdlib-rpath

// RUN: %target-swift-frontend -num-threads 1 -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -enable-library-evolution %t/b.swift %t/a.swift -emit-ir -o %t/b.ll -o %t/a.ll

// RUN: %FileCheck -check-prefix=CHECK-A %s < %t/a.ll

// RUN: %FileCheck -check-prefix=CHECK-B %s < %t/b.ll

//--- macro.swift

import SwiftSyntax
import SwiftSyntaxBuilder
@_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacros

public struct SomeExtensionMacro: ExtensionMacro {
public static func expansion(of node: AttributeSyntax, attachedTo declaration: some DeclGroupSyntax, providingExtensionsOf type: some TypeSyntaxProtocol, conformingTo protocols: [TypeSyntax], in context: some MacroExpansionContext) throws -> [ExtensionDeclSyntax] {
let decl: DeclSyntax =
"""
extension \(type.trimmed) {
struct Storage {
let x: Int
}

func alsoUseStorage(_ x: Storage?) {
print(type(of: x))
}
}
"""
guard let extensionDecl = decl.as(ExtensionDeclSyntax.self) else {
return []
}

return [extensionDecl]
}
}

//--- a.swift

@attached(
extension,
names: named(Storage), named(alsoUseStorage)
)
macro someExtension() = #externalMacro(module: "MacroDefinition", type: "SomeExtensionMacro")

// CHECK-A: @"$s1b1SV7StorageVMn" = {{.*}}constant
@someExtension
struct S {}

//--- b.swift

// This file needs no content, it just needs to exist

// CHECK-B-NOT: @"$s1b1SV7StorageVMn"