Skip to content

Commit 48ebf22

Browse files
authored
Merge pull request #63843 from rxwei/fix-macro-decl-module-crash
[Macros] Fix `MacroDecl` serialization crash with custom parameter attributes
2 parents 756523e + ecc3386 commit 48ebf22

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8418,6 +8418,9 @@ class MacroDecl : public GenericContext, public ValueDecl {
84188418
/// Retrieve the definition of this macro.
84198419
MacroDefinition getDefinition() const;
84208420

8421+
/// Retrieve the parameter list of this macro.
8422+
ParameterList *getParameterList() const { return parameterList; }
8423+
84218424
/// Retrieve the builtin macro kind for this macro, or \c None if it is a
84228425
/// user-defined macro with no special semantics.
84238426
Optional<BuiltinMacroKind> getBuiltinKind() const;

lib/Sema/TypeCheckAccess.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,6 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
11741174
UNINTERESTING(Destructor) // Always correct.
11751175
UNINTERESTING(Accessor) // Handled by the Var or Subscript.
11761176
UNINTERESTING(OpaqueType) // Handled by the Var or Subscript.
1177-
UNINTERESTING(Macro)
11781177

11791178
/// If \p VD's layout is exposed by a @frozen struct or class, return said
11801179
/// struct or class.
@@ -1569,6 +1568,10 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
15691568
}
15701569
}
15711570

1571+
void visitMacroDecl(MacroDecl *MD) {
1572+
// FIXME: Check access of macro generic parameters, parameters and result
1573+
}
1574+
15721575
void visitEnumElementDecl(EnumElementDecl *EED) {
15731576
if (!EED->hasAssociatedValues())
15741577
return;

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20192019
if (!MD->getAttrs().hasAttribute<MacroRoleAttr>(/*AllowInvalid*/ true))
20202020
MD->diagnose(diag::macro_without_role, MD->getName());
20212021

2022+
TypeChecker::checkParameterList(MD->getParameterList(), MD);
2023+
20222024
// Check the macro definition.
20232025
switch (auto macroDef = MD->getDefinition()) {
20242026
case MacroDefinition::Kind::Undefined:

test/Serialization/Inputs/def_macros.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ public struct Base {
2424
public struct TestMacroArgTypechecking {
2525
public var value: Int
2626
}
27+
28+
@resultBuilder
29+
public struct Builder {
30+
static func buildBlock(_: Int...) -> Void {}
31+
}
32+
@freestanding(expression)
33+
public macro macroWithBuilderArgs(@Builder _: () -> Void) = #externalMacro(module: "A", type: "B")

test/Serialization/macros.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// RUN: %target-build-swift -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/def_macro_plugin.swift -g -no-toolchain-stdlib-rpath
66
// RUN: %target-swift-frontend -emit-module -o %t/def_macros.swiftmodule %S/Inputs/def_macros.swift -module-name def_macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
77
// RUN: %target-swift-frontend -typecheck -I%t -verify %s -verify-ignore-unknown -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
8+
// RUN: llvm-bcanalyzer %t/def_macros.swiftmodule | %FileCheck %s
9+
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -print-module -module-to-print=def_macros -I %t -source-filename=%s | %FileCheck -check-prefix=CHECK-PRINT %s
810
// REQUIRES: OS=macosx
911

1012
import def_macros
@@ -19,3 +21,9 @@ func test(a: Int, b: Int) {
1921
struct TestStruct {
2022
@myWrapper var x: Int
2123
}
24+
25+
// CHECK: MACRO_DECL
26+
27+
// CHECK-NOT: UnknownCode
28+
29+
// CHECK-PRINT-DAG: macro macroWithBuilderArgs(@Builder _: () -> Void)

0 commit comments

Comments
 (0)