|
| 1 | +// RUN: %target-typecheck-verify-swift |
| 2 | + |
| 3 | +func callee(file: String = #file) {} // expected-note {{'file' declared here}} |
| 4 | +func callee(optFile: String? = #file) {} // expected-note {{'optFile' declared here}} |
| 5 | +func callee(arbitrary: String) {} |
| 6 | + |
| 7 | +class SomeClass { |
| 8 | + static func callee(file: String = #file) {} // expected-note 2{{'file' declared here}} |
| 9 | + static func callee(optFile: String? = #file) {} // expected-note {{'optFile' declared here}} |
| 10 | + static func callee(arbitrary: String) {} |
| 11 | + |
| 12 | + func callee(file: String = #file) {} // expected-note 2{{'file' declared here}} |
| 13 | + func callee(optFile: String? = #file) {} // expected-note {{'optFile' declared here}} |
| 14 | + func callee(arbitrary: String) {} |
| 15 | +} |
| 16 | + |
| 17 | +// |
| 18 | +// Basic functionality |
| 19 | +// |
| 20 | + |
| 21 | +// We should warn when we we pass a `#function`-defaulted argument to a |
| 22 | +// `#file`-defaulted argument. |
| 23 | +func bad(function: String = #function) { |
| 24 | + // expected-note@-1 3{{did you mean for parameter 'function' to default to '#file'?}} {{29-38=#file}} |
| 25 | + callee(file: function) |
| 26 | + // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}} |
| 27 | + // expected-note@-2 {{add parentheses to silence this warning}} {{16-16=(}} {{24-24=)}} |
| 28 | + |
| 29 | + SomeClass.callee(file: function) |
| 30 | + // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}} |
| 31 | + // expected-note@-2 {{add parentheses to silence this warning}} {{26-26=(}} {{34-34=)}} |
| 32 | + |
| 33 | + SomeClass().callee(file: function) |
| 34 | + // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}} |
| 35 | + // expected-note@-2 {{add parentheses to silence this warning}} {{28-28=(}} {{36-36=)}} |
| 36 | +} |
| 37 | + |
| 38 | +// We should not warn when we pass a `#file`-defaulted argument to a |
| 39 | +// `#file`-defaulted argument. |
| 40 | +func good(file: String = #file) { |
| 41 | + callee(file: file) |
| 42 | + |
| 43 | + SomeClass.callee(file: file) |
| 44 | + |
| 45 | + SomeClass().callee(file: file) |
| 46 | +} |
| 47 | + |
| 48 | +// We should not warn when we surround the `#function`-defaulted argument |
| 49 | +// with parentheses, which explicitly silences the warning. |
| 50 | +func disabled(function: String = #function) { |
| 51 | + callee(file: (function)) |
| 52 | + |
| 53 | + SomeClass.callee(file: (function)) |
| 54 | + |
| 55 | + SomeClass().callee(file: (function)) |
| 56 | +} |
| 57 | + |
| 58 | +// |
| 59 | +// With implicit instance `self` |
| 60 | +// |
| 61 | + |
| 62 | +extension SomeClass { |
| 63 | + // We should warn when we we pass a `#function`-defaulted argument to a |
| 64 | + // `#file`-defaulted argument. |
| 65 | + func bad(function: String = #function) { |
| 66 | + // expected-note@-1 1{{did you mean for parameter 'function' to default to '#file'?}} {{31-40=#file}} |
| 67 | + callee(file: function) |
| 68 | + // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}} |
| 69 | + // expected-note@-2 {{add parentheses to silence this warning}} {{18-18=(}} {{26-26=)}} |
| 70 | + } |
| 71 | + |
| 72 | + // We should not warn when we pass a `#file`-defaulted argument to a |
| 73 | + // `#file`-defaulted argument. |
| 74 | + func good(file: String = #file) { |
| 75 | + callee(file: file) |
| 76 | + } |
| 77 | + |
| 78 | + // We should not warn when we surround the `#function`-defaulted argument |
| 79 | + // with parentheses, which explicitly silences the warning. |
| 80 | + func disabled(function: String = #function) { |
| 81 | + callee(file: (function)) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// |
| 86 | +// With implicit type `self` |
| 87 | +// |
| 88 | + |
| 89 | +extension SomeClass { |
| 90 | + // We should warn when we we pass a `#function`-defaulted argument to a |
| 91 | + // `#file`-defaulted argument. |
| 92 | + static func bad(function: String = #function) { |
| 93 | + // expected-note@-1 1{{did you mean for parameter 'function' to default to '#file'?}} {{38-47=#file}} |
| 94 | + callee(file: function) |
| 95 | + // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'file', whose default argument is '#file'}} |
| 96 | + // expected-note@-2 {{add parentheses to silence this warning}} {{18-18=(}} {{26-26=)}} |
| 97 | + } |
| 98 | + |
| 99 | + // We should not warn when we pass a `#file`-defaulted argument to a |
| 100 | + // `#file`-defaulted argument. |
| 101 | + static func good(file: String = #file) { |
| 102 | + callee(file: file) |
| 103 | + } |
| 104 | + |
| 105 | + // We should not warn when we surround the `#function`-defaulted argument |
| 106 | + // with parentheses, which explicitly silences the warning. |
| 107 | + static func disabled(function: String = #function) { |
| 108 | + callee(file: (function)) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// |
| 113 | +// Looking through implicit conversions |
| 114 | +// |
| 115 | +// Same as above, but these lift the argument from `String` to `String?`, so |
| 116 | +// the compiler has to look through the implicit conversion. |
| 117 | +// |
| 118 | + |
| 119 | +// We should warn when we we pass a `#function`-defaulted argument to a |
| 120 | +// `#file`-defaulted argument. |
| 121 | +func optionalBad(function: String = #function) { |
| 122 | + // expected-note@-1 3{{did you mean for parameter 'function' to default to '#file'?}} {{37-46=#file}} |
| 123 | + callee(optFile: function) |
| 124 | + // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'optFile', whose default argument is '#file'}} |
| 125 | + // expected-note@-2 {{add parentheses to silence this warning}} {{19-19=(}} {{27-27=)}} |
| 126 | + |
| 127 | + SomeClass.callee(optFile: function) |
| 128 | + // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'optFile', whose default argument is '#file'}} |
| 129 | + // expected-note@-2 {{add parentheses to silence this warning}} {{29-29=(}} {{37-37=)}} |
| 130 | + |
| 131 | + SomeClass().callee(optFile: function) |
| 132 | + // expected-warning@-1 {{parameter 'function' with default argument '#function' passed to parameter 'optFile', whose default argument is '#file'}} |
| 133 | + // expected-note@-2 {{add parentheses to silence this warning}} {{31-31=(}} {{39-39=)}} |
| 134 | +} |
| 135 | + |
| 136 | +// We should not warn when we pass a `#file`-defaulted argument to a |
| 137 | +// `#file`-defaulted argument. |
| 138 | +func optionalGood(file: String = #file) { |
| 139 | + callee(optFile: file) |
| 140 | + |
| 141 | + SomeClass.callee(optFile: file) |
| 142 | + |
| 143 | + SomeClass().callee(optFile: file) |
| 144 | +} |
| 145 | + |
| 146 | +// We should not warn when we surround the `#function`-defaulted argument |
| 147 | +// with parentheses, which explicitly silences the warning. |
| 148 | +func optionalDisabled(function: String = #function) { |
| 149 | + callee(optFile: (function)) |
| 150 | + |
| 151 | + SomeClass.callee(optFile: (function)) |
| 152 | + |
| 153 | + SomeClass().callee(optFile: (function)) |
| 154 | +} |
| 155 | + |
| 156 | +// |
| 157 | +// More negative cases |
| 158 | +// |
| 159 | + |
| 160 | +// We should not warn if the caller's parameter has no default. |
| 161 | +func explicit(arbitrary: String) { |
| 162 | + callee(file: arbitrary) |
| 163 | + |
| 164 | + SomeClass.callee(file: arbitrary) |
| 165 | + |
| 166 | + SomeClass().callee(file: arbitrary) |
| 167 | +} |
| 168 | + |
| 169 | +// We should not warn if the caller's parameter has a non-magic-identifier |
| 170 | +// default. |
| 171 | +func empty(arbitrary: String = "") { |
| 172 | + callee(file: arbitrary) |
| 173 | + |
| 174 | + SomeClass.callee(file: arbitrary) |
| 175 | + |
| 176 | + SomeClass().callee(file: arbitrary) |
| 177 | +} |
| 178 | + |
| 179 | +// We should not warn if the callee's parameter has no default. |
| 180 | +func ineligible(function: String = #function) { |
| 181 | + callee(arbitrary: function) |
| 182 | + |
| 183 | + SomeClass.callee(arbitrary: function) |
| 184 | + |
| 185 | + SomeClass().callee(arbitrary: function) |
| 186 | +} |
0 commit comments