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-297] Overflow not trapped in generic function #42919

Open
jepers opened this issue Dec 18, 2015 · 2 comments
Open

[SR-297] Overflow not trapped in generic function #42919

jepers opened this issue Dec 18, 2015 · 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

@jepers
Copy link

jepers commented Dec 18, 2015

Previous ID SR-297
Radar rdar://22936450
Original Reporter @jepers
Type Bug
Environment

OS X 10.11.2, Swift version 2.2-dev (LLVM 3ebdbb2c7e, Clang f66c5bb67b, Swift 69940d1)

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

md5: 266267f2264d51f0997250ad08fd57d0

Issue Description:

$ cat > t.swift
func foo<T: UnsignedIntegerType>(_: T.Type) -> T { return -1 }
if foo(UInt8) == 255 { print("Overflow is not trapped 1.") }
func bar<T: UnsignedIntegerType>(_: T.Type) -> T {
    let uintVal: T = -1
    return uintVal
}
if bar(UInt8) == 255 { print("Overflow is not trapped 2.") }

$ swiftc t.swift && ./t
Overflow is not trapped 1.
Overflow is not trapped 2.
@karwa
Copy link
Contributor

karwa commented Oct 14, 2016

@DougGregor this looks like the same issue we discussed:

func getValue<T>() -> T where T:ExpressibleByIntegerLiteral { 
     return 1000 
}
getValue() as UInt8 // returns 232. No warnings.

This also affects the generated RawRep conformance for enums when the raw type is generic.

@DougGregor
Copy link
Member

Ah, yes, it is. Something in the integer literal protocols is dodging the overflow checking.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added standard library Area: Standard library umbrella generics Feature: generic declarations and types numerics Area → standard library: Numeric API literals Feature → expressions: Literals such as an integer or string literal expressions Feature: expressions unexpected behavior Bug: Unexpected behavior or incorrect output swift 6.0 integer literals Feature → literals: Integer literals and removed compiler The Swift compiler in itself labels 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

4 participants