Skip to content

Commit b2069a3

Browse files
committed
Update tests with diagnostics in macro expansions
1 parent e3d92db commit b2069a3

10 files changed

+232
-83
lines changed

test/Concurrency/task_local.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,23 @@
77
struct TL {
88
@TaskLocal // expected-note{{in expansion of macro 'TaskLocal' on static property 'number' here}}
99
static var number: Int = 0
10+
/*
11+
expected-expansion@-2:29{{
12+
expected-note@1:8{{change 'let' to 'var' to make it mutable}}
13+
}}
14+
*/
1015

1116
@TaskLocal
1217
static var someNil: Int?
1318

1419
// expected-note@+1{{in expansion of macro 'TaskLocal' on static property 'noValue' here}}
1520
@TaskLocal // expected-error{{@TaskLocal' property must have default value, or be optional}}
1621
static var noValue: Int // expected-note{{'noValue' declared here}}
22+
/*
23+
expected-expansion@-2:26{{
24+
expected-error@3:9{{cannot find '$noValue' in scope; did you mean 'noValue'?}}
25+
}}
26+
*/
1727

1828
@TaskLocal // expected-error{{'@TaskLocal' can only be applied to 'static' property}}
1929
var notStatic: String?

test/Distributed/Macros/distributed_macro_expansion_DistributedProtocol_errors.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ distributed actor Caplin {
3030
protocol Fail: DistributedActor {
3131
distributed func method() -> String
3232
}
33+
/*
34+
expected-expansion@-2:2{{
35+
expected-error@1:19{{distributed actor '$Fail' does not declare ActorSystem it can be used with}}
36+
expected-note@1:13{{you can provide a module-wide default actor system by declaring:}}
37+
expected-error@1:19{{type '$Fail' does not conform to protocol 'DistributedActor'}}
38+
expected-note@1:19{{add stubs for conformance}}
39+
}}
40+
*/
3341

3442
@Resolvable // expected-note2{{in expansion of macro 'Resolvable' on protocol 'SomeRoot' here}}
3543
public protocol SomeRoot: DistributedActor, Sendable
@@ -39,3 +47,9 @@ public protocol SomeRoot: DistributedActor, Sendable
3947
static var staticValue: String { get }
4048
var value: String { get }
4149
}
50+
/*
51+
expected-expansion@-2:2{{
52+
expected-error@1:27{{type '$SomeRoot<ActorSystem>' does not conform to protocol 'SomeRoot'}}
53+
expected-note@1:27{{add stubs for conformance}}
54+
}}
55+
*/

test/Macros/Inputs/top_level_freestanding_other.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ func deprecated() -> Int { 0 }
1313
var globalVar3 = #stringify({ deprecated() })
1414
// expected-note@-1 {{in expansion of macro 'stringify' here}}
1515
// expected-warning@-2{{'deprecated()' is deprecated}}
16+
// expected-expansion@-3:18{{
17+
// expected-warning@2:9{{'deprecated()' is deprecated}}
18+
// }}
1619

1720
var globalVar4 = #stringify({ deprecated() })
1821
// expected-note@-1 {{in expansion of macro 'stringify' here}}
1922
// expected-warning@-2{{'deprecated()' is deprecated}}
23+
// expected-expansion@-3:18{{
24+
// expected-warning@2:9{{'deprecated()' is deprecated}}
25+
// }}
2026

test/Macros/accessor_macros.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ struct MyBrokenStruct {
9999
// expected-note@+1 2{{in expansion of macro 'myPropertyWrapper' on property 'birthDate' here}}
100100
@myPropertyWrapper
101101
var birthDate: Date? {
102+
/*
103+
expected-expansion@-2:25{{
104+
expected-error@1:1{{variable already has a getter}}
105+
}}
106+
*/
102107
// CHECK-DIAGS: variable already has a getter
103108
// CHECK-DIAGS: in expansion of macro
104109
// CHECK-DIAGS: previous definition of getter here
@@ -153,6 +158,12 @@ struct HasStoredTests {
153158
// expected-error@-1{{expansion of macro 'MakeComputedSneakily()' produced an unexpected getter}}
154159
// expected-note@-2 2{{in expansion of macro}}
155160
// expected-note@-3 2{{'z' declared here}}
161+
/*
162+
expected-expansion@-5:36{{
163+
expected-error@3:9{{cannot find '_z' in scope; did you mean 'z'?}}
164+
expected-error@6:9{{cannot find '_z' in scope; did you mean 'z'?}}
165+
}}
166+
*/
156167
#endif
157168
}
158169

test/Macros/macro_expand.swift

Lines changed: 114 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ macro NotCovered() = #externalMacro(module: "MacroDefinition", type: "InvalidMac
7373
struct MemberNotCovered {
7474
#NotCovered
7575
// expected-note@-1 {{in expansion of macro 'NotCovered' here}}
76-
77-
// CHECK-DIAGS: error: declaration name 'value' is not covered by macro 'NotCovered'
78-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-5]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll10NotCoveredfMf_.swift
79-
// CHECK-DIAGS: var value: Int
80-
// CHECK-DIAGS: END CONTENTS OF FILE
76+
/*
77+
expected-expansion@-3:3 {{
78+
expected-error@1:5{{declaration name 'value' is not covered by macro 'NotCovered'}}
79+
}}
80+
*/
8181
}
8282

8383
@attached(peer)
@@ -86,24 +86,29 @@ macro Invalid() = #externalMacro(module: "MacroDefinition", type: "InvalidMacro"
8686
@Invalid
8787
struct Bad {}
8888
// expected-note@-2 18 {{in expansion of macro 'Invalid' on struct 'Bad' here}}
89+
/*
90+
expected-expansion@-3:14 {{
91+
expected-error@1:8{{macro expansion cannot introduce import}}
92+
expected-error@3:17{{macro expansion cannot introduce precedence group}}
93+
expected-error@6:25{{macro 'myMacro()' requires a definition}}
94+
expected-error@6:25{{macro expansion cannot introduce macro}}
95+
expected-error@8:1{{macro expansion cannot introduce extension}}
96+
expected-error@11:1{{macro expansion cannot introduce '@main' type}}
97+
expected-error@12:8{{declaration name 'MyMain' is not covered by macro 'Invalid'}}
98+
expected-error@17:11{{declaration name 'Array' is not covered by macro 'Invalid'}}
99+
expected-error@19:11{{declaration name 'Dictionary' is not covered by macro 'Invalid'}}
100+
expected-error@21:11{{macro expansion cannot introduce default literal type 'BooleanLiteralType'}}
101+
expected-error@23:11{{macro expansion cannot introduce default literal type 'ExtendedGraphemeClusterType'}}
102+
expected-error@25:11{{macro expansion cannot introduce default literal type 'FloatLiteralType'}}
103+
expected-error@27:11{{macro expansion cannot introduce default literal type 'IntegerLiteralType'}}
104+
expected-error@29:11{{macro expansion cannot introduce default literal type 'StringLiteralType'}}
105+
expected-error@31:11{{macro expansion cannot introduce default literal type 'UnicodeScalarType'}}
106+
expected-error@33:11{{macro expansion cannot introduce default literal type '_ColorLiteralType'}}
107+
expected-error@35:11{{macro expansion cannot introduce default literal type '_ImageLiteralType'}}
108+
expected-error@37:11{{macro expansion cannot introduce default literal type '_FileReferenceLiteralType'}}
109+
}}
110+
*/
89111

90-
// CHECK-DIAGS: error: macro expansion cannot introduce import
91-
// CHECK-DIAGS: error: macro expansion cannot introduce precedence group
92-
// CHECK-DIAGS: error: macro expansion cannot introduce macro
93-
// CHECK-DIAGS: error: macro expansion cannot introduce extension
94-
// CHECK-DIAGS: error: macro expansion cannot introduce '@main' type
95-
// CHECK-DIAGS: error: declaration name 'MyMain' is not covered by macro 'Invalid'
96-
// CHECK-DIAGS: error: declaration name 'Array' is not covered by macro 'Invalid'
97-
// CHECK-DIAGS: error: declaration name 'Dictionary' is not covered by macro 'Invalid'
98-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'BooleanLiteralType'
99-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'ExtendedGraphemeClusterType'
100-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'FloatLiteralType'
101-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'IntegerLiteralType'
102-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'StringLiteralType'
103-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'UnicodeScalarType'
104-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type '_ColorLiteralType'
105-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type '_ImageLiteralType'
106-
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type '_FileReferenceLiteralType'
107112

108113
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3Bad7InvalidfMp_.swift
109114
// CHECK-DIAGS: import Swift
@@ -132,21 +137,28 @@ struct Bad {}
132137

133138
class HasStoredPropertyClassInvalid {
134139
#AddStoredProperty((Self.self, 0).1) // expected-note {{in expansion of macro 'AddStoredProperty' here}}
135-
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-2]]_2_33_{{.*}}AddStoredPropertyfMf_.swift:1:22: error: covariant 'Self' type cannot be referenced from a stored property initializer
140+
/*
141+
expected-expansion@-2:3 {{
142+
expected-error@1:22{{covariant 'Self' type cannot be referenced from a stored property initializer}}
143+
}}
144+
*/
136145
}
137146

138147
// Redeclaration checking should behave as though expansions are part of the
139148
// source file.
140149
struct RedeclChecking {
141150
#varValue
151+
/*
152+
expected-expansion@-2:3 {{
153+
expected-note@1:5{{'value' previously declared here}}
154+
}}
155+
*/
142156

143157
// expected-error@+1 {{invalid redeclaration of 'value'}}
144158
var value: Int { 0 }
145159
}
146160

147-
// CHECK-DIAGS: macro_expand.swift:[[@LINE-3]]:7: error: invalid redeclaration of 'value'
148-
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-8]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll8varValuefMf_.swift:1:5: note: 'value' previously declared here
149-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-9]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll8varValuefMf_.swift:
161+
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-12]]_2_33_4361AD9339943F52AE6186DD51E04E91Ll8varValuefMf_.swift:
150162
// CHECK-DIAGS: var value: Int {
151163
// CHECK-DIAGS: 1
152164
// CHECK-DIAGS: }
@@ -159,11 +171,19 @@ public macro ThrowCancellation() = #externalMacro(module: "MacroDefinition", typ
159171
// error mismatch.
160172
@ThrowCancellation // expected-note {{in expansion of macro 'ThrowCancellation' on global function 'issue79039()' here}}
161173
func issue79039() throws(DecodingError)
162-
// CHECK-DIAGS: @__swiftmacro_9MacroUser10issue7903917ThrowCancellationfMb_.swift:2:11: error: thrown expression type 'CancellationError' cannot be converted to error type 'DecodingError'
174+
/*
175+
expected-expansion@-2:39 {{
176+
expected-error@2:11{{thrown expression type 'CancellationError' cannot be converted to error type 'DecodingError'}}
177+
}}
178+
*/
163179

164180
@ThrowCancellation // expected-note {{in expansion of macro 'ThrowCancellation' on global function 'issue79039_2()' here}}
165181
func issue79039_2() throws(DecodingError) {}
166-
// CHECK-DIAGS: @__swiftmacro_9MacroUser12issue79039_217ThrowCancellationfMb_.swift:2:11: error: thrown expression type 'CancellationError' cannot be converted to error type 'DecodingError'
182+
/*
183+
expected-expansion@-2:43 {{
184+
expected-error@2:11{{thrown expression type 'CancellationError' cannot be converted to error type 'DecodingError'}}
185+
}}
186+
*/
167187
#endif
168188

169189
@freestanding(declaration)
@@ -176,16 +196,28 @@ macro AccidentalCodeItem() = #externalMacro(module: "MacroDefinition", type: "Fa
176196
func invalidDeclarationMacro() {
177197
#accidentalCodeItem
178198
// expected-note@-1 {{in expansion of macro 'accidentalCodeItem' here}}
179-
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-3]]_2_18accidentalCodeItemfMf_.swift:1:1: error: expected macro expansion to produce a declaration
199+
/*
200+
expected-expansion@-3:3 {{
201+
expected-error@1:1{{expected macro expansion to produce a declaration}}
202+
}}
203+
*/
180204

181205
@AccidentalCodeItem struct S {}
182206
// expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}}
183-
// CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l018AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration
207+
/*
208+
expected-expansion@-3:34 {{
209+
expected-error@1:1{{expected macro expansion to produce a declaration}}
210+
}}
211+
*/
184212

185213
do {
186214
@AccidentalCodeItem struct S {}
187215
// expected-note@-1 {{in expansion of macro 'AccidentalCodeItem' on struct 'S' here}}
188-
// CHECK-DIAGS: @__swiftmacro_9MacroUser018invalidDeclarationA0yyF5S_$l118AccidentalCodeItemfMp_.swift:1:1: error: expected macro expansion to produce a declaration
216+
/*
217+
expected-expansion@-3:36 {{
218+
expected-error@1:1{{expected macro expansion to produce a declaration}}
219+
}}
220+
*/
189221
}
190222
}
191223
#endif
@@ -323,9 +355,14 @@ func testNested() {
323355
struct Nested { }
324356
_ = #stringify(#assertAny(Nested()))
325357
// expected-note@-1 {{in expansion of macro 'stringify' here}}
326-
// CHECK-DIAGS-NOT: error: cannot convert value of type 'Nested' to expected argument type 'Bool'
327-
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX{{.*}}_9stringifyfMf_9assertAnyfMf_.swift:1:8: error: cannot convert value of type 'Nested' to expected argument type 'Bool'
328-
// CHECK-DIAGS-NOT: error: cannot convert value of type 'Nested' to expected argument type 'Bool'
358+
/*
359+
expected-expansion@-3:7 {{
360+
expected-note@1:2{{in expansion of macro 'assertAny' here}}
361+
expected-expansion@1:2{{
362+
expected-error@1:8{{cannot convert value of type 'Nested' to expected argument type 'Bool'}}
363+
}}
364+
}}
365+
*/
329366

330367
// PRETTY-DIAGS: 1:8: error: cannot convert value of type 'Nested' to expected argument type 'Bool'
331368
// PRETTY-DIAGS: macro_expand.swift:{{.*}}:39: note: expanded code originates here
@@ -344,8 +381,15 @@ func testStringifyWithThrows() throws {
344381
#if TEST_DIAGNOSTICS
345382
// FIXME: Lots of duplicate notes here
346383
_ = #stringify(maybeThrowing()) // expected-note 4{{in expansion of macro 'stringify' here}}
384+
/*
385+
expected-expansion@-2:7 {{
386+
expected-error@1:2{{call can throw but is not marked with 'try'}}
387+
expected-note@1:2{{did you mean to disable error propagation?}}
388+
expected-note@1:2{{did you mean to handle error as optional value?}}
389+
expected-note@1:2{{did you mean to use 'try'?}}
390+
}}
391+
*/
347392

348-
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX{{.*}}_9stringifyfMf1_.swift:1:2: error: call can throw but is not marked with 'try'
349393
#endif
350394

351395
// The macro adds the 'try' for us.
@@ -385,14 +429,18 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
385429
_ = #addBlocker(oa + oa) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}
386430
// expected-note@-1{{in expansion of macro 'addBlocker' here}}
387431
// expected-note@-2{{use '-'}}{{22-23=-}}
388-
389-
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX{{.*}}_10addBlockerfMf1_.swift:1:4: error: binary operator '-' cannot be applied to two 'OnlyAdds' operands [] []
390-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX{{.*}}_10addBlockerfMf1_.swift:
391-
// CHECK-DIAGS-NEXT: Original source range: {{.*}}macro_expand.swift:[[@LINE-6]]:7 - {{.*}}macro_expand.swift:[[@LINE-6]]:27
392-
// CHECK-DIAGS-NEXT: oa - oa
393-
// CHECK-DIAGS-NEXT: END CONTENTS OF FILE
432+
/*
433+
expected-expansion@-4:7 {{
434+
expected-error@1:4{{binary operator '-' cannot be applied to two 'OnlyAdds' operands}}
435+
}}
436+
*/
394437

395438
_ = #addBlocker({ // expected-note{{in expansion of macro 'addBlocker' here}}
439+
/*
440+
expected-expansion@-2:7 {{
441+
expected-error@9:16{{referencing operator function '-' on 'FloatingPoint' requires that 'OnlyAdds' conform to 'FloatingPoint'}}
442+
}}
443+
*/
396444

397445
print("hello")
398446
print(oa + oa) // expected-error{{blocked an add; did you mean to subtract? (from macro 'addBlocker')}}
@@ -403,7 +451,13 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
403451

404452
// Check recursion.
405453
#recurse(false) // okay
406-
#recurse(true) // expected-note{{in expansion of macro 'recurse' here}}
454+
#recurse(true)
455+
/*
456+
expected-expansion@-2:3 {{
457+
expected-error@1:1{{recursive expansion of macro 'recurse'}}
458+
}}
459+
expected-note@-5{{in expansion of macro 'recurse' here}}
460+
*/
407461
#endif
408462
}
409463

@@ -494,6 +548,14 @@ func testFreestandingMacroExpansion() {
494548
struct Foo3 {
495549
#bitwidthNumberedStructs("BUG", blah: false)
496550
// expected-note@-1 4{{in expansion of macro 'bitwidthNumberedStructs' here}}
551+
/*
552+
expected-expansion@-3:5 {{
553+
expected-error@3:14{{unexpected non-void return value in void function}}
554+
expected-note@3:14{{did you mean to add a return type?}}
555+
expected-error@6:14{{unexpected non-void return value in void function}}
556+
expected-note@6:14{{did you mean to add a return type?}}
557+
}}
558+
*/
497559
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX{{.*}}_23bitwidthNumberedStructsfMf_.swift
498560
// CHECK-DIAGS: struct BUG {
499561
// CHECK-DIAGS: func $s9MacroUser0023macro_expandswift_elFCffMX{{.*}}_23bitwidthNumberedStructsfMf_6methodfMu_()
@@ -727,6 +789,11 @@ struct ABIAttrWithFreestandingMacro1 {
727789
@abi(#varValue)
728790
#varValue
729791
// expected-note@-1 {{in expansion of macro 'varValue' here}}
792+
/*
793+
expected-expansion@-3:3 {{
794+
expected-error@2:6{{cannot use pound literal in '@abi'}}
795+
}}
796+
*/
730797
}
731798

732799
struct ABIAttrWithFreestandingMacro2 {
@@ -759,7 +826,11 @@ func invalidDeclarationMacro2() {
759826
func f() {
760827
#accidentalCodeItem
761828
// expected-note@-1 {{in expansion of macro 'accidentalCodeItem' here}}
762-
// CHECK-DIAGS: @__swiftmacro_9MacroUser0023macro_expandswift_elFCffMX[[@LINE-3]]_6_18accidentalCodeItemfMf_.swift:1:1: error: expected macro expansion to produce a declaration
829+
/*
830+
expected-expansion@-3:7 {{
831+
expected-error@1:1{{expected macro expansion to produce a declaration}}
832+
}}
833+
*/
763834
}
764835
}
765836
}

0 commit comments

Comments
 (0)