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

[ConstraintSystem] Detect and diagnose missing generic arguments #25149

Merged
merged 9 commits into from May 31, 2019

Conversation

xedin
Copy link
Member

@xedin xedin commented May 30, 2019

Introduce a fix to detect and diagnose situations when omitted
generic arguments couldn't be deduced by the solver based on
the enclosing context.

Example:

struct S<T> {
}

_ = S() // There is not enough context to deduce `T`

Resolves: rdar://problem/51203824

xedin added 8 commits May 29, 2019 10:26
Emit separate notes for generic parameters found in operator, initializer
and function declarations. As well as clarify types of `declared in type`
note.
… member to `Any`

As part of the `DefineBasedOnUse` fix introduced in places where
there is a reference to non-existent member, let's also add
constraints which allow to default any generic parameters found
in base type to `Any`.
Introduce a fix to detect and diagnose situations when omitted
generic arguments couldn't be deduced by the solver based on
the enclosing context.

Example:

```swift
struct S<T> {
}

_ = S() // There is not enough context to deduce `T`
```

Resolves: rdar://problem/51203824
@xedin xedin requested a review from DougGregor May 30, 2019 19:44
Copy link
Member

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff!

@@ -102,7 +102,7 @@ func longArray() {
var _=["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"]
}

[1,2].map // expected-error {{expression type '((Int) throws -> _) throws -> [_]' is ambiguous without more context}}
[1,2].map // expected-error {{generic parameter 'T' could not be inferred}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@@ -79,7 +79,8 @@ func badDiagnostic2() {
func takesAny(_ a: Any) {}

func badDiagnostic3() {
takesAny(Deli.self) // expected-error {{argument type 'Deli<_>.Type' does not conform to expected type 'Any'}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

// expected-error@-1 {{generic parameter 'A' could not be inferred}}
_ = Claws.Fangs<NotADog>()
// expected-error@-1 {{generic parameter 'A' could not be inferred}}
// expected-error@-2 {{referencing initializer 'init()' on 'Claws.Fangs' requires that 'NotADog' conform to 'ExpressibleByDogLiteral'}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

let _ : D = D<Int, Int, Float>(a: 1, b: 2)
// expected-error@-1 {{generic parameter 'T3' could not be inferred}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Member

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I do have a couple of minor comments :)


_ = Pair< // expected-error {{generic parameter 'Foo' could not be inferred}}
_ = Pair<
FullyGeneric,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slavapestov I think the biggest improvement here is actually related to couple of examples down here, which we now diagnostic relative to TypeRepr and suggest all of the missing signatures.

@jckarter
Copy link
Member

jckarter commented May 30, 2019

This is great! Would it be possible to show the declaration name in the diagnostic? For some of these cases, I think it'd be easier to understand if the diagnostic could point at the declaration the generic argument comes from, e.g.:

error: generic parameter 'T' of 'map' could not be inferred
note: 'map' declared here

since in some cases there may be multiple generic declarations involved in an expression.

@xedin
Copy link
Member Author

xedin commented May 30, 2019

@jckarter Yes, absolutely, also I'll also cover more cases in the note, it currently only covers functions and initializes.

@xedin
Copy link
Member Author

xedin commented May 30, 2019

@swift-ci please test

@xedin
Copy link
Member Author

xedin commented May 30, 2019

@swift-ci please test source compatibility

@swift-ci
Copy link
Collaborator

Build failed
Swift Test Linux Platform
Git Sha - 5e35c3e

@swift-ci
Copy link
Collaborator

Build failed
Swift Test OS X Platform
Git Sha - 5e35c3e

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.

None yet

4 participants