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-15194] Warn about integer divisions where abs(constant numerator) < abs(constant denominator) #57516

Open
beccadax opened this issue Sep 14, 2021 · 1 comment
Labels
compiler The Swift compiler in itself new feature type checker Area → compiler: Semantic analysis

Comments

@beccadax
Copy link
Contributor

Previous ID SR-15194
Radar rdar://problem/83153142
Original Reporter @beccadax
Type New Feature
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels New Feature, TypeChecker
Assignee None
Priority Medium

md5: 85fc84b679e3eb6e54def2cf78308558

Issue Description:

Consider the following code:

let x = 1 / 8

There is nothing here that says that Int or any other integer type will be used, but because we are using integer literals, we end up inferring that we should use integer division. However, because 1 < 8, this division will always produce 0. It is more likely that the user wanted to compute 0.125 in Double or some other floating-point type.

In this situation, we should warn the developer that the result will always be rounded to 0, and provide two fix-its: one which changes the integer literals to floating-point literals, and one which silences the warning by mentioning Int explicitly.

To be clear, here are some code snippets which ideally should not warn:

let x: Int = 1 / 8
let x = Int(1) / 8
let x = 1 / Int(8)
let x = 1 / 8 as Int
functionTakingInt(1 / 8)    // Bonus points: warn if a Double would also be valid in that argument

let x = 1.0 / 8
let x = 1 / 8.0
let x = 1.0 / 8.0

let x = 9 / 8
let x = 0 / 8
let x = -9 / 8

let x = y / 8
let x = 1 / y
let x = y / y

One way to make the idea of "Int was inferred" concrete might be to ask whether we were forced to rely on default type constraints to determine the literals' types.

@typesanitizer
Copy link

@swift-ci create

@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
compiler The Swift compiler in itself new feature type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants