Skip to content

Commit 391c6e0

Browse files
committed
[Compiler] add 'value' label for NSNumber to prevent roundabout error message.
1 parent d8780d3 commit 391c6e0

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,8 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
11821182
}
11831183
}
11841184

1185-
bool needsParensInside = exprNeedsParensBeforeAddingAs(anchor);
1186-
bool needsParensOutside = exprNeedsParensAfterAddingAs(anchor);
1185+
bool needsParensInside = exprNeedsParensBeforeAddingAs(anchor, DC);
1186+
bool needsParensOutside = exprNeedsParensAfterAddingAs(anchor, DC);
11871187

11881188
llvm::SmallString<2> insertBefore;
11891189
llvm::SmallString<32> insertAfter;
@@ -2940,6 +2940,30 @@ bool ContextualFailure::tryIntegerCastFixIts(
29402940
}
29412941
}
29422942

2943+
// bridge to prevent roundabout error message
2944+
// See rdar://problem/82828226
2945+
if (TypeChecker::isObjCBridgedTo(fromType, toType, getDC())) {
2946+
auto *ac = castToExpr(getAnchor());
2947+
bool needsParensInside = exprNeedsParensBeforeAddingAs(ac, getDC());
2948+
bool needsParensOutside = exprNeedsParensAfterAddingAs(ac, getDC());
2949+
llvm::SmallString<2> insertBefore;
2950+
llvm::SmallString<32> insertAfter;
2951+
if (needsParensOutside) {
2952+
insertBefore += "(";
2953+
}
2954+
if (needsParensInside) {
2955+
insertBefore += "(";
2956+
insertAfter += ")";
2957+
}
2958+
insertAfter += " as ";
2959+
insertAfter += toType->getWithoutParens()->getString();
2960+
if (needsParensOutside)
2961+
insertAfter += ")";
2962+
diagnostic.fixItInsert(exprRange.Start, insertBefore);
2963+
diagnostic.fixItInsertAfter(exprRange.End, insertAfter);
2964+
return true;
2965+
}
2966+
29432967
// Add a wrapping integer cast.
29442968
std::string convWrapBefore = toType.getString();
29452969
convWrapBefore += "(";

lib/Sema/CSDiagnostics.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,29 @@ class ContextualFailure : public FailureDiagnostic {
701701

702702
static Optional<Diag<Type, Type>>
703703
getDiagnosticFor(ContextualTypePurpose context, Type contextualType);
704+
705+
protected:
706+
bool exprNeedsParensBeforeAddingAs(const Expr *expr, DeclContext *DC) const {
707+
auto asPG = TypeChecker::lookupPrecedenceGroup(
708+
DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc())
709+
.getSingle();
710+
if (!asPG)
711+
return true;
712+
return exprNeedsParensInsideFollowingOperator(DC, const_cast<Expr *>(expr),
713+
asPG);
714+
}
715+
716+
bool exprNeedsParensAfterAddingAs(const Expr *expr, DeclContext *DC) const {
717+
auto asPG = TypeChecker::lookupPrecedenceGroup(
718+
DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc())
719+
.getSingle();
720+
if (!asPG)
721+
return true;
722+
723+
return exprNeedsParensOutsideFollowingOperator(
724+
DC, const_cast<Expr *>(expr), asPG,
725+
[&](auto *E) { return findParentExpr(E); });
726+
}
704727
};
705728

706729
/// Diagnose errors related to using an array literal where a
@@ -891,29 +914,6 @@ class MissingExplicitConversionFailure final : public ContextualFailure {
891914
ASTNode getAnchor() const override;
892915

893916
bool diagnoseAsError() override;
894-
895-
private:
896-
bool exprNeedsParensBeforeAddingAs(const Expr *expr) {
897-
auto *DC = getDC();
898-
auto asPG = TypeChecker::lookupPrecedenceGroup(
899-
DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc()).getSingle();
900-
if (!asPG)
901-
return true;
902-
return exprNeedsParensInsideFollowingOperator(DC, const_cast<Expr *>(expr),
903-
asPG);
904-
}
905-
906-
bool exprNeedsParensAfterAddingAs(const Expr *expr) {
907-
auto *DC = getDC();
908-
auto asPG = TypeChecker::lookupPrecedenceGroup(
909-
DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc()).getSingle();
910-
if (!asPG)
911-
return true;
912-
913-
return exprNeedsParensOutsideFollowingOperator(
914-
DC, const_cast<Expr *>(expr), asPG,
915-
[&](auto *E) { return findParentExpr(E); });
916-
}
917917
};
918918

919919
/// Diagnose failures related to passing value of some type

test/Constraints/rdar82828226.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// REQUIRES: objc_interop
4+
5+
import Foundation
6+
7+
let int: Int = 0
8+
func a(_ a: NSNumber) {}
9+
a(int)// expected-error {{cannot convert value of type 'Int' to expected argument type 'NSNumber'}} {{6-6= as NSNumber}}

0 commit comments

Comments
 (0)