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-11865] Constant folding of Int.dividedReportingOverflow(by:) produces divide by zero error #54279

Open
spevans opened this issue Nov 27, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@spevans
Copy link
Collaborator

spevans commented Nov 27, 2019

Previous ID SR-11865
Radar rdar://problem/57510224
Original Reporter @spevans
Type Bug
Environment

Linux ubuntu18.04

swift-DEVELOPMENT-SNAPSHOT-2019-11-26-a-ubuntu18.04

macOS 10.14.6

Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)

Target: x86_64-apple-darwin18.7.0

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

md5: 676c169994993b20be4fcd51d6ae65ff

Issue Description:

Int.dividedReportingOverflow(by: x) should return (x, true) when x = 0 however under some conditions the compiler fails with an error: division by zero:

$ cat divide1.swift
print(1.dividedReportingOverflow(by:0))

$ swift divide1.swift
divide1.swift:1:9: error: division by zero
print(1.dividedReportingOverflow(by:0))
        ^

$ cat divide2.swift
let x = 1
print(x.dividedReportingOverflow(by: 0))

$ swift divide2.swift
divide2.swift:2:9: error: division by zero
print(x.dividedReportingOverflow(by: 0))
        ^

If the divisor is not constant it works correctly

$ cat divide3.swift
let x = 0
print(1.dividedReportingOverflow(by: x))

$ swift divide3.swift
(partialValue: 1, overflow: true)

The same occurs for remainderReportingOverflow(dividingBy🙂

Thanks to @gwynne for determining it was a compiler issue.

@typesanitizer
Copy link

Thanks for reporting the issue!

@swift-ci create

@NevinBR
Copy link
Contributor

NevinBR commented Apr 9, 2021

I believe this is the same as SR-3535 and SR-5964

And I believe a valid solution would be to make “Division by zero” (and in general, any arithmetic overflow) not be a compile-time error at all.

• • •

Swift has taken a principled stance that “trapping is safe”. Swift programs trap on overflow, on out-of-bounds, and on precondition failure. There is even a dedicated function fatalError() in the standard library whose entire purpose is to trap.

Programmers are allowed, and in many cases expected and encouraged, to write code that traps. The fact that a line of code will trap is, therefore, absolutely and emphatically not cause to prevent compilation.

If a programmer writes code that will always trap (such as fatalError() or division by zero) it is self-evident that they want the program to crash if execution reaches that line. This is a normal and standard practice in Swift.

Therefore, arithmetic overflow should not be a compile-time error at all. In fact, it should probably not even be a warning. Because trapping is safe.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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
Projects
None yet
Development

No branches or pull requests

3 participants