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-10979] Add FixIt for "Operator implementation without matching operator declaration" #53368

Closed
harlanhaskins opened this issue Jun 20, 2019 · 7 comments
Labels
compiler The Swift compiler in itself good first issue Good for newcomers improvement

Comments

@harlanhaskins
Copy link
Collaborator

Previous ID SR-10979
Radar rdar://problem/51138321
Original Reporter @harlanhaskins
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, StarterBug
Assignee mcichecki (JIRA)
Priority Medium

md5: f0bd648bfcc55e26a41a74cccf434f09

Issue Description:

If you create an operator function without declaring the operator, like so:

func <=> (lhs: Int, rhs: Int) -> Bool { false }

You get an error:

Operator implementation without matching operator declaration

It would be nice to have a Fix-It for this, that puts a regular operator there.

If the function body has 2 parameters, you could supply

infix operator <=> : <# Precedence Group #>

And if there's one parameter, you could supply

<# prefix or postfix #> operator <=> : <# Precedence Group #>
@swift-ci
Copy link
Collaborator

Comment by Zsolt Török (JIRA)

I really would like to tackle this down, as my first contribution. Could someone help me a bit? I think I found how to add the Fix-It, but I'm not quite sure how to position the inserted snippets. I may also have some question about testing.

@swift-ci
Copy link
Collaborator

Comment by Michal Cichecki (JIRA)

@harlanhaskins

Where exactly do we want to put these error messages? I was thinking about adding it to the line before function declaration:

infix operator <=> : <# Precedence Group #>
func <=> (lhs: Int, rhs: Int) -> Bool { false }

and

<# prefix or postfix #> operator <=> : <# Precedence Group #>
prefix func <=>(lhs: X) -> X {}

@swift-ci
Copy link
Collaborator

Comment by Zsolt Török (JIRA)

mcichecki (JIRA User)
That was exactly my first thought, but I think we also have to care about the following:

class C {
    static func <=> (lhs: C, rhs: C) -> Bool { false }
}

In this case, I think, we need to add the FixIt to the line before the class declaration instead of the line before the function declaration. Furthermore, I suppose, we need to think about nested classes too.
And this is where I got stuck with my implementation.

@swift-ci
Copy link
Collaborator

Comment by Michal Cichecki (JIRA)

torokzsolt (JIRA User) in this case we should also include extensions. This is example from docs.swift.org:

infix operator +-: AdditionPrecedence
extension Vector2D {
    static func +- (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(x: left.x + right.x, y: left.y - right.y)
    }
} 

The only question is if that's really something that we should take care of or leave it to user who will change the position after the operator is added according to his need.

@swift-ci
Copy link
Collaborator

Comment by Zsolt Török (JIRA)

mcichecki (JIRA User)

You are absolutely right.
Personally, I think it would be nice if the 'fix' would not introduce a new issue to be solved. I feel that we could deal with these cases, but I don't have enough experience with the compiler to solve this problem alone.

@swift-ci
Copy link
Collaborator

Comment by Michal Cichecki (JIRA)

PR

@belkadan
Copy link
Contributor

This got merged, so feel free to mark it Resolved, mcichecki (JIRA User). :-) Thank you for the fix!

@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
compiler The Swift compiler in itself good first issue Good for newcomers improvement
Projects
None yet
Development

No branches or pull requests

3 participants