forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sema] Downgrade implicit self diag to warning for macro args
We previously missed diagnosing this for macro args, fixing it turned out to be a bit more source breaking than initially thought though, so downgrade to a warning until Swift 7. rdar://141963700
- Loading branch information
1 parent
7959485
commit e80f6bb
Showing
5 changed files
with
191 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,44 @@ | ||
// 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: %target-swift-frontend -typecheck -swift-version 5 -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 TrailingClosureMacro: ExpressionMacro { | ||
public static func expansion( | ||
of macro: some FreestandingMacroExpansionSyntax, | ||
in context: some MacroExpansionContext | ||
) -> ExprSyntax { | ||
guard let argument = macro.trailingClosure else { | ||
fatalError() | ||
} | ||
return "\(argument)" | ||
} | ||
} | ||
|
||
//--- Client.swift | ||
@freestanding(expression) | ||
macro trailingClosure<T>(_ x: T) -> T = #externalMacro(module: "MacroPlugin", type: "TrailingClosureMacro") | ||
|
||
class rdar138997009_Class { | ||
func foo() {} | ||
func bar() { | ||
// rdar://141963700 - This is downgraded to a warning for Swift 6 in the | ||
// expansion, and Swift 7 for the argument. | ||
_ = { [self] in | ||
_ = #trailingClosure { | ||
foo() | ||
// CHECK-DIAG: @__swiftmacro_6Client0017Clientswift_yEEFcfMX[[@LINE-3]]{{.*}}trailingClosurefMf_.swift:2:9: warning: call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit; this is an error in the Swift 6 language mode | ||
// CHECK-DIAG: Client.swift:[[@LINE-2]]:9: warning: call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit; this will be an error in a future Swift language mode | ||
} | ||
} | ||
} | ||
} |
This file contains 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,61 @@ | ||
// REQUIRES: swift_swift_parser | ||
// REQUIRES: swift7 | ||
|
||
// 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 7 -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 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 MakeFunc : DeclarationMacro { | ||
static public func expansion( | ||
of node: some FreestandingMacroExpansionSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [DeclSyntax] { | ||
["func expansionFn() -> Int { 0 }"] | ||
} | ||
} | ||
|
||
//--- Client.swift | ||
@freestanding(expression) | ||
macro trailingClosure<T>(_ x: T) -> T = #externalMacro(module: "MacroPlugin", type: "TrailingClosureMacro") | ||
|
||
@freestanding(declaration, names: named(expansionFn)) | ||
macro makeFunc<T>(_ x: T) = #externalMacro(module: "MacroPlugin", type: "MakeFunc") | ||
|
||
class rdar138997009_Class { | ||
func foo() {} | ||
func bar() { | ||
// rdar://141963700 - In Swift 7 these are errors. | ||
_ = { | ||
_ = #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 | ||
// CHECK-DIAG: Client.swift:[[@LINE-2]]:9: error: call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit | ||
} | ||
// Use an attribute to force a MacroExpansionDecl (otherwise we parse a | ||
// MacroExpansionExpr) | ||
@discardableResult | ||
#makeFunc(foo()) | ||
// CHECK-DIAG: Client.swift:[[@LINE-1]]:17: error: call to method 'foo' in closure requires explicit use of 'self' to make capture semantics explicit | ||
} | ||
} | ||
} |