Skip to content

Commit 434ff12

Browse files
[Sema] Check for extra optionals injected on checked cast subexprs when recording extraneous cast warnings
1 parent c085302 commit 434ff12

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6636,8 +6636,12 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
66366636
}
66376637

66386638
// Except for forced cast expressions, if optionals are more than a single
6639-
// level difference, we don't need to record any fix.
6640-
if (!isExpr<ForcedCheckedCastExpr>(anchor) && extraOptionals > 1)
6639+
// level difference or there is a single level between the types but an extra
6640+
// level of optional is added to subexpr via OptionalEvaluationExpr, we don't
6641+
// need to record any fix.
6642+
if (!isExpr<ForcedCheckedCastExpr>(anchor) &&
6643+
(extraOptionals > 1 ||
6644+
isExpr<OptionalEvaluationExpr>(castExpr->getSubExpr())))
66416645
return nullptr;
66426646

66436647
// Always succeed

test/Constraints/casts.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,17 @@ protocol PP2: PP1 { }
535535
extension Optional: PP1 where Wrapped == PP2 { }
536536

537537
nil is PP1 // expected-error {{'nil' requires a contextual type}}
538+
539+
// SR-15039
540+
enum ChangeType<T> {
541+
case initial(T)
542+
case delta(previous: T, next: T)
543+
case unset
544+
545+
var delta: (previous: T?, next: T)? { nil }
546+
}
547+
548+
extension ChangeType where T == String? {
549+
var foo: String? { return self.delta?.previous as? String } // OK
550+
var bar: String? { self.delta?.next }
551+
}

0 commit comments

Comments
 (0)