Overload resolution behavior of ==
permits type inference to "ignore" default float literal type if Foundation is imported
#78365
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
triage needed
This issue needs more specific labels
Description
As we all know very well, in binary floating-point math,
0.1 + 0.2 != 0.3
.And as users are taught and rightly expect, in the absence of other type context, a float literal defaults to
FloatLiteralType
(akaDouble
, unless shadowed).Therefore, the following code shows the expected behavior:
However:
Foundation defines a
Decimal
type that conforms toExpressibleByFloatLiteral
, in which (rightly)0.1 + 0.2 == 0.3 as Decimal
.On Apple platforms, Foundation also defines a
RunLoop.SchedulerTimeType.Stride
type which shockingly also conforms toExpressibleByFloatLiteral
.For...reasons, importing Foundation (without ever touching
Decimal
) breaks user expectations:Here (unless I'm mistaken), Foundation isn't getting any special treatment that a third-party library wouldn't get. And a third-party library shouldn't be able to break user code that uses float literals without further type context, or worse yet silently change how it executes.
Reproduction
Expected behavior
Regardless of what libraries are imported, as long as the user isn't shadowing
FloatLiteralType
, the expressionsum([0.1, 0.2]) == 0.3
should always compile and evaluate tofalse
.Environment
All versions from at least Swift 4.2 onwards (checked on godbolt.org)
Additional information
Based on the question raised on the Swift Forums in:
https://forums.swift.org/t/why-isnt-the-typesolver-unable-to-resolve-this-without-the-implicit-assignment-first/76852
The text was updated successfully, but these errors were encountered: