Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-9609] Float80.init(_ other: Double) is called on passing float literal to Float80.init(_:) #52055

Closed
norio-nomura opened this issue Jan 7, 2019 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@norio-nomura
Copy link
Contributor

Previous ID SR-9609
Radar None
Original Reporter @norio-nomura
Type Bug
Status Resolved
Resolution Invalid
Environment

swift-4.2.1-RELEASE
swift-DEVELOPMENT-SNAPSHOT-2019-01-05-a
swift-5.0-DEVELOPMENT-SNAPSHOT-2019-01-04-a

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: c995a995369ba536b9298a0306bfdc5f

Issue Description:

$ docker run --privileged -it --rm norionomura/swift:20190105a swift
Welcome to Swift version 5.0-dev (LLVM f63b283c71, Clang 41ac4c4262, Swift bbfc0649ed).
Type :help for assistance.
  1> import Foundation 
  2. let a = Float80.init(6.8523015e+5) 
  3. let b = 6.8523015e+5 as Float80 
  4. print(a) 
  5. print(b) 
  6. print(a == b)
685230.1500000000233
685230.15
false
a: Float80 = 685230.150000000023283
b: Float80 = 685230.149999999999977
  7>  

ast:

$ docker run --privileged -it --rm norionomura/swift:20190105a
root@92211f3075d3:/# echo "import Foundation; _ = Float80.init(6.8523015e+5)"|swift -frontend -dump-ast -
(source_file "<stdin>"
  (import_decl range=[<stdin>:1:1 - line:1:8] trailing_semi 'Foundation')
  (top_level_code_decl range=[<stdin>:1:20 - line:1:49]
    (brace_stmt range=[<stdin>:1:20 - line:1:49]
      (assign_expr type='()' location=<stdin>:1:22 range=[<stdin>:1:20 - line:1:49]
        (discard_assignment_expr type='@lvalue Float80' location=<stdin>:1:20 range=[<stdin>:1:20 - line:1:20])
        (call_expr type='Float80' location=<stdin>:1:32 range=[<stdin>:1:24 - line:1:49] nothrow arg_labels=_:
          (constructor_ref_call_expr type='(Double) -> Float80' location=<stdin>:1:32 range=[<stdin>:1:24 - line:1:32] nothrow
            (declref_expr type='(Float80.Type) -> (Double) -> Float80' location=<stdin>:1:32 range=[<stdin>:1:32 - line:1:32] decl=Swift.(file).Float80 extension.init(_:) function_ref=double)
            (type_expr type='Float80.Type' location=<stdin>:1:24 range=[<stdin>:1:24 - line:1:24] typerepr='Float80'))
          (paren_expr type='(Double)' location=<stdin>:1:37 range=[<stdin>:1:36 - line:1:49]
            (call_expr implicit type='Double' location=<stdin>:1:37 range=[<stdin>:1:37 - line:1:37] nothrow arg_labels=_builtinFloatLiteral:
              (constructor_ref_call_expr implicit type='(FPIEEE80) -> Double' location=<stdin>:1:37 range=[<stdin>:1:37 - line:1:37] nothrow
                (declref_expr implicit type='(Double.Type) -> (FPIEEE80) -> Double' location=<stdin>:1:37 range=[<stdin>:1:37 - line:1:37] decl=Swift.(file).Double extension.init(_builtinFloatLiteral:) function_ref=single)
                (type_expr implicit type='Double.Type' location=<stdin>:1:37 range=[<stdin>:1:37 - line:1:37] typerepr='Double'))
              (tuple_expr implicit type='(_builtinFloatLiteral: FPIEEE80)' location=<stdin>:1:37 range=[<stdin>:1:37 - line:1:37] names=_builtinFloatLiteral
                (float_literal_expr type='FPIEEE80' location=<stdin>:1:37 range=[<stdin>:1:37 - line:1:37] value=6.8523015e+5)))))))))
@belkadan
Copy link
Contributor

belkadan commented Jan 7, 2019

This is correct behavior. Per SE-0213, construction syntax is only treated as equivalent to as when you just use the name of the type; adding init indicates that you really do want to call an initializer.

@norio-nomura
Copy link
Contributor Author

Hm, that's strange. Our code didn't use init, but that was affected by this issue.
jpsim/Yams@e829e15

@belkadan
Copy link
Contributor

belkadan commented Jan 8, 2019

SE-0213 is new behavior in Swift 5; prior to that, it was always construction syntax and never equivalent to as.

@norio-nomura
Copy link
Contributor Author

Ah, I confirmed that Swift 5.0 is working as described in SE-0213 and Swift 4.2.1 is not.
Thanks! 🙂

$ swift
swift_oss_helper command enabled.
Welcome to Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1). Type :help for assistance.
  1> import Foundation 
  2. let a = Float80(6.8523015e+5) 
  3. let b = 6.8523015e+5 as Float80 
  4. print(a) 
  5. print(b) 
  6. print(a == b)
685230.1500000000233
685230.15
false
a: Float80 = 685230.150000000023283
b: Float80 = 685230.149999999999977
  7> ^D
$ xcrun --toolchain org.swift.5020190104a swift
swift_oss_helper command enabled.
Welcome to Apple Swift version 5.0-dev (LLVM c351ac7354, Clang 2b49a6d7dc, Swift 559f45ee8e).
Type :help for assistance.
  1> import Foundation 
  2. let a = Float80(6.8523015e+5) 
  3. let b = 6.8523015e+5 as Float80 
  4. print(a) 
  5. print(b) 
  6. print(a == b)
685230.15
685230.15
true
a: Float80 = 685230.149999999999977
b: Float80 = 685230.149999999999977

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants