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-13407] Can't init Scalar with 9992 in enum #55848

Closed
swift-ci opened this issue Aug 17, 2020 · 5 comments
Closed

[SR-13407] Can't init Scalar with 9992 in enum #55848

swift-ci opened this issue Aug 17, 2020 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13407
Radar None
Original Reporter 酷酷的哀殿 (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Environment

Xcode 11.5

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

md5: 633333f39caf6549a04da338eaccb904

Issue Description:

```

    **enum** CharOrSectionMarkerScalar

{ case Paragraph case Char(Unicode.Scalar) case Chapter }

   CharOrSectionMarkerScalar.Char(Unicode.Scalar(9992))

```

Integer literal '9992' overflows when stored into 'UInt8'

The follow code work well.

```

CharOrSectionMarkerScalar.Char(Unicode.Scalar(9992)!);

```

@typesanitizer
Copy link

This behaves as intended.

 CharOrSectionMarkerScalar.Char(Unicode.Scalar(9992)) 

For this example, the type of the literal ends up being UInt8 (because there is only one initializer of Unicode.Scalar which takes an integral value). Hence you get the compilation error because of the overflow.

CharOrSectionMarkerScalar.Char(Unicode.Scalar(9992)!)

For this example, a failable initializer is used due to the outer !. Unicode.Scalar has three failable initializers which take integral values. An integer literal defaults to Int in the absence of additional type information, so that initializer is used. Since 9992 fits in the range of Int, there is no compilation error.

@swift-ci
Copy link
Collaborator Author

Comment by bohong sun (JIRA)

theindigamer (JIRA User)

1、

Since 9992 is a legal parameter, it should return a legal value,not Some<Unicode.Scalar>

2、

Moreover, the following error message is more appropriate. It can help developers avoid confusion.

@typesanitizer
Copy link

> Since 9992 is a legal parameter, it should return a legal value,not Some<Unicode.Scalar>

I don't understand what you mean by "legal parameter". If you look at the initializers for Unicode.Scalar (https://developer.apple.com/documentation/swift/unicode/scalar), those are:

1. init(UInt8)
2. init(Unicode.Scalar)
3. init?(UInt32)
4. init?(UInt16)
5. init?(Int)
6. init(unicodeScalarLiteral: Unicode.Scalar)
7. init?(String) 

As I said above, in the case with a force-unwrap, initializer 5 is selected (which has a ? indicating that it returns an Optional). In the case without the force-unwrap, initializer 1 is selected, which gives the overflow error.

> Moreover, the following error message is more appropriate.

The reason you are seeing different error messages is because s0 has type Unicode.Scalar?, whereas when you write CharOrSectionMarkerScalar.Char(Unicode.Scalar(9992)) directly, the inner temporary has type Unicode.Scalar (no optional) because the associated value for the CharOrSectionMarkerScalar.Char case is expected to have type Unicode.Scalar.

Could you clarify what the central issue is?

  1. Is the issue that the overflow diagnostic doesn't make sense/is difficult to understand?

  2. Are you suggesting that the code example with CharOrSectionMarkerScalar.Char(Unicode.Scalar(9992)) should compile?

  3. Something else?

@swift-ci
Copy link
Collaborator Author

Comment by bohong sun (JIRA)

1、The overflow diagnostic is difficult to understand. We could make it more understandable.

2、9992 is a valid Unicode code point

///     let airplane = Unicode.Scalar(9992)

///     print(airplane)

///     // Prints "✈︎"

And https://developer.apple.com/documentation/swift/unicode/scalar/2905753-init lists the legal range

0...0xD7FF or 0xE000...0x10FFFF.

CharOrSectionMarkerScalar.Char(Unicode.Scalar(9992)) should be compile and Unicode.Scalar(9992) should't be a Unicode.Scalar?, {{}} it should be Unicode.Scalar?

@typesanitizer
Copy link

> Integer literal '9992' overflows when stored into 'UInt8'

Point taken, I've filed SR-13429 to improve that.

> 9992 is a valid Unicode code point

Sure, that's why the initializer succeeds and returns .some("✈︎") instead of nil.

@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.
Projects
None yet
Development

No branches or pull requests

2 participants