Skip to content

Commit becbdc8

Browse files
committed
[Macros] Disallow declaration of codeItem macros
1 parent 186b854 commit becbdc8

File tree

6 files changed

+29
-2
lines changed

6 files changed

+29
-2
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ ERROR(unknown_attribute,none,
223223
NOTE(in_macro_expansion,none,
224224
"in expansion of macro %0 here", (DeclName))
225225
ERROR(macro_experimental,none,
226-
"%0 macros are an experimental feature that is not enabled (%1)",
226+
"%0 macros are an experimental feature that is not enabled %select{|(%1)}1",
227227
(StringRef, StringRef))
228228
ERROR(ambiguous_macro_reference,none,
229229
"ambiguous reference to macro %0", (DeclName))

include/swift/AST/MacroDeclaration.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ bool isAttachedMacro(MacroRoles contexts);
8383

8484
MacroRoles getAttachedMacroRoles();
8585

86+
/// Checks if the macro is supported or guarded behind an experimental flag.
87+
bool isMacroSupported(MacroRole role, ASTContext &ctx);
88+
8689
enum class MacroIntroducedDeclNameKind {
8790
Named,
8891
Overloaded,

lib/AST/Decl.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10182,6 +10182,21 @@ MacroRoles swift::getAttachedMacroRoles() {
1018210182
return attachedMacroRoles;
1018310183
}
1018410184

10185+
bool swift::isMacroSupported(MacroRole role, ASTContext &ctx) {
10186+
switch (role) {
10187+
case MacroRole::Expression:
10188+
case MacroRole::Declaration:
10189+
case MacroRole::Accessor:
10190+
case MacroRole::MemberAttribute:
10191+
case MacroRole::Member:
10192+
case MacroRole::Peer:
10193+
case MacroRole::Conformance:
10194+
return true;
10195+
case MacroRole::CodeItem:
10196+
return ctx.LangOpts.hasFeature(Feature::CodeItemMacros);
10197+
}
10198+
}
10199+
1018510200
void MissingDecl::forEachMacroExpandedDecl(MacroExpandedDeclCallback callback) {
1018610201
auto macroRef = unexpandedMacro.macroRef;
1018710202
auto *baseDecl = unexpandedMacro.baseDecl;

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,11 @@ Parser::parseMacroRoleAttribute(
22572257
status.setIsParseError();
22582258
return status;
22592259
}
2260+
if (!isMacroSupported(*role, Context)) {
2261+
diagnose(roleNameLoc, diag::macro_experimental, roleName.str(), "");
2262+
status.setIsParseError();
2263+
return status;
2264+
}
22602265

22612266
// Check that the role makes sense.
22622267
if (isAttached == !isAttachedMacro(*role)) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 5 %s
2+
3+
@freestanding(codeItem) // expected-error {{codeItem macros are an experimental feature that is not enabled}}
4+
macro codeItems() // expected-error {{macro 'codeItems()' requires a definition}}

test/Macros/macros_diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// REQUIRES: swift_swift_parser
22

3-
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -module-name MacrosTest
3+
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -enable-experimental-feature CodeItemMacros -module-name MacrosTest
44

55
@expression macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
66
// expected-note@-1 2{{'stringify' declared here}}

0 commit comments

Comments
 (0)