@@ -6945,3 +6945,50 @@ bool InvalidEmptyKeyPathFailure::diagnoseAsError() {
69456945 emitDiagnostic (diag::expr_swift_keypath_empty);
69466946 return true ;
69476947}
6948+
6949+ bool MissingContextualTypeForNil::diagnoseAsError () {
6950+ auto *expr = castToExpr<NilLiteralExpr>(getAnchor ());
6951+
6952+ // If this is a standalone `nil` literal expression e.g.
6953+ // `_ = nil`, let's diagnose it here because solver can't
6954+ // attempt any types for it.
6955+ auto *parentExpr = findParentExpr (expr);
6956+
6957+ while (parentExpr && isa<IdentityExpr>(parentExpr))
6958+ parentExpr = findParentExpr (parentExpr);
6959+
6960+ // In cases like `_ = nil?` AST would have `nil`
6961+ // wrapped in `BindOptionalExpr`.
6962+ if (parentExpr && isa<BindOptionalExpr>(parentExpr))
6963+ parentExpr = findParentExpr (parentExpr);
6964+
6965+ if (parentExpr) {
6966+ // `_ = nil as? ...`
6967+ if (isa<ConditionalCheckedCastExpr>(parentExpr)) {
6968+ emitDiagnostic (diag::conditional_cast_from_nil);
6969+ return true ;
6970+ }
6971+
6972+ // `_ = nil!`
6973+ if (isa<ForceValueExpr>(parentExpr)) {
6974+ emitDiagnostic (diag::cannot_force_unwrap_nil_literal);
6975+ return true ;
6976+ }
6977+
6978+ // `_ = nil?`
6979+ if (isa<OptionalEvaluationExpr>(parentExpr)) {
6980+ emitDiagnostic (diag::unresolved_nil_literal);
6981+ return true ;
6982+ }
6983+ // `_ = nil`
6984+ if (auto *assignment = dyn_cast<AssignExpr>(parentExpr)) {
6985+ if (isa<DiscardAssignmentExpr>(assignment->getDest ())) {
6986+ emitDiagnostic (diag::unresolved_nil_literal);
6987+ return true ;
6988+ }
6989+ }
6990+ }
6991+
6992+ emitDiagnostic (diag::unresolved_nil_literal);
6993+ return true ;
6994+ }
0 commit comments