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-14484] odd "error: no exact matches in call to initializer" #56839

Closed
weissi opened this issue Apr 14, 2021 · 3 comments
Closed

[SR-14484] odd "error: no exact matches in call to initializer" #56839

weissi opened this issue Apr 14, 2021 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself generics Feature: generic declarations and types

Comments

@weissi
Copy link
Member

weissi commented Apr 14, 2021

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

@weissi
Copy link
Member Author

weissi commented Apr 14, 2021

@swift-ci generate

@slavapestov
Copy link
Member

@swift-ci create

@slavapestov
Copy link
Member

#42113

@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. compiler The Swift compiler in itself generics Feature: generic declarations and types
Projects
None yet
Development

No branches or pull requests

2 participants