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-4984] Integer literal not inferred as unsigned in != comparison #47561

Open
nevil opened this issue May 24, 2017 · 10 comments
Open

[SR-4984] Integer literal not inferred as unsigned in != comparison #47561

nevil opened this issue May 24, 2017 · 10 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. regression standard library Area: Standard library umbrella swift 4.0 type checker Area → compiler: Semantic analysis

Comments

@nevil
Copy link

nevil commented May 24, 2017

Previous ID SR-4984
Radar None
Original Reporter @nevil
Type Bug
Status Closed
Resolution Done
Environment

swift-4.0-DEVELOPMENT-SNAPSHOT-2017-05-22-a

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, 4.0Regression, TypeChecker
Assignee @nevil
Priority Medium

md5: 72c3acdcb42f9b52ada14b67693bd331

relates to:

  • SR-13755 Convenience operators on BinaryInteger causing subtle bugs due to unintuitive type inference

Issue Description:

The below sample code results in build error for 32 bit targets when using swift-4.0-DEVELOPMENT-SNAPSHOT-2017-05-22.

When using "swift-3.1-DEVELOPMENT-SNAPSHOT-2017-03-27" there is no error.

Invocation:
/Library/Developer/Toolchains/swift-4.0-DEVELOPMENT-SNAPSHOT-2017-05-22-a.xctoolchain/usr/bin/swift -frontend -c Overflow.swift -swift-version 3 -target armv7-apple-ios9.0

let q: UInt = 0

// Works
if q == 0xFFFFFFFF {
    print("==")
}

// Integer literal '4294967295' overflows when stored into 'Int'
if q != 0xFFFFFFFF {
    print("!=")
}
@belkadan
Copy link
Contributor

64-bit targets have the same problem with 0xFFFF_FFFF_FFFF_FFFF.

@rudkx
Copy link
Member

rudkx commented May 24, 2017

It looks like this is fixed on master, and this is really a library issue, not type checker.

@moiseev
Copy link
Mannequin

moiseev mannequin commented May 24, 2017

This is the price we are paying for the heterogeneous comparisons. In the March toolchain the new integer protocols did not exist yet, in May they do. And now there is a generic version of != that allows to compare any integer to any other integer. There are also homogeneous versions, but, since the default type of an integer literal is Int, this version is tried first, and when it succeeds, the typecheker will not try to evaluate any other solutions, even though there might be better ones.

@rudkx, you told me that there was some bug/hack in the compiler to shortcut decision making on operators specifically. In all other cases the typechecker will find all the solutions and rank them, which would choose the (Self, Self) overload in this case.

@moiseev
Copy link
Mannequin

moiseev mannequin commented May 24, 2017

Interestingly... Here is the behavior on master now:

(swift) UInt() == 0xFFFF_FFFF_FFFF_FFFF
// r0 : Bool = false
(swift) UInt() != 0xFFFF_FFFF_FFFF_FFFF
<REPL Input>:1:11: error: integer literal '18446744073709551615' overflows when stored into 'Int'
UInt() != 0xFFFF_FFFF_FFFF_FFFF
          ^

So there is hope!

@moiseev
Copy link
Mannequin

moiseev mannequin commented May 24, 2017

Testing the solution here: #9909

@rudkx
Copy link
Member

rudkx commented May 24, 2017

There are various hacks in the type checker, some specific to operators, but generally the issue with default literals is just one example of a bigger choice there, which is having two orthogonal systems for determining what solution is "best", where one of those systems short-circuits trying to find more solutions.

@nevil
Copy link
Author

nevil commented Mar 29, 2018

@moiseev

I don't know if this was (intentionally) solved in a PR but it is no longer occurring in :

  • swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-28-a.xctoolchain

  • swift-DEVELOPMENT-SNAPSHOT-2018-03-28-a.xctoolchain

I guess I can close the issue?

@moiseev
Copy link
Mannequin

moiseev mannequin commented Mar 29, 2018

It must've been fixed here: #13830
In an attempt to fix https://bugs.swift.org/browse/SR-6634.

@moiseev
Copy link
Mannequin

moiseev mannequin commented Mar 29, 2018

@nevil please go ahead and close.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@xwu
Copy link
Collaborator

xwu commented Nov 27, 2023

As reported in #70041, this has regressed.

@xwu xwu reopened this Nov 27, 2023
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. regression standard library Area: Standard library umbrella swift 4.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants