Skip to content

[Macros] Don't allow macros to add accessors to let variables. #73878

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
May 24, 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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7618,6 +7618,9 @@ ERROR(external_macro_arg_not_type_name,none,
ERROR(invalid_decl_in_macro_expansion,none,
"macro expansion cannot introduce %0",
(DescriptiveDeclKind))
ERROR(let_accessor_expansion,none,
"cannot expand accessors on variable declared with 'let'",
())
ERROR(invalid_main_type_in_macro_expansion,none,
"macro expansion cannot introduce '@main' type",
())
Expand Down
16 changes: 13 additions & 3 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,12 +739,12 @@ static void validateMacroExpansion(SourceFile *expansionBuffer,
};

for (auto item : expansionBuffer->getTopLevelItems()) {
auto &ctx = expansionBuffer->getASTContext();
auto *decl = item.dyn_cast<Decl *>();
if (!decl) {
if (role != MacroRole::CodeItem &&
role != MacroRole::Preamble &&
role != MacroRole::Body) {
auto &ctx = expansionBuffer->getASTContext();
ctx.Diags.diagnose(item.getStartLoc(),
diag::expected_macro_expansion_decls);
}
Expand All @@ -753,8 +753,18 @@ static void validateMacroExpansion(SourceFile *expansionBuffer,
}

// Certain macro roles can generate special declarations.
if ((isa<AccessorDecl>(decl) && role == MacroRole::Accessor) ||
(isa<ExtensionDecl>(decl) && role == MacroRole::Conformance)) {
if (isa<AccessorDecl>(decl) && role == MacroRole::Accessor) {
auto *var = dyn_cast<VarDecl>(attachedTo);
if (var && var->isLet()) {
ctx.Diags.diagnose(var->getLoc(),
diag::let_accessor_expansion)
.warnUntilSwiftVersion(6);
}

continue;
}

if (isa<ExtensionDecl>(decl) && role == MacroRole::Conformance) {
continue;
}

Expand Down
9 changes: 9 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2482,3 +2482,12 @@ public struct AllLexicalContextsMacro: DeclarationMacro {
}
}

public struct AddGetterMacro: AccessorMacro {
public static func expansion(
of node: AttributeSyntax,
providingAccessorsOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [AccessorDeclSyntax] {
return ["get { 0 }"]
}
}
11 changes: 11 additions & 0 deletions test/Macros/accessor_macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,14 @@ struct MultipleVars {
// expected-error@-1 2{{accessor macro 'AddWillSet()' can only apply to a single variable}}
}
#endif

@attached(accessor)
macro addGetterMacro() =
#externalMacro(module: "MacroDefinition", type: "AddGetterMacro")

#if TEST_DIAGNOSTICS
struct S {
@addGetterMacro let x: Int
// expected-warning@-1 {{cannot expand accessors on variable declared with 'let'; this is an error in the Swift 6 language mode}}
}
#endif