Skip to content

Commit 4d3f1fa

Browse files
authored
Merge pull request #64044 from DougGregor/macros-feature-flags
Clean up feature flags for macros.
2 parents 517a539 + f88d2c6 commit 4d3f1fa

29 files changed

+76
-80
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ ERROR(unknown_attribute,none,
220220
NOTE(in_macro_expansion,none,
221221
"in expansion of macro %0 here", (DeclName))
222222
ERROR(macro_experimental,none,
223-
"macros are an experimental feature that is not enabled", ())
223+
"%0 macros are an experimental feature that is not enabled (%1)",
224+
(StringRef, StringRef))
224225
ERROR(ambiguous_macro_reference,none,
225226
"ambiguous reference to macro %0", (DeclName))
226227

include/swift/Basic/Features.def

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ SUPPRESSIBLE_LANGUAGE_FEATURE(PrimaryAssociatedTypes2, 346, "Primary associated
9393
SUPPRESSIBLE_LANGUAGE_FEATURE(UnavailableFromAsync, 0, "@_unavailableFromAsync", true)
9494
SUPPRESSIBLE_LANGUAGE_FEATURE(NoAsyncAvailability, 340, "@available(*, noasync)", true)
9595
LANGUAGE_FEATURE(BuiltinIntLiteralAccessors, 368, "Builtin.IntLiteral accessors", true)
96+
LANGUAGE_FEATURE(Macros, 0, "Macros", true)
9697
LANGUAGE_FEATURE(
97-
FreestandingExpressionMacros, 0, "@freestanding(expression) macros",
98-
langOpts.hasFeature(Feature::Macros))
98+
FreestandingExpressionMacros, 382, "Expression macros",
99+
true)
100+
LANGUAGE_FEATURE(AttachedMacros, 389, "Attached macros", true)
99101

100102
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
101103
UPCOMING_FEATURE(ForwardTrailingClosures, 286, 6)
@@ -107,6 +109,7 @@ EXPERIMENTAL_FEATURE(VariadicGenerics, false)
107109
EXPERIMENTAL_FEATURE(NamedOpaqueTypes, false)
108110
EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
109111
EXPERIMENTAL_FEATURE(MoveOnly, true)
112+
EXPERIMENTAL_FEATURE(FreestandingMacros, true)
110113

111114
// FIXME: MoveOnlyClasses is not intended to be in production,
112115
// but our tests currently rely on it, and we want to run those
@@ -160,9 +163,6 @@ EXPERIMENTAL_FEATURE(ImplicitSome, false)
160163
/// corresponding syntax tree.
161164
EXPERIMENTAL_FEATURE(ParserASTGen, false)
162165

163-
/// Enable the experimental macros feature.
164-
EXPERIMENTAL_FEATURE(Macros, true)
165-
166166
/// Parse using the Swift (swift-syntax) parser and use ASTGen to generate the
167167
/// corresponding syntax tree.
168168
EXPERIMENTAL_FEATURE(BuiltinMacros, false)

lib/AST/ASTPrinter.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2788,7 +2788,23 @@ static bool usesFeatureActors(Decl *decl) {
27882788
}
27892789

27902790
static bool usesFeatureMacros(Decl *decl) {
2791-
return isa<MacroExpansionDecl>(decl) || isa<MacroDecl>(decl);
2791+
return isa<MacroDecl>(decl);
2792+
}
2793+
2794+
static bool usesFeatureFreestandingMacros(Decl *decl) {
2795+
auto macro = dyn_cast<MacroDecl>(decl);
2796+
if (!macro)
2797+
return false;
2798+
2799+
return macro->getMacroRoles().contains(MacroRole::Declaration);
2800+
}
2801+
2802+
static bool usesFeatureAttachedMacros(Decl *decl) {
2803+
auto macro = dyn_cast<MacroDecl>(decl);
2804+
if (!macro)
2805+
return false;
2806+
2807+
return static_cast<bool>(macro->getMacroRoles() & getAttachedMacroRoles());
27922808
}
27932809

27942810
static bool usesFeatureConcurrentFunctions(Decl *decl) {

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,7 @@ Parser::parseSourceFileViaASTGen(SmallVectorImpl<ASTNode> &items,
296296
bool suppressDiagnostics) {
297297
#if SWIFT_SWIFT_PARSER
298298
Optional<DiagnosticTransaction> existingParsingTransaction;
299-
if ((Context.LangOpts.hasFeature(Feature::Macros) ||
300-
Context.LangOpts.hasFeature(Feature::BuiltinMacros) ||
301-
Context.LangOpts.hasFeature(Feature::ParserRoundTrip) ||
302-
Context.LangOpts.hasFeature(Feature::ParserDiagnostics) ||
303-
Context.LangOpts.hasFeature(Feature::ParserValidation) ||
304-
Context.LangOpts.hasFeature(Feature::ParserASTGen)) &&
305-
!SourceMgr.hasIDEInspectionTargetBuffer() &&
299+
if (!SourceMgr.hasIDEInspectionTargetBuffer() &&
306300
SF.Kind != SourceFileKind::SIL) {
307301
StringRef contents =
308302
SourceMgr.extractText(SourceMgr.getRangeForBuffer(L->getBufferID()));

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,8 +2021,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20212021
TypeChecker::checkDeclAttributes(MD);
20222022
checkAccessControl(MD);
20232023

2024-
if (!Ctx.LangOpts.hasFeature(Feature::Macros))
2025-
MD->diagnose(diag::macro_experimental);
20262024
if (!MD->getDeclContext()->isModuleScopeContext())
20272025
MD->diagnose(diag::macro_in_nested, MD->getName());
20282026
if (!MD->getAttrs().hasAttribute<MacroRoleAttr>(/*AllowInvalid*/ true))

lib/Sema/TypeCheckMacros.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,6 @@ Expr *swift::expandMacroExpr(
569569
return nullptr;
570570
}
571571

572-
// Make sure macros are enabled before we expand.
573-
if (!ctx.LangOpts.hasFeature(Feature::Macros)) {
574-
ctx.Diags.diagnose(expr->getLoc(), diag::macro_experimental);
575-
return nullptr;
576-
}
577-
578572
#if SWIFT_SWIFT_PARSER
579573
PrettyStackTraceExpr debugStack(ctx, "expanding macro", expr);
580574

@@ -738,9 +732,11 @@ bool swift::expandFreestandingDeclarationMacro(
738732
return false;
739733
}
740734

741-
// Make sure macros are enabled before we expand.
742-
if (!ctx.LangOpts.hasFeature(Feature::Macros)) {
743-
med->diagnose(diag::macro_experimental);
735+
// Make sure freestanding macros are enabled before we expand.
736+
if (!ctx.LangOpts.hasFeature(Feature::FreestandingMacros) &&
737+
!macro->getMacroRoles().contains(MacroRole::Expression)) {
738+
med->diagnose(
739+
diag::macro_experimental, "freestanding", "FreestandingMacros");
744740
return false;
745741
}
746742

@@ -937,12 +933,6 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
937933
return nullptr;
938934
}
939935

940-
// Make sure macros are enabled before we expand.
941-
if (!ctx.LangOpts.hasFeature(Feature::Macros)) {
942-
attachedTo->diagnose(diag::macro_experimental);
943-
return nullptr;
944-
}
945-
946936
#if SWIFT_SWIFT_PARSER
947937
PrettyStackTraceDecl debugStack("expanding attached macro", attachedTo);
948938

stdlib/public/core/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,6 @@ endif()
289289
# STAGING: Temporarily avoids having to write #fileID in Swift.swiftinterface.
290290
list(APPEND swift_stdlib_compile_flags "-Xfrontend" "-enable-experimental-concise-pound-file")
291291

292-
list(APPEND swift_stdlib_compile_flags "-enable-experimental-feature" "Macros")
293-
294292
if(SWIFT_CHECK_ESSENTIAL_STDLIB)
295293
add_swift_target_library(swift_stdlib_essential ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB IS_STDLIB_CORE
296294
INSTALL_IN_COMPONENT never_install

test/IDE/complete_pound_expr_macros.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=POUND_EXPR_1 -enable-experimental-feature Macros| %FileCheck %s -check-prefix=POUND_EXPR_INTCONTEXT
2-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=POUND_EXPR_2 -enable-experimental-feature Macros | %FileCheck %s -check-prefix=POUND_EXPR_STRINGCONTEXT
3-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=BARE_EXPR_1 -enable-experimental-feature Macros| %FileCheck %s -check-prefix=BARE_EXPR_INTCONTEXT
1+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=POUND_EXPR_1 | %FileCheck %s -check-prefix=POUND_EXPR_INTCONTEXT
2+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=POUND_EXPR_2 | %FileCheck %s -check-prefix=POUND_EXPR_STRINGCONTEXT
3+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=BARE_EXPR_1 | %FileCheck %s -check-prefix=BARE_EXPR_INTCONTEXT
44
// REQUIRES: objc_interop
55

66
import ObjectiveC

test/Index/index_macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s -enable-experimental-feature Macros | %FileCheck %s
2+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck %s
33
// REQUIRES: OS=macosx
44

55

test/Macros/accessor_macros.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// RUN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
33

44
// First check for no errors.
5-
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
5+
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
66

77
// Check that the expansion buffer are as expected.
8-
// RUN: %target-swift-frontend -swift-version 5 -typecheck -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -dump-macro-expansions > %t/expansions-dump.txt 2>&1
8+
// RUN: %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -dump-macro-expansions > %t/expansions-dump.txt 2>&1
99
// RUN: %FileCheck -check-prefix=CHECK-DUMP %s < %t/expansions-dump.txt
1010

1111
// Execution testing
12-
// RUN: %target-build-swift -swift-version 5 -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser
12+
// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser
1313
// RUN: %target-run %t/main | %FileCheck %s
1414
// REQUIRES: executable_test
1515

0 commit comments

Comments
 (0)