[Trailing closures] Diagnose the change in behavior with SE-0268.

SE-0248 changes the backward-scan matching behavior for the unlabeled
trailing closure into a forward scan. In circumstances where this
could silently change the meaning of a call to a particular
function, i.e., when there are two defaulted closure parameters such
that a given closure to match either one of them, produce an warning
that describes the change in behavior. For example:

    t4.swift:2:24: warning: since Swift 5.3, unlabeled trailing
closure argument matches parameter 'x' rather than parameter 'z'
    trailingClosureSingle2 { $0 }
                           ^
    t4.swift:2:24: note: label the argument with 'z' to retain the
pre-Swift 5.3 behavior
    trailingClosureSingle2 { $0 }
                           ^
                          (z:    )
    t4.swift:2:24: note: label the argument with 'x' to silence this
warning for Swift 5.3 and newer
    trailingClosureSingle2 { $0 }
                           ^
                          (x:    )
    t4.swift:1:6: note: 'trailingClosureSingle2(x:y:z:)' contains
defaulted closure parameters 'x' and 'z'
    func trailingClosureSingle2(x: (Int) -> Int = { $0 } , y: (Int) ->
Int = { $0 }, z: (Int) -> Int = { $0 }) {}
         ^                      ~

This explains the (rare) case where SE-0286 silently changes the
meaning of a program, offering Fix-Its to either restore the
pre-SE-0286 behavior or silence the warning, as appropriate.
This commit is contained in:
Doug Gregor
2020-07-18 00:33:57 -07:00
parent 1756979d8c
commit c6670250e1
5 changed files with 316 additions and 1 deletions

View File

@@ -196,7 +196,7 @@ static ConstraintSystem::TypeMatchOptions getDefaultDecompositionOptions(
}
/// Whether the given parameter requires an argument.
static bool parameterRequiresArgument(
bool swift::parameterRequiresArgument(
ArrayRef<AnyFunctionType::Param> params,
const ParameterListInfo &paramInfo,
unsigned paramIdx) {