Skip to content

Commit 1a10009

Browse files
committed
[Macro] Diagnose macros declared in nested scopes
1 parent ab9608c commit 1a10009

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6767,6 +6767,8 @@ WARNING(macro_type_access_warn,none,
67676767
"because its %select{parameter|result type}3 uses "
67686768
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
67696769
(bool, AccessLevel, AccessLevel, bool))
6770+
ERROR(macro_in_nested,none,
6771+
"macro %0 can only be declared at file scope", (DeclName))
67706772

67716773
//------------------------------------------------------------------------------
67726774
// MARK: Move Only Errors

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,9 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
19851985
void visitMacroDecl(MacroDecl *MD) {
19861986
TypeChecker::checkDeclAttributes(MD);
19871987
checkAccessControl(MD);
1988+
1989+
if (!MD->getDeclContext()->isModuleScopeContext())
1990+
MD->diagnose(diag::macro_in_nested, MD->getName());
19881991
}
19891992

19901993
void visitMacroExpansionDecl(MacroExpansionDecl *MED) {

test/Macros/macros_diagnostics.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ macro m3(_: Int) -> Float = A.B
2525
macro m4(_: Int) -> Int = A.B // expected-note{{'m4' previously declared here}}
2626
macro m4(_: Int) -> Int = A.B // expected-error{{invalid redeclaration of 'm4'}}
2727

28+
struct ZZZ {
29+
macro m5: Int = A.B
30+
// expected-error@-1{{macro 'm5' can only be declared at file scope}}
31+
}
32+
2833
func test(a: Int, b: Int) {
2934
// FIXME: Bad diagnostic.
3035
let s = #stringify<Int, Int>(a + b) // expected-error{{type of expression is ambiguous without more context}}

0 commit comments

Comments
 (0)