In this simple piece of code, I'm using the integer literal -1
in a context 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
. I would have expected a runtime error, or at least a warning at compile time. Is this a bug or is this documented somewhere?
struct Bad<T: FixedWidthInteger> {
func getNegativeOne() -> T {
return -1
}
}
print(Bad<UInt32>().getNegativeOne())
When running it from the swift
repl:
$ swift
Welcome to Swift version 5.5.2-dev.
Type :help for assistance.
1> [paste code]
0