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-14524] The inferred type of integer literals changes between Swift 5.3 and 5.4 #56876

Closed
saagarjha opened this issue Apr 25, 2021 · 3 comments
Assignees
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

@saagarjha
Copy link
Contributor

Previous ID SR-14524
Radar rdar://problem/77164403
Original Reporter @saagarjha
Type Bug
Status Resolved
Resolution Done
Environment

I'm using Xcode 12.5 beta 3 and the Xcode 12.5 RC, which ship with:

$ DEVELOPER_DIR=/Applications/Xcode-beta.app/Contents/Developer/ xcrun swift --version
Apple Swift version 5.4 (swiftlang-1205.0.26.4 clang-1205.0.19.54)
Target: x86_64-apple-darwin20.5.0
$ DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer/ xcrun swift --version
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Target: x86_64-apple-darwin20.5.0

The code above compiles fine with the beta and fails to compile with the RC.

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

md5: 279060c658586dc4c14273ffff7679d7

Issue Description:

Consider the following code:

var a = 0 as UInt8
let b = 1

a = a & ~(1 << b) | 1 << (b % 8)

In Swift 5.3 this code fails to compile with

<stdin>:4:29: error: binary operator '%' cannot be applied to operands of type 'Int' and 'UInt8'
a = a & ~(1 << b) | 1 << (b % 8)
                          ~ ^ ~
<stdin>:4:29: note: overloads for '%' exist with these partially matching parameter lists: (Int, Int), (UInt8, UInt8)
a = a & ~(1 << b) | 1 << (b % 8)
                            ^

However, this code compiles correctly in Swift 5.4. I am unsure if this change is intentional (as a bugfix?) but I was asked to report it here just in case.

@typesanitizer
Copy link

@swift-ci create

@hborla
Copy link
Member

hborla commented May 3, 2021

Thanks for the report! The type inference change was a bug fix, and the fact that this expression compiles is correct behavior. In Swift 5.3, the type checker was not able to infer different types for integer literals in certain expressions involving operators. This was fixed in Swift 5.4, which allows this expression to compile.

@hborla
Copy link
Member

hborla commented May 3, 2021

For reference, this was fixed by #33060 which also fixed https://bugs.swift.org/browse/SR-12365

@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

3 participants