Skip to content

[ConstraintSystem] Detect and diagnose inability to infer type of closure parameter(s) #31809

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 7 commits into from
May 15, 2020

Conversation

xedin
Copy link
Contributor

@xedin xedin commented May 15, 2020

Detect situation when it's impossible to determine types for
closure parameters used in the body from the context. E.g.
when a call closure is associated with refers to a missing
member.

struct S {
}

S.foo { a, b in } // `S` doesn't have static member `foo`

let _ = { v in } // not enough context to infer type of `v`

_ = .foo { v in } // base type for `.foo` couldn't be determined

Resolves: SR-12815
Resolves: rdar://problem/63230293

xedin added 7 commits May 15, 2020 01:13
That helps when there is no context around a closure and parameters
used in the body are untyped.
…contextual couldn't be established

If closure is an argument to a call to a missing member or invalid
contextual member reference let's not diagnose problems related to
inability to infer parameter types because root issue is that context
for closure inference couldn't be established.
Holes couldn't be contracted instead solving of the constraint
has to be delayed until one of the sides is bound to a type,
otherwise there is a risk to loose contextual information.
…sure parameter(s)

Detect situation when it's impossible to determine types for
closure parameters used in the body from the context. E.g.
when a call closure is associated with refers to a missing
member.

```swift
struct S {
}

S.foo { a, b in } // `S` doesn't have static member `foo`

let _ = { v in } // not enough context to infer type of `v`

_ = .foo { v in } // base type for `.foo` couldn't be determined
```

Resolves: [SR-12815](https://bugs.swift.org/browse/SR-12815)
Resolves: rdar://problem/63230293
@xedin xedin requested a review from hborla May 15, 2020 08:15
@xedin
Copy link
Contributor Author

xedin commented May 15, 2020

@swift-ci please smoke test

var flag = false
return try? i.getB(&flag)
}.compactMap { u -> P? in
}.compactMap { u -> P? in // expected-error {{nable to infer type of a closure parameter 'u' in the current context}}
Copy link
Contributor

Choose a reason for hiding this comment

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

typo "nable" -> "unable" :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that's okay, I'll fix it later, it's just a pattern match anyway :)

Comment on lines +6183 to +6196
auto paramIdx = getLocator()
->castLastElementTo<LocatorPathElt::TupleElement>()
.getIndex();

auto *PD = closure->getParameters()->get(paramIdx);

llvm::SmallString<16> id;
llvm::raw_svector_ostream OS(id);

if (PD->isAnonClosureParam()) {
OS << "$" << paramIdx;
} else {
OS << "'" << PD->getParameterName() << "'";
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be using the new getName method defined below?

Copy link
Member

Choose a reason for hiding this comment

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

SpecifyClosureParameterType::getName is the name of the constraint fix, used in the -debug-constraints output

Comment on lines +6183 to +6196
auto paramIdx = getLocator()
->castLastElementTo<LocatorPathElt::TupleElement>()
.getIndex();

auto *PD = closure->getParameters()->get(paramIdx);

llvm::SmallString<16> id;
llvm::raw_svector_ostream OS(id);

if (PD->isAnonClosureParam()) {
OS << "$" << paramIdx;
} else {
OS << "'" << PD->getParameterName() << "'";
}
Copy link
Member

Choose a reason for hiding this comment

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

SpecifyClosureParameterType::getName is the name of the constraint fix, used in the -debug-constraints output

@xedin xedin merged commit 01dcc29 into swiftlang:master May 15, 2020
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