File: diag_mismatched_magic_literals.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 (211 lines) | stat: -rw-r--r-- 8,123 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-concise-pound-file

// The test cases in this file work the same in both Swift 5 and "Swift 6" mode.
// See the _swift5 and _swift6 files for version-specific test cases.

func callee(file: String = #file) {} // expected-note {{'file' declared here}}
func callee(optFile: String? = #file) {} // expected-note {{'optFile' declared here}}
func callee(fileID: String = #fileID) {} // expected-note {{'fileID' declared here}}
func callee(filePath: String = #filePath) {} // expected-note {{'filePath' declared here}}
func callee(arbitrary: String) {}

class SomeClass {
  static func callee(file: String = #file) {} // expected-note 2{{'file' declared here}}
  static func callee(optFile: String? = #file) {} // expected-note {{'optFile' declared here}}
  static func callee(arbitrary: String) {}

  func callee(file: String = #file) {} // expected-note 2{{'file' declared here}}
  func callee(optFile: String? = #file) {} // expected-note {{'optFile' declared here}}
  func callee(arbitrary: String) {}
}

//
// Basic functionality
//

// We should warn when we we pass a `#function`-defaulted argument to a
// `#file`-defaulted argument.
func bad(function: String = #function) {
  // expected-note@-1 3{{did you mean for parameter 'function' to default to '#file'?}} {{29-38=#file}}
  // expected-note@-2 {{did you mean for parameter 'function' to default to '#fileID'?}} {{29-38=#fileID}}
  // expected-note@-3 {{did you mean for parameter 'function' to default to '#filePath'?}} {{29-38=#filePath}}

  callee(file: function)
  // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}}
  // expected-note@-2 {{add parentheses to silence this warning}} {{16-16=(}} {{24-24=)}}

  SomeClass.callee(file: function)
  // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}}
  // expected-note@-2 {{add parentheses to silence this warning}} {{26-26=(}} {{34-34=)}}

  SomeClass().callee(file: function)
  // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}}
  // expected-note@-2 {{add parentheses to silence this warning}} {{28-28=(}} {{36-36=)}}

  callee(fileID: function)
  // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'fileID', whose default argument is '#fileID'}}
  // expected-note@-2 {{add parentheses to silence this warning}} {{18-18=(}} {{26-26=)}}

  callee(filePath: function)
  // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'filePath', whose default argument is '#filePath'}}
  // expected-note@-2 {{add parentheses to silence this warning}} {{20-20=(}} {{28-28=)}}
}

// We should not warn when we pass a `#file`-defaulted argument to a
// `#file`-defaulted argument.
func good(file: String = #file, fileID: String = #fileID, filePath: String = #filePath) {
  callee(file: file)

  SomeClass.callee(file: file)

  SomeClass().callee(file: file)

  callee(fileID: fileID)

  callee(filePath: filePath)
}

// We should not warn when we surround the `#function`-defaulted argument
// with parentheses, which explicitly silences the warning.
func disabled(function: String = #function) {
  callee(file: (function))

  SomeClass.callee(file: (function))

  SomeClass().callee(file: (function))

  callee(fileID: (function))

  callee(filePath: (function))
}

//
// With implicit instance `self`
//

extension SomeClass {
  // We should warn when we we pass a `#function`-defaulted argument to a
  // `#file`-defaulted argument.
  func bad(function: String = #function) {
    // expected-note@-1 1{{did you mean for parameter 'function' to default to '#file'?}} {{31-40=#file}}
    callee(file: function)
    // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}}
    // expected-note@-2 {{add parentheses to silence this warning}} {{18-18=(}} {{26-26=)}}
  }

  // We should not warn when we pass a `#file`-defaulted argument to a
  // `#file`-defaulted argument.
  func good(file: String = #file) {
    callee(file: file)
  }

  // We should not warn when we surround the `#function`-defaulted argument
  // with parentheses, which explicitly silences the warning.
  func disabled(function: String = #function) {
    callee(file: (function))
  }
}

//
// With implicit type `self`
//

extension SomeClass {
  // We should warn when we we pass a `#function`-defaulted argument to a
  // `#file`-defaulted argument.
  static func bad(function: String = #function) {
    // expected-note@-1 1{{did you mean for parameter 'function' to default to '#file'?}} {{38-47=#file}}
    callee(file: function)
    // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}}
    // expected-note@-2 {{add parentheses to silence this warning}} {{18-18=(}} {{26-26=)}}
  }

  // We should not warn when we pass a `#file`-defaulted argument to a
  // `#file`-defaulted argument.
  static func good(file: String = #file) {
    callee(file: file)
  }

  // We should not warn when we surround the `#function`-defaulted argument
  // with parentheses, which explicitly silences the warning.
  static func disabled(function: String = #function) {
    callee(file: (function))
  }
}

//
// Looking through implicit conversions
//
// Same as above, but these lift the argument from `String` to `String?`, so
// the compiler has to look through the implicit conversion.
//

// We should warn when we we pass a `#function`-defaulted argument to a
// `#file`-defaulted argument.
func optionalBad(function: String = #function) {
  // expected-note@-1 3{{did you mean for parameter 'function' to default to '#file'?}} {{37-46=#file}}
  callee(optFile: function)
  // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'optFile', whose default argument is '#file'}}
  // expected-note@-2 {{add parentheses to silence this warning}} {{19-19=(}} {{27-27=)}}

  SomeClass.callee(optFile: function)
  // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'optFile', whose default argument is '#file'}}
  // expected-note@-2 {{add parentheses to silence this warning}} {{29-29=(}} {{37-37=)}}

  SomeClass().callee(optFile: function)
  // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'optFile', whose default argument is '#file'}}
  // expected-note@-2 {{add parentheses to silence this warning}} {{31-31=(}} {{39-39=)}}
}

// We should not warn when we pass a `#file`-defaulted argument to a
// `#file`-defaulted argument.
func optionalGood(file: String = #file) {
  callee(optFile: file)

  SomeClass.callee(optFile: file)

  SomeClass().callee(optFile: file)
}

// We should not warn when we surround the `#function`-defaulted argument
// with parentheses, which explicitly silences the warning.
func optionalDisabled(function: String = #function) {
  callee(optFile: (function))

  SomeClass.callee(optFile: (function))

  SomeClass().callee(optFile: (function))
}

//
// More negative cases
//

// We should not warn if the caller's parameter has no default.
func explicit(arbitrary: String) {
  callee(file: arbitrary)

  SomeClass.callee(file: arbitrary)

  SomeClass().callee(file: arbitrary)
}

// We should not warn if the caller's parameter has a non-magic-identifier
// default.
func empty(arbitrary: String = "") {
  callee(file: arbitrary)

  SomeClass.callee(file: arbitrary)

  SomeClass().callee(file: arbitrary)
}

// We should not warn if the callee's parameter has no default.
func ineligible(function: String = #function) {
  callee(arbitrary: function)

  SomeClass.callee(arbitrary: function)

  SomeClass().callee(arbitrary: function)
}