Don't allow an implicit conversion from NSNumber to one of its bridged value types.

Eliminate the implicit conversion from NSNumber to one of its bridged
value types (Int, Float, Double, Bool, etc.). One can use "as" to
perform the conversion instead. This eliminates a class of
accepts-dubious that involve lossy conversions from NSNumber to Int.

Our eventual arc is to eliminate all of these conversions. This is a
small, high-value step along that path rdar://problem/18269449.

Swift SVN r21879
This commit is contained in:
Doug Gregor
2014-09-11 17:12:02 +00:00
parent c3ca1475e1
commit 9f4ddfe2e1
4 changed files with 28 additions and 2 deletions

View File

@@ -1203,6 +1203,24 @@ static bool isStringCompatiblePointerBaseType(TypeChecker &TC,
return false;
}
/// Determine whether the given type is a value type to which we can implicitly
/// bridge a value of its corresponding class type, such as 'String' implicitly
/// bridging from NSString.
static bool allowsImplicitBridgingFromObjC(TypeChecker &tc, DeclContext *dc,
Type type) {
auto objcType = tc.getBridgedToObjC(dc, type);
if (!objcType)
return nullptr;
auto objcClass = objcType->getClassOrBoundGenericClass();
if (!objcClass)
return nullptr;
// FIXME: As a temporary hack, eliminate implicit bridging from NSNumber. We
// want to eventually eliminate all implicit bridging.
return objcClass->getName().str() != "NSNumber";
}
ConstraintSystem::SolutionKind
ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
unsigned flags,
@@ -1687,7 +1705,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
if (type1->mayHaveSuperclass() && type2->isPotentiallyBridgedValueType() &&
type2->getAnyNominal()
!= TC.Context.getImplicitlyUnwrappedOptionalDecl() &&
TC.getBridgedToObjC(DC, type2) &&
allowsImplicitBridgingFromObjC(TC, DC, type2) &&
!HandlingFavoredConstraint) {
conversionsOrFixes.push_back(ConversionRestrictionKind::BridgeFromObjC);
}