-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Closed
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfgenericsFeature: generic declarations and typesFeature: generic declarations and types
Description
| Previous ID | SR-14484 |
| Radar | rdar://problem/77794156 |
| Original Reporter | @weissi |
| Type | Bug |
| Status | Resolved |
| Resolution | Done |
Additional Detail from JIRA
| Votes | 0 |
| Component/s | Compiler |
| Labels | Bug, Generics |
| Assignee | @slavapestov |
| Priority | Medium |
md5: 76471e1930f4bfd0901075d1dc28402d
Issue Description:
This Swift program compiles and works:
func truncate<Input: FixedWidthInteger & UnsignedInteger & BinaryInteger,
Output: FixedWidthInteger & SignedInteger & BinaryInteger>(_ input: Input, outputType: Output.Type = Output.self) -> Output {
precondition(Output.Magnitude.self == Input.self)
return Output(truncatingIfNeeded: input)
}
print(truncate(UInt(4), outputType: Int.self))
but if I add where Output.Magnitude == Input to the type constraints, then the compilation fails with
test.swift:1:22: warning: redundant conformance constraint 'Input': 'FixedWidthInteger'
func truncate<Input: FixedWidthInteger & UnsignedInteger & BinaryInteger,
^
test.swift:2:31: note: conformance constraint 'Input': 'FixedWidthInteger' implied here
Output: FixedWidthInteger & SignedInteger & BinaryInteger>(_ input: Input, outputType: Output.Type = Output.self) -> Output
^
test.swift:1:42: warning: redundant conformance constraint 'Input': 'UnsignedInteger'
func truncate<Input: FixedWidthInteger & UnsignedInteger & BinaryInteger,
^
test.swift:2:31: note: conformance constraint 'Input': 'UnsignedInteger' implied here
Output: FixedWidthInteger & SignedInteger & BinaryInteger>(_ input: Input, outputType: Output.Type = Output.self) -> Output
^
test.swift:4:12: error: no exact matches in call to initializer
return Output(truncatingIfNeeded: input)
^
Swift.BinaryInteger:6:5: note: candidate requires that 'Input' conform to 'BinaryInteger' (requirement specified as 'T' == 'BinaryInteger')
init<T>(truncatingIfNeeded source: T) where T : BinaryInteger
^
Swift.FixedWidthInteger:6:23: note: candidate requires that 'Input' conform to 'BinaryInteger' (requirement specified as 'T' == 'BinaryInteger')
@inlinable public init<T>(truncatingIfNeeded source: T) where T : BinaryInteger
which is odd because there are matches for that initialiser (see the runtime precondition check).
The failing program is
func truncate<Input: FixedWidthInteger & UnsignedInteger & BinaryInteger,
Output: FixedWidthInteger & SignedInteger & BinaryInteger>(_ input: Input, outputType: Output.Type = Output.self) -> Output
where Output.Magnitude == Input {
precondition(Output.Magnitude.self == Input.self)
return Output(truncatingIfNeeded: input)
}
print(truncate(UInt(4), outputType: Int.self))
The above error is from 5.3 / 5.4, on main the compiler actually crashes: SR-14485
Metadata
Metadata
Assignees
Labels
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfgenericsFeature: generic declarations and typesFeature: generic declarations and types