@@ -1015,80 +1015,13 @@ namespace {
10151015 }
10161016
10171017 Type visitNilLiteralExpr (NilLiteralExpr *expr) {
1018- auto &DE = CS.getASTContext ().Diags ;
1019- // If this is a standalone `nil` literal expression e.g.
1020- // `_ = nil`, let's diagnose it here because solver can't
1021- // attempt any types for it.
1022- auto *parentExpr = CS.getParentExpr (expr);
1023- bool hasContextualType = bool (CS.getContextualType (expr));
1024-
1025- while (parentExpr) {
1026- if (!isa<IdentityExpr>(parentExpr))
1027- break ;
1028-
1029- // If there is a parent, use it, otherwise we need
1030- // to check whether the last parent node in the chain
1031- // had a contextual type associated with it because
1032- // in situations like:
1033- //
1034- // \code
1035- // func foo() -> Int? {
1036- // return (nil)
1037- // }
1038- // \endcode
1039- //
1040- // parentheses around `nil` are significant.
1041- if (auto *nextParent = CS.getParentExpr (parentExpr)) {
1042- parentExpr = nextParent;
1043- } else {
1044- hasContextualType |= bool (CS.getContextualType (parentExpr));
1045- // Since current expression is an identity expr
1046- // and there are no more parents, let's pretend
1047- // that `nil` don't have a parent since parens
1048- // are not semantically significant for further checks.
1049- parentExpr = nullptr ;
1050- }
1051- }
1052-
1053- // In cases like `_ = nil?` AST would have `nil`
1054- // wrapped in `BindOptionalExpr`.
1055- if (parentExpr && isa<BindOptionalExpr>(parentExpr))
1056- parentExpr = CS.getParentExpr (parentExpr);
1057-
1058- if (parentExpr) {
1059- // `_ = nil as? ...`
1060- if (isa<ConditionalCheckedCastExpr>(parentExpr)) {
1061- DE.diagnose (expr->getLoc (), diag::conditional_cast_from_nil);
1062- return Type ();
1063- }
1064-
1065- // `_ = nil!`
1066- if (isa<ForceValueExpr>(parentExpr)) {
1067- DE.diagnose (expr->getLoc (), diag::cannot_force_unwrap_nil_literal);
1068- return Type ();
1069- }
1070-
1071- // `_ = nil?`
1072- if (isa<OptionalEvaluationExpr>(parentExpr)) {
1073- DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1074- return Type ();
1075- }
1018+ auto literalTy = visitLiteralExpr (expr);
1019+ // Allow `nil` to be a hole so we can diagnose it via a fix
1020+ // if it turns out that there is no contextual information.
1021+ if (auto *typeVar = literalTy->getAs <TypeVariableType>())
1022+ CS.recordPotentialHole (typeVar);
10761023
1077- // `_ = nil`
1078- if (auto *assignment = dyn_cast<AssignExpr>(parentExpr)) {
1079- if (isa<DiscardAssignmentExpr>(assignment->getDest ())) {
1080- DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1081- return Type ();
1082- }
1083- }
1084- }
1085-
1086- if (!parentExpr && !hasContextualType) {
1087- DE.diagnose (expr->getLoc (), diag::unresolved_nil_literal);
1088- return Type ();
1089- }
1090-
1091- return visitLiteralExpr (expr);
1024+ return literalTy;
10921025 }
10931026
10941027 Type visitFloatLiteralExpr (FloatLiteralExpr *expr) {
@@ -3042,11 +2975,19 @@ namespace {
30422975 TVO_PrefersSubtypeBinding |
30432976 TVO_CanBindToLValue |
30442977 TVO_CanBindToNoEscape);
3045-
2978+
2979+ auto *valueExpr = expr->getSubExpr ();
2980+ // It's invalid to force unwrap `nil` literal e.g. `_ = nil!` or
2981+ // `_ = (try nil)!` and similar constructs.
2982+ if (auto *nilLiteral = dyn_cast<NilLiteralExpr>(
2983+ valueExpr->getSemanticsProvidingExpr ())) {
2984+ CS.recordFix (SpecifyContextualTypeForNil::create (
2985+ CS, CS.getConstraintLocator (nilLiteral)));
2986+ }
2987+
30462988 // The result is the object type of the optional subexpression.
3047- CS.addConstraint (ConstraintKind::OptionalObject,
3048- CS.getType (expr->getSubExpr ()), objectTy,
3049- locator);
2989+ CS.addConstraint (ConstraintKind::OptionalObject, CS.getType (valueExpr),
2990+ objectTy, locator);
30502991 return objectTy;
30512992 }
30522993
0 commit comments