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-15971] Negative integer literal converted to generic unsigned integer type without warning #58232

Closed
swift-ci opened this issue Mar 12, 2022 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. expressions Feature: expressions generics Feature: generic declarations and types integer literals Feature → literals: Integer literals literals Feature → expressions: Literals such as an integer or string literal numerics Area → standard library: Numeric API standard library Area: Standard library umbrella swift 6.0 unexpected behavior Bug: Unexpected behavior or incorrect output

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-15971
Radar rdar://problem/90348991
Original Reporter feuermurmel (JIRA User)
Type Bug
Environment
$ swift --version swift-driver version: 1.26.21 Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30) Target: arm64-apple-macosx12.0
Additional Detail from JIRA
Votes 1
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 13075866fbea31d75181d4790f9f8c31

Issue Description:

Reporting this after asking on forums.swift.org, where user @xwu suggested to report this as a bug:

In this simple piece of code, the integer literal -1 is used where a value of type T is expected. T conforms to FixedWidthInteger so it's unknown at compile time whether the literal can be converted to the actual type T. If T is set to an unsigned integer type, the -1 simply becomes a 0:

func f<T: FixedWidthInteger>(_: T.Type) -> T { -1 as T }
f(UInt.self) // 0 (no error!)

I would have expected a runtime error, or at least a warning at compile time, when comparing this to how integer literals are checked agains concrete types as compile time and how explicit casts using an integer type's initializer result in a runtime error in such a case.

@stephentyrone
Copy link
Member

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added standard library Area: Standard library umbrella accepts invalid Bug: Accepts invalid swift 6.0 generics Feature: generic declarations and types literals Feature → expressions: Literals such as an integer or string literal expressions Feature: expressions integer literals Feature → literals: Integer literals unexpected behavior Bug: Unexpected behavior or incorrect output numerics Area → standard library: Numeric API and removed accepts invalid Bug: Accepts invalid labels Feb 14, 2024
@AnthonyLatsis
Copy link
Collaborator

Duplicate of #42919.

@AnthonyLatsis AnthonyLatsis closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2024
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. expressions Feature: expressions generics Feature: generic declarations and types integer literals Feature → literals: Integer literals literals Feature → expressions: Literals such as an integer or string literal numerics Area → standard library: Numeric API standard library Area: Standard library umbrella swift 6.0 unexpected behavior Bug: Unexpected behavior or incorrect output
Projects
None yet
Development

No branches or pull requests

3 participants