mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Sema] Don't suggest fixits that try to force unwrap implicit $match
Check if a variable is actually spelled out in code before suggesting a fixit that adds a force unwrap to it This fixes SR-1827
This commit is contained in:
@@ -2009,8 +2009,21 @@ commit_to_conversions:
|
||||
// If we have an optional type, try to force-unwrap it.
|
||||
// FIXME: Should we also try '?'?
|
||||
if (objectType1->getOptionalObjectType()) {
|
||||
bool forceUnwrapPossible = true;
|
||||
if (auto declRefExpr =
|
||||
dyn_cast_or_null<DeclRefExpr>(locator.trySimplifyToExpr())) {
|
||||
if (declRefExpr->getDecl()->isImplicit()) {
|
||||
// The expression that provides the first type is implicit and never
|
||||
// spelled out in source code, e.g. $match in an expression pattern.
|
||||
// Thus we cannot force unwrap the first type
|
||||
forceUnwrapPossible = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (forceUnwrapPossible) {
|
||||
conversionsOrFixes.push_back(FixKind::ForceOptional);
|
||||
}
|
||||
}
|
||||
|
||||
// If we have a value of type AnyObject that we're trying to convert to
|
||||
// a class, force a downcast.
|
||||
|
||||
@@ -2150,6 +2150,8 @@ bool TypeChecker::typeCheckExprPattern(ExprPattern *EP, DeclContext *DC,
|
||||
Context.getIdentifier("$match"),
|
||||
rhsType,
|
||||
DC);
|
||||
|
||||
matchVar->setImplicit();
|
||||
EP->setMatchVar(matchVar);
|
||||
matchVar->setHasNonPatternBindingInit();
|
||||
|
||||
|
||||
@@ -120,3 +120,18 @@ switch optionalSel {
|
||||
case #selector(C1.method1)?:
|
||||
break
|
||||
}
|
||||
|
||||
@objc class SR1827 {
|
||||
func bar() {}
|
||||
}
|
||||
|
||||
switch optionalSel {
|
||||
case #selector(SR1827.bar): // expected-error{{expression pattern of type 'Selector' cannot match values of type 'Selector?'}}
|
||||
break
|
||||
case #selector(SR1827.bar)!: // expected-error{{cannot force unwrap value of non-optional type 'Selector'}}
|
||||
break
|
||||
case #selector(SR1827.bar)?:
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user