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-8002] Failed to infer type of integer literal #50535

Closed
tayloraswift opened this issue Jun 15, 2018 · 10 comments
Closed

[SR-8002] Failed to infer type of integer literal #50535

tayloraswift opened this issue Jun 15, 2018 · 10 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

@tayloraswift
Copy link
Contributor

Previous ID SR-8002
Radar rdar://problem/41146547
Original Reporter @Kelvin13
Type Bug
Status Resolved
Resolution Done
Environment

```

$ swift --version
Swift version 4.2-dev (LLVM 70f121e1f0, Clang 4c555650a6, Swift bb9532c)
Target: x86_64-unknown-linux-gnu

```

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

md5: cf8329b51c747aa989d37a85c3772d97

Issue Description:

In the following example

func amplitude(count:Int, bitPattern:UInt16)
{ 
    let sign:UInt16 = bitPattern &>> (count &- 1) ^ 1 
}

Swift is unable to infer the rightmost `1` as `UInt16`.

error: binary operator '^' cannot be applied to operands of type 'UInt16' and 'Int'
 let sign:UInt16 = bitPattern &>> (count &- 1) ^ 1
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
 note: expected an argument list of type '(UInt16, UInt16)'
 let sign:UInt16 = bitPattern &>> (count &- 1) ^ 1
@huonw
Copy link
Mannequin

huonw mannequin commented Jun 15, 2018

There's the obvious fix of adding UInt16(...) to the second 1 (bitPattern &>> (count &- 1) ^ UInt16(1)), but, surprisingly/why I think this is a bug, inference can also be made to work by adding `Int(...)` to the first `1` (bitPattern &>> (count &- Int(1)) ^ 1), or by removing the subtraction completely (bitPattern &>> count ^ 1).

@swift-ci create

@belkadan
Copy link
Contributor

cc @xedin

@rudkx
Copy link
Member

rudkx commented Jun 15, 2018

It's a bad diagnostic. We don't have heterogeneous shifts for the masking shifts, so we should be complaining about the count here.

Note that the precedence is
(bitPattern &>> (count &- 1)) ^ 1

and not

bitPattern &>> ((count &- 1) ^ 1)

Is the lack of heterogenous overloads in this case is intentional, @moiseev?

@rudkx
Copy link
Member

rudkx commented Jun 15, 2018

Taking a look at Integers.swift.gyb, it does look to be intentional.

@rudkx
Copy link
Member

rudkx commented Jun 15, 2018

Well perhaps there is more going on here than I noticed. There is a heterogeneous masking shift in FixedWidthInteger.

@moiseev
Copy link
Mannequin

moiseev mannequin commented Jun 16, 2018

@rudkx there are heterogeneous overloads like &>> <Value: FixedWidthInteger, Shift: BinaryInteger>, and there are also homogeneous ones on concrete integer types.

@moiseev
Copy link
Mannequin

moiseev mannequin commented Jun 16, 2018

I agree that it looks like the wrong diagnostics, what I think happens is: bitPattern &>> (count &- 1) – this part gets Int because count is Int. Then ^ gets applied to Int and Int and results in Int, but then it tries to squeeze this value into a Uint16 and fails.

@huonw
Copy link
Mannequin

huonw mannequin commented Jun 19, 2018

I think there's more than just being a wrong diagnostic: the version without a subtraction bitPattern &>> count ^ 1 compiles fine, even though, presumably, the bitPattern &>> count subexpression should be being getting the same type as the shift in the version with the subtraction.

@hborla
Copy link
Member

hborla commented Aug 12, 2020

This was fixed recently on master with #33060

Could you please verify using the latest master toolchain from https://swift.org/download/#snapshots? Thank you!

@tayloraswift
Copy link
Contributor Author

@hborla I tested with the DEVELOPMENT-SNAPSHOT-2020-08-07-a snapshot available in swiftenv, and it compiles without errors 🙂 thanks!

@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

4 participants