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-7389] Inference issue for default implementations #49932

Open
swift-ci opened this issue Apr 9, 2018 · 2 comments
Open

[SR-7389] Inference issue for default implementations #49932

swift-ci opened this issue Apr 9, 2018 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 9, 2018

Previous ID SR-7389
Radar None
Original Reporter koher (JIRA User)
Type Bug
Environment

Version 9.3 (9E145)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @xedin
Priority Medium

md5: 35dd3e010d8f47e63e9da7ec6f4e1f66

Issue Description:

Following code causes a compile-time error.

protocol FooProtocol {
    static func +(lhs: Self, rhs: Self) -> Self
    static func -(lhs: Self, rhs: Self) -> Self
    static prefix func-(value: Self) -> Self
}

extension FooProtocol {
    static func -(lhs: Self, rhs: Self) -> Self {
        return lhs + -rhs
    }
}

struct Foo : FooProtocol {
    static func +(lhs: Foo, rhs: Foo) -> Foo { return lhs }
    static prefix func-(value: Foo) -> Foo { return value }
}


let a = Foo()
let b = a + a - a + a - a + a - a + a
error: expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

let b = a + a - a + a - a + a - a + a

        ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~

However, when the extension below is given, the code can be compiled.

extension Foo {
    static func -(lhs: Foo, rhs: Foo) -> Foo {
        return lhs + -rhs
    }
}

It seems strange. In both cases, types of the operands are determined even if the - is implemented by a default implementation.

@belkadan
Copy link
Contributor

belkadan commented Apr 9, 2018

Operations that work on concrete types are always going to be simpler for the compiler to reason about than those involving protocols, but we'd still like to do better here. @xedin?

@xedin
Copy link
Member

xedin commented Apr 10, 2018

koher (JIRA User) Unfortunately type-checking performance related to generics is not yet optimal (but we are working on it), because unlike for concrete types, we can't attempt bindings for arguments until all of the operator overloads are applied which leads to exponential behavior. So I would suggest for time being and if it's possible add new operator overloads only with concrete types.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants