Skip to content

Commit 1b5ed65

Browse files
committed
[Sema] Consistently run MiscDiagnostics on macro arguments
Previously we would only run MiscDiagnostics passes on macro arguments for some statement diagnostics, update the expression walkers that inherit from BaseDiagnosticWalker such that we consistently do MiscDiagnostics on macro arguments.
1 parent 146e95c commit 1b5ed65

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

lib/Sema/MiscDiagnostics.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ namespace swift {
139139
}
140140

141141
MacroWalking getMacroWalkingBehavior() const override {
142-
// Macro expansions will be walked when they're type-checked, not as
143-
// part of the surrounding code.
144-
return MacroWalking::None;
142+
// We only want to walk macro arguments. Expansions will be walked when
143+
// they're type-checked, not as part of the surrounding code.
144+
return MacroWalking::Arguments;
145145
}
146146
};
147147

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ class SyntacticDiagnosticWalker final : public ASTWalker {
342342
}
343343

344344
MacroWalking getMacroWalkingBehavior() const override {
345-
// Macro expansions will be walked when they're type-checked, not as
346-
// part of the surrounding code.
347-
return MacroWalking::None;
345+
// We only want to walk macro arguments. Expansions will be walked when
346+
// they're type-checked, not as part of the surrounding code.
347+
return MacroWalking::Arguments;
348348
}
349349

350350
PreWalkResult<Expr *> walkToExprPre(Expr *expr) override {

test/Macros/macro_misc_diags.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@ macro makeBinding<T>(_ x: T) = #externalMacro(module: "MacroPlugin", type: "Make
6161
@available(*, deprecated)
6262
func deprecatedFunc() -> Int { 0 }
6363

64-
// FIXME: We also ought to be diagnosing the macro argument
64+
// Make sure we do MiscDiagnostics passes for both macro arguments and expansions.
65+
6566
_ = #identity(Int)
6667
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-2]]{{.*}}identityfMf_.swift:1:1: error: expected member name or initializer call after type name
68+
// CHECK-DIAG: Client.swift:[[@LINE-2]]:15: error: expected member name or initializer call after type name
6769

6870
_ = {
6971
_ = #identity(Int)
7072
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-2]]{{.*}}identityfMf0_.swift:1:1: error: expected member name or initializer call after type name
73+
// CHECK-DIAG: Client.swift:[[@LINE-2]]:17: error: expected member name or initializer call after type name
7174
}
7275

7376
_ = #identity(deprecatedFunc())
@@ -99,6 +102,14 @@ _ = #trailingClosure {
99102
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-4]]{{.*}}trailingClosurefMf_.swift:2:27: warning: trailing closure in this context is confusable with the body of the statement
100103
}
101104

105+
func testOptionalToAny(_ y: Int?) {
106+
_ = #trailingClosure {
107+
let _: Any = y
108+
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-3]]{{.*}}trailingClosurefMf_.swift:2:18: warning: expression implicitly coerced from 'Int?' to 'Any'
109+
// CHECK-DIAG: Client.swift:[[@LINE-2]]:18: warning: expression implicitly coerced from 'Int?' to 'Any'
110+
}
111+
}
112+
102113
// rdar://138997009 - Make sure we don't crash in MiscDiagnostics' implicit
103114
// self diagnosis.
104115
struct rdar138997009 {
@@ -119,6 +130,7 @@ class rdar138997009_Class {
119130
_ = #trailingClosure {
120131
foo()
121132
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-3]]{{.*}}trailingClosurefMf_.swift:2:9: error: call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit
133+
// CHECK-DIAG: Client.swift:[[@LINE-2]]:9: error: call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit
122134
}
123135
}
124136
}

0 commit comments

Comments
 (0)