Skip to content
Open
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
19 changes: 19 additions & 0 deletions test/Utils/update-verify-tests/Inputs/unstringify-macro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct UnstringifyPeerMacro: PeerMacro {
public static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
do {
let argumentList = node.arguments!.as(LabeledExprListSyntax.self)!
let arguments = [LabeledExprSyntax](argumentList)
let arg = arguments.first!.expression.as(StringLiteralExprSyntax.self)!
let content = arg.representedLiteralValue!
return [DeclSyntax("\(raw: content)")]
}
}
}
240 changes: 240 additions & 0 deletions test/Utils/update-verify-tests/expansion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
// REQUIRES: swift_swift_parser, executable_test

// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// Building this macro takes some time, so amortise the cost by using it for multiple sub tests
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(UnstringifyMacroDefinition) -module-name=UnstringifyMacroDefinition \
// RUN: %S/Inputs/unstringify-macro.swift -g -no-toolchain-stdlib-rpath



// RUN: not %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/single.swift 2>%t/output.txt
// RUN: %update-verify-tests < %t/output.txt
// RUN: %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/single.swift
// RUN: %diff %t/single.swift %t/single.swift.expected

// RUN: not %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/multiple.swift 2>%t/output.txt
// RUN: %update-verify-tests < %t/output.txt
// RUN: %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/multiple.swift
// RUN: %diff %t/multiple.swift %t/multiple.swift.expected

// RUN: not %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/existing.swift 2>%t/output.txt
// RUN: %update-verify-tests < %t/output.txt
// RUN: %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/existing.swift
// RUN: %diff %t/existing.swift %t/existing.swift.expected

// RUN: not %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/gone.swift 2>%t/output.txt
// RUN: %update-verify-tests < %t/output.txt
// RUN: %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/gone.swift
// RUN: %diff %t/gone.swift %t/gone.swift.expected

// RUN: not %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/wrong-location.swift 2>%t/output.txt
// RUN: %update-verify-tests < %t/output.txt
// RUN: %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/wrong-location.swift
// RUN: %diff %t/wrong-location.swift %t/wrong-location.swift.expected

// RUN: not %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/nested.swift 2>%t/output.txt
// RUN: %update-verify-tests < %t/output.txt
// RUN: %target-swift-frontend-verify -load-plugin-library %t/%target-library-name(UnstringifyMacroDefinition) -typecheck %t/nested.swift
// RUN: %diff %t/nested.swift %t/nested.swift.expected

//--- single.swift
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

@unstringifyPeer("""
func foo(_ x: Int) {
let a = x
}
""")
func foo() {}

//--- single.swift.expected
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

// expected-note@+1{{in expansion of macro 'unstringifyPeer' on global function 'foo()' here}}
@unstringifyPeer("""
func foo(_ x: Int) {
let a = x
}
""")
// expected-expansion@+3:14{{
// expected-warning@2{{initialization of immutable value 'a' was never used; consider replacing with assignment to '_' or removing it}}
// }}
func foo() {}

//--- multiple.swift
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

@unstringifyPeer("""
func foo(_ x: Int) {
a = 2
b = x
}
""")
func foo() {}

//--- multiple.swift.expected
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

// expected-note@+1 4{{in expansion of macro 'unstringifyPeer' on global function 'foo()' here}}
@unstringifyPeer("""
func foo(_ x: Int) {
a = 2
b = x
}
""")
// expected-expansion@+5:14{{
// expected-note@1 2{{'x' declared here}}
// expected-error@2{{cannot find 'a' in scope; did you mean 'x'?}}
// expected-error@3{{cannot find 'b' in scope; did you mean 'x'?}}
// }}
func foo() {}

//--- existing.swift
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

@unstringifyPeer("""
func foo(_ x: Int) {
a = 2
b = x
}
""")
//expected-expansion@+4:14{{
// expected-note@1 {{'x' declared here}}
// expected-error@3 {{cannot find 'b' in scope; did you mean 'x'?}}
//}}
func foo() {}

//--- existing.swift.expected
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

// expected-note@+1 4{{in expansion of macro 'unstringifyPeer' on global function 'foo()' here}}
@unstringifyPeer("""
func foo(_ x: Int) {
a = 2
b = x
}
""")
//expected-expansion@+5:14{{
// expected-error@3 {{cannot find 'b' in scope; did you mean 'x'?}}
// expected-note@1 2{{'x' declared here}}
// expected-error@2 {{cannot find 'a' in scope; did you mean 'x'?}}
//}}
func foo() {}

//--- gone.swift
//expected-expansion@+4:14{{
// expected-note@1 {{'x' declared here}}
// expected-error@3 {{cannot find 'b' in scope; did you mean 'x'?}}
//}}
func foo() {}

//--- gone.swift.expected
func foo() {}

//--- wrong-location.swift
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

// expected-expansion@2:14{{
// expected-note@2 {{'x' declared here}}
// expected-error@3 {{cannot find 'b' in scope; did you mean 'x'?}}
// }}
@unstringifyPeer("""
func foo(_ x: Int) {
a = 2
b = x
}
""")
func foo() {}

//--- wrong-location.swift.expected
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

// expected-note@+1 4{{in expansion of macro 'unstringifyPeer' on global function 'foo()' here}}
@unstringifyPeer("""
func foo(_ x: Int) {
a = 2
b = x
}
""")
// expected-expansion@+5:14{{
// expected-note@1 2{{'x' declared here}}
// expected-error@2{{cannot find 'a' in scope; did you mean 'x'?}}
// expected-error@3{{cannot find 'b' in scope; did you mean 'x'?}}
// }}
func foo() {}

//--- nested.swift
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")
// hack to make this seem non-recursive
@attached(peer, names: overloaded)
macro unstringifyPeer2(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

@unstringifyPeer("""
func bar(_ y: Int) {
@unstringifyPeer2(\"""
func foo(_ x: Int) {
a = 2
b = x
}
\""")
func foo() {}
foo(y)
}
""")
func bar() {}

//--- nested.swift.expected
@attached(peer, names: overloaded)
macro unstringifyPeer(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")
// hack to make this seem non-recursive
@attached(peer, names: overloaded)
macro unstringifyPeer2(_ s: String) =
#externalMacro(module: "UnstringifyMacroDefinition", type: "UnstringifyPeerMacro")

// expected-note@+1 7{{in expansion of macro 'unstringifyPeer' on global function 'bar()' here}}
@unstringifyPeer("""
func bar(_ y: Int) {
@unstringifyPeer2(\"""
func foo(_ x: Int) {
a = 2
b = x
}
\""")
func foo() {}
foo(y)
}
""")
// expected-expansion@+10:14{{
// expected-note@1 2{{did you mean 'y'?}}
// expected-note@2 4{{in expansion of macro 'unstringifyPeer2' on local function 'foo()' here}}
// expected-expansion@9:6{{
// expected-note@1 2{{did you mean 'x'?}}
// expected-error@2{{cannot find 'a' in scope}}
// expected-error@3{{cannot find 'b' in scope}}
// }}
// expected-error@10{{argument passed to call that takes no arguments}}
// }}
func bar() {}

21 changes: 21 additions & 0 deletions test/Utils/update-verify-tests/remarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: not %target-swift-frontend-verify -typecheck %t/test.swift -Rmodule-api-import 2>%t/output.txt
// RUN: %update-verify-tests < %t/output.txt
// RUN: %target-swift-frontend-verify -typecheck %t/test.swift -Rmodule-api-import
// RUN: %diff %t/test.swift %t/test.swift.expected

//--- test.swift
public typealias Foo = String

public typealias Bar = Optional<Int> // expected-remark@+1{{asdf}}

//--- test.swift.expected
// expected-remark@+1{{struct 'String' is imported via 'Swift'}}
public typealias Foo = String

// expected-remark@+2{{struct 'Int' is imported via 'Swift'}}
// expected-remark@+1{{generic enum 'Optional' is imported via 'Swift'}}
public typealias Bar = Optional<Int>

64 changes: 64 additions & 0 deletions test/Utils/update-verify-tests/update-existing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: not %target-swift-frontend-verify -typecheck %t/test.swift 2>%t/output.txt
// RUN: %update-verify-tests < %t/output.txt
// RUN: %target-swift-frontend-verify -typecheck %t/test.swift
// RUN: %diff %t/test.swift %t/test.swift.expected

//--- test.swift
func foo() {
let a = 2 // expected-error@+1{{asdf}}
b = a // expected-error@+1{{asdf}}
}

func bar() {
a = 2 // expected-error@+1{{asdf}}
}

func baz() {
// expected-error@+2{{cannot find 'a' in scope}}
// expected-error@+1{{cannot find 'a'}}
let b = a; let c = a; // expected-error{{asdf}}
}

func qux() {
let b = a; let c = a; // expected-error{{asdf}}
}

func foobar() {
var b = 1
b = a; b = a; // expected-error{{asdf}}
}

//--- test.swift.expected
func foo() {
// expected-note@+1{{'a' declared here}}
let a = 2 // expected-error@+1{{cannot find 'b' in scope; did you mean 'a'?}}
b = a
}

func bar() {
// expected-error@+1{{cannot find 'a' in scope}}
a = 2
}

func baz() {
// expected-error@+3{{cannot find 'a' in scope}}
// expected-error@+2{{cannot find 'a'}}
// expected-note@+1{{'b' declared here}}
let b = a; let c = a;
}

func qux() {
// expected-note@+2{{'b' declared here}}
// expected-error@+1{{cannot find 'a' in scope}}
let b = a; let c = a; // expected-error{{cannot find 'a' in scope; did you mean 'b'?}}
}

func foobar() {
// expected-note@+1 2{{'b' declared here}}
var b = 1
b = a; b = a; // expected-error 2{{cannot find 'a' in scope; did you mean 'b'?}}
}

5 changes: 1 addition & 4 deletions utils/update-verify-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@

def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--prefix", default="", help="The prefix passed to -verify"
)
parser.add_argument("--prefix", default="", help="The prefix passed to -verify")
args = parser.parse_args()
(ret_code, output) = check_expectations(sys.stdin.readlines(), args.prefix)
print(output)
Expand All @@ -41,4 +39,3 @@ def main():

if __name__ == "__main__":
main()

Loading