File: attached_macros_diags.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (72 lines) | stat: -rw-r--r-- 2,714 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// REQUIRES: swift_swift_parser

// RUN: %empty-directory(%t)
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -disable-availability-checking -module-name MacrosTest

@attached(peer) macro m1() = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")

@attached(peer) macro m2(_: Int) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
// expected-note@-1{{candidate has partially matching parameter list (Int)}}
// expected-note@-2{{candidate expects value of type 'Int' for parameter #1 (got 'String')}}

@attached(peer) macro m2(_: Double) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")
// expected-note@-1{{candidate has partially matching parameter list (Double)}}
// expected-note@-2{{candidate expects value of type 'Double' for parameter #1 (got 'String')}}

@attached(peer) macro m3(message: String) = #externalMacro(module: "MacroDefinition", type: "EmptyPeerMacro")

@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MyMacros", type: "StringifyMacro")
// expected-warning@-1{{external macro implementation type 'MyMacros.StringifyMacro' could not be found for macro 'stringify'}}
// expected-note@-2{{'stringify' declared here}}

@m1 struct X1 { }

@m2 struct X2 { } // expected-error{{no exact matches in call to macro 'm2'}}

// Check for nesting rules.
struct SkipNestedType {
  @propertyWrapper
  struct m1<T> {
    init() { }

    var wrappedValue: T
  }

  // We select the macro, not the property wrapper.
  @m1 var x: Int = 0
  //expected-note@-1{{did you mean 'x'?}}

  func test() {
    let _: m1<Int> = _x
    // expected-error@-1{{cannot find '_x' in scope}}
  }
}

struct TestMacroArgs {
  @m1("extra arg") struct Args1 {} // expected-error{{argument passed to macro expansion that takes no arguments}}

  @m2(10) struct Args2 {}

  @m2(10.0) struct Args3 {}

  @m2("") struct Args4 {} // expected-error{{no exact matches in call to macro 'm2'}}

  @m2(Nested.x) struct Args5 {}

  struct Nested {
    static let x = 10

    @m2(x) struct Args1 {}

    @m2(Nested.x) struct Args2 {}
  }

  @m3(message: stringify(Nested.x).1) struct Args6 {}
  // expected-error@-1{{expansion of macro 'stringify' requires leading '#'}}

  @m3(message: #stringify().1) struct Args7 {}
  // expected-error@-1{{missing argument for parameter #1 in macro expansion}}

  @m3(message: #stringify(Nested.x).1) struct Args8 {}
}