Skip to content

[CSDiagnostics] Diagnose invalid partial application #22124

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

Merged
merged 5 commits into from
Jan 28, 2019

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Jan 25, 2019

Detect and diagnose invalid partial application of 'mutating' methods and constructor delegation.

This used to be diagnosed by MiscDiagnostics when invalid solution is already applied to AST,
moving these diagnostics to constraint system allows to detect failures like that early and makes
other diagnostics easier e.g. missing or extraneous metatype base in member reference.

Currently supported only partial applications of instance methods
marked as `mutating`.
@xedin xedin requested a review from jckarter January 25, 2019 22:06
@xedin
Copy link
Contributor Author

xedin commented Jan 25, 2019

/cc @theblixguy

@xedin
Copy link
Contributor Author

xedin commented Jan 25, 2019

@jckarter Since you have worked on this before could you take a look? Any other examples I should add?

@xedin xedin force-pushed the diagnose-partial-applies branch from 65d18af to 14f6534 Compare January 25, 2019 22:13
xedin added 4 commits January 25, 2019 14:17
…al apply

Verify that invalid partial apply is detected if base of the
mutating method if unknown upfront.
…tics

Diagnostics for cases like these has been moved to new diagnostic framework.
@xedin xedin force-pushed the diagnose-partial-applies branch from 14f6534 to 6134533 Compare January 25, 2019 22:18
@xedin
Copy link
Contributor Author

xedin commented Jan 25, 2019

@swift-ci please test

@jckarter
Copy link
Contributor

Hopefully our existing test cases cover all the edge cases. One of the tricky ones is partially applying an instance method as a static member:

struct X {
  mutating func foo() {}
}

var x = X()
_ = X.foo // an illegal partial application of type (inout X) -> () -> ()
_ = X.foo(&x) // an illegal partial application of type () -> ()
_ = X.foo(&x)() // legal, i guess

I think our existing test cases cover this, but it'd be good to confirm.

@xedin
Copy link
Contributor Author

xedin commented Jan 25, 2019

@jckarter Yes, existing tests in test/Constraints/mutating_memeber_{compat}.swift check exactly that, I have also added a couple new ones where base is overloaded

@xedin
Copy link
Contributor Author

xedin commented Jan 25, 2019

@swift-ci please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 65d18afbe721321033c33339f4b4c40963f7cc89

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 65d18afbe721321033c33339f4b4c40963f7cc89

@slavapestov
Copy link
Contributor

@xedin Also it would be cool to diagnose construction of non-constant metatype values early. Eg if I write

let foo = Int.self
foo(0)

Right now we solve the solution and then diagnose it in MiscDiagnostics.

Same with missing .self:

foo(_: Any.Type) {}
foo(Int)

@xedin
Copy link
Contributor Author

xedin commented Jan 26, 2019

@slavapestov I'm preparing the PR for that and another partial application problems like:

class A {}

enum B {
  func foo() {
    bar(A()) // should diagnose as `'A' cannot be passed as 'self' parameter to instance method 'bar' of enum 'B'`
  }

  func bar(_: A) {}
}

I'm just trying to figure out the best way to attach fixes to overload choices.

@slavapestov
Copy link
Contributor

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants