mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
When wrapping a function which is supposed to capture the caller’s location, there’s always a risk that the wrapper won’t capture the information the wrapped function wants; for instance, you might pass `(…, line, column)` where the callee expected `(…, column, line)`. This commit emits a warning when a call passes an explicit argument to something that has a default argument, and that explicit argument is itself a parameter with a default argument, and both parameters use magic identifiers, but they use *different* magic identifiers. This is partially in support of concise #file, but applies to all magic identifiers. Fixes rdar://problem/58588633.
75 lines
2.5 KiB
Swift
75 lines
2.5 KiB
Swift
// RUN: %target-swift-frontend -emit-sil -verify -module-name main -primary-file %s %S/Inputs/mismatched-magic-literals-other.swift
|
|
|
|
//
|
|
// 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 {{did you mean for parameter 'function' to default to '#file'?}} {{29-38=#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}} {{16-16=(}} {{24-24=)}}
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
//
|
|
// 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 {{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=)}}
|
|
}
|
|
|
|
// We should not warn when we pass a `#file`-defaulted argument to a
|
|
// `#file`-defaulted argument.
|
|
func optionalGood(file: String = #file) {
|
|
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))
|
|
}
|
|
|
|
//
|
|
// More negative cases
|
|
//
|
|
|
|
// We should not warn if the caller's parameter has no default.
|
|
func explicit(arbitrary: String) {
|
|
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)
|
|
}
|
|
|
|
// We should not warn if the callee's parameter has no default.
|
|
func ineligible(function: String = #function) {
|
|
callee(arbitrary: function)
|
|
}
|