-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
Conversation
That helps when there is no context around a closure and parameters used in the body are untyped.
…epresents a closure parameter type
…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
@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}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo "nable" -> "unable" :)
There was a problem hiding this comment.
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 :)
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() << "'"; | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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() << "'"; | ||
} |
There was a problem hiding this comment.
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
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.
Resolves: SR-12815
Resolves: rdar://problem/63230293