Skip to content

Conversation

@jameesbrown
Copy link
Contributor

@jameesbrown jameesbrown commented Apr 6, 2025

In resolveClosureType allow binding a closure parameter (when it's a free type variable) to a contextual type that is a pack expansion and otherwise valid.

Resolves: #78426

Copy link
Contributor

@xedin xedin Apr 6, 2025

Choose a reason for hiding this comment

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

I think this should go into ConstraintSystem::resolveClosure because that's the spot where we know that there is a contextual type.

Copy link
Contributor Author

@jameesbrown jameesbrown Apr 6, 2025

Choose a reason for hiding this comment

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

Thanks! I actually started with ConstraintSystem::resolveClosure, but the solution didn’t quite fall into place as cleanly, which led me to try this approach. I definitely missed something though and will try that way again.

Copy link
Contributor

Choose a reason for hiding this comment

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

This approach would only work for cases were contextual type is known in advance but if closure is passed as an argument to an overloaded function there is no contextual type available upfront, it becomes available once an overload of that function is bound.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, thanks for pointing it out, I was having trouble thinking of how we'd not have the contextual type other than the most basic version: let _ = { x in }.

@jameesbrown jameesbrown force-pushed the closure-type-inference branch from 162a751 to f20e3bc Compare April 7, 2025 21:37
@jameesbrown jameesbrown changed the title [CSGen] Use contextualTypeInfo in inferClosureType [CS] Allow binding closure param to pack expansion Apr 7, 2025
@jameesbrown jameesbrown requested a review from xedin April 7, 2025 21:47
else if (isSuitable) {
// If we have an unbound type variable and the contextual parameter
// type is a pack expansion, we must bind it here. Otherwise, it will
// never get bound.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the important question here is why doesn't it get bound, is this an inference problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we start with a plain type variable that doesn't have TVO_PackExpansion, it's not allowed to be bound to a PackExpansionType (there's a comment about this in matchTypesBindTypeVar). In some places we do the:

auto *pack = PackType::get(Ctx, tyVar)
// bind this to the pack expansion
auto *expansion = PackExpansionType::get(pack, pack)

But that always fails when the pack expansion we are binding to has a pattern type that's a PackArchetypeType because we'll try to match the reduced shape of Pack{$T1} to a PackArchetypeType.

What we are expecting is to have opened the pack expansion type via

Type ConstraintSystem::openPackExpansionType(PackExpansionType *expansion,
and use that as our type variable, but at this point it seems to late. The only place I can think of is when we initially infer the closure type, but there are problems with that too. Is there a way we can say "forget about this type variable" and replace it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, so I think what we need to do instead of assigning is to flip the flag on parameter type variable to allow it to be bound to a pack expansion.

Copy link
Contributor

Choose a reason for hiding this comment

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

This might be okay to do by default in inferClosureType since we already set TVO_CanBindInOut there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay I like that way better too, thank you! Going to try that.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jameesbrown are you still planning to work on this?

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.

Closure parameter type cannot be inferred when contextual type is pack expansion

2 participants