Skip to content

Commit de1cfd2

Browse files
committed
[Macros] Fix MacroDecl serialization crash with custom parameter attributes
Type-check `MacroDecl`'s parameter list so that custom attributes on parameters will have a type. Fixes a serialization crash.
1 parent 25c2c37 commit de1cfd2

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-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:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@resultBuilder
2+
public struct Builder {
3+
static func buildBlock(_: Int...) -> Void {}
4+
}
5+
@freestanding(expression)
6+
public macro macroWithBuilderArgs(@Builder _: () -> Void) = #externalMacro(module: "A", type: "B")
7+
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'macroWithBuilderArgs'}}

test/Serialization/macro_decl.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -module-name macro_decl -o %t %S/Inputs/macro_decl.swift -enable-library-evolution -enable-experimental-feature Macros
3+
// RUN: llvm-bcanalyzer %t/macro_decl.swiftmodule | %FileCheck %s
4+
// RUN: %target-swift-frontend -typecheck -I %t %s -o /dev/null
5+
// RUN: %target-swift-frontend -emit-sil -I %t %s -o /dev/null
6+
7+
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -print-module -module-to-print=macro_decl -I %t -source-filename=%s | %FileCheck -check-prefix=CHECK-PRINT %s
8+
9+
// CHECK: MACRO_DECL
10+
11+
// CHECK-NOT: UnknownCode
12+
13+
// CHECK-PRINT-DAG: macro macroWithBuilderArgs(@Builder _: () -> Void)

0 commit comments

Comments
 (0)