-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] A couple of fixes for MiscDiagnostics on macro expansions #77479
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
hamishknight
merged 4 commits into
swiftlang:main
from
hamishknight:macro-misc-diag-fixes
Nov 11, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6352b01
[Sema] Avoid double-diagnosing in macro expansions
hamishknight caceca6
[CS] NFC: Move some code
hamishknight be94e5d
[CS] NFC: Sink `LocalDeclsToTypeCheck` into ExprRewriter
hamishknight 9c3b8a6
[CS] Delay macro expansion until end of CSApply
hamishknight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// REQUIRES: swift_swift_parser | ||
|
||
// RUN: %empty-directory(%t) | ||
// RUN: split-file --leading-lines %s %t | ||
|
||
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroPlugin) -module-name=MacroPlugin %t/MacroPlugin.swift -g -no-toolchain-stdlib-rpath | ||
|
||
// RUN: not %target-swift-frontend -typecheck -swift-version 6 -load-plugin-library %t/%target-library-name(MacroPlugin) %t/Client.swift -module-name Client -diagnostic-style=llvm 2> %t/diags | ||
// RUN: %FileCheck --check-prefix=CHECK-DIAG --implicit-check-not="{{error|warning}}: " -input-file=%t/diags %s | ||
|
||
//--- MacroPlugin.swift | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
|
||
public struct IdentityMacro: ExpressionMacro { | ||
public static func expansion( | ||
of macro: some FreestandingMacroExpansionSyntax, | ||
in context: some MacroExpansionContext | ||
) -> ExprSyntax { | ||
guard let argument = macro.arguments.first else { | ||
fatalError() | ||
} | ||
return "\(argument)" | ||
} | ||
} | ||
|
||
public struct TrailingClosureMacro: ExpressionMacro { | ||
public static func expansion( | ||
of macro: some FreestandingMacroExpansionSyntax, | ||
in context: some MacroExpansionContext | ||
) -> ExprSyntax { | ||
guard let argument = macro.trailingClosure else { | ||
fatalError() | ||
} | ||
return "\(argument)" | ||
} | ||
} | ||
|
||
public struct MakeBinding : DeclarationMacro { | ||
static public func expansion( | ||
of node: some FreestandingMacroExpansionSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [DeclSyntax] { | ||
guard let arg = node.arguments.first else { | ||
fatalError() | ||
} | ||
return ["let x = \(arg)"] | ||
} | ||
} | ||
|
||
//--- Client.swift | ||
@freestanding(expression) | ||
macro identity<T>(_ x: T) -> T = #externalMacro(module: "MacroPlugin", type: "IdentityMacro") | ||
|
||
@freestanding(expression) | ||
macro trailingClosure<T>(_ x: T) -> T = #externalMacro(module: "MacroPlugin", type: "TrailingClosureMacro") | ||
|
||
@freestanding(declaration, names: named(x)) | ||
macro makeBinding<T>(_ x: T) = #externalMacro(module: "MacroPlugin", type: "MakeBinding") | ||
|
||
@available(*, deprecated) | ||
func deprecatedFunc() -> Int { 0 } | ||
|
||
// FIXME: We also ought to be diagnosing the macro argument | ||
_ = #identity(Int) | ||
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-2]]{{.*}}identityfMf_.swift:1:1: error: expected member name or initializer call after type name | ||
|
||
_ = { | ||
_ = #identity(Int) | ||
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-2]]{{.*}}identityfMf0_.swift:1:1: error: expected member name or initializer call after type name | ||
} | ||
|
||
_ = #identity(deprecatedFunc()) | ||
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-2]]{{.*}}identityfMf1_.swift:1:1: warning: 'deprecatedFunc()' is deprecated | ||
// CHECK-DIAG: Client.swift:[[@LINE-2]]:15: warning: 'deprecatedFunc()' is deprecated | ||
|
||
#makeBinding(deprecatedFunc()) | ||
// CHECK-DIAG: Client.swift:[[@LINE-1]]:14: warning: 'deprecatedFunc()' is deprecated | ||
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-3]]{{.*}}makeBindingfMf_.swift:1:9: warning: 'deprecatedFunc()' is deprecated | ||
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-4]]{{.*}}makeBindingfMf_.swift:1:5: warning: initialization of immutable value 'x' was never used | ||
|
||
struct S1 { | ||
#makeBinding(deprecatedFunc()) | ||
// CHECK-DIAG: Client.swift:[[@LINE-1]]:16: warning: 'deprecatedFunc()' is deprecated | ||
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-3]]{{.*}}makeBindingfMf_.swift:1:9: warning: 'deprecatedFunc()' is deprecated | ||
} | ||
|
||
struct S2 { | ||
#makeBinding({deprecatedFunc()}) | ||
// CHECK-DIAG: Client.swift:[[@LINE-1]]:17: warning: 'deprecatedFunc()' is deprecated | ||
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-3]]{{.*}}makeBindingfMf_.swift:2:5: warning: 'deprecatedFunc()' is deprecated | ||
} | ||
|
||
func takesClosure(_ fn: () -> Void) -> Int? { nil } | ||
|
||
_ = #trailingClosure { | ||
if let _ = takesClosure {} {} | ||
// CHECK-DIAG: Client.swift:[[@LINE-1]]:27: warning: trailing closure in this context is confusable with the body of the statement; pass as a parenthesized argument to silence this warning | ||
// 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 | ||
} | ||
|
||
// rdar://138997009 - Make sure we don't crash in MiscDiagnostics' implicit | ||
// self diagnosis. | ||
struct rdar138997009 { | ||
func foo() {} | ||
func bar() { | ||
_ = { | ||
_ = #trailingClosure { | ||
foo() | ||
} | ||
} | ||
} | ||
} | ||
|
||
class rdar138997009_Class { | ||
func foo() {} | ||
func bar() { | ||
_ = { | ||
_ = #trailingClosure { | ||
foo() | ||
// 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 | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please separately evaluate whether it would be possible to subtype all of this walkers from
BaseDiagnosticWalker
and remove all of these overrides?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, I have that exact change in a follow-up PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect! :)