Skip to content

[CSGen] Allow _ pattern to be a hole #60203

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
Aug 8, 2022
Merged

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Jul 22, 2022

In cases like case .test(_) it might not be possible to
establish a type of _ and hole cannot be propagated to
it from context if condition of switch is incorrect, so
_ just like a named pattern should be allowed to become
a hole.

Resolves: rdar://96997534

@xedin xedin requested review from ahoppen and hborla July 22, 2022 23:34
@xedin
Copy link
Contributor Author

xedin commented Jul 22, 2022

@swift-ci please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

The code completion failure might be related to the comment I put inline regarding the anchor. If not, I can take a look at it if you like.

@ahoppen
Copy link
Member

ahoppen commented Jul 24, 2022

I just looked at the code completion failure and the problem is that in this test case we fail to resolve the type for _ if we choose the keypath subscript overload, resulting in a fix score of 100, thus ruling out that solution.

struct MyStruct<T> {
  static subscript(x: Int, static defValue: T) -> MyStruct<T> {
    fatalError()
  }
  subscript(x: Int, instance defValue: T) -> Int {
    fatalError()
  }
}

func test1() {
  let _ = MyStruct<Int>()[#^INSTANCE_INT_BRACKET^#
}

We don’t have this problem because it cannot be bound to a hole since the type variable is created by

varType = CS.getType(boundExpr)->getRValueType();

AFAICT the solution to this problem is to make variables at pattern decls as potentially incomplete, i.e. add

    // Delay resolution of pattern decls because we prefer to determine its type
    // based on the expression it is assigned from. Also, if there is an error
    // in the expression, we want to prefer creating the direct hole inside the
    // expression, not at the pattern decl.
    if (locator->isLastElement<LocatorPathElt::PatternDecl>()) {
      return true;
    }

inside the if (isHole()) of BindingSet::isPotentiallyIncomplete.

@xedin
Copy link
Contributor Author

xedin commented Jul 24, 2022

I think it makes sense to do for _ what is done for named patterns in this case, if it could assume a type of an initializer it should since it would regardless just indirectly…

@xedin xedin requested a review from ahoppen August 2, 2022 23:21
@xedin
Copy link
Contributor Author

xedin commented Aug 2, 2022

@swift-ci please test

xedin added 7 commits August 3, 2022 15:50
The fix should support both named (i.e. `test(a)` and "any" patterns
i.e. `test(_)`.
In cases like `case .test(_)` it might not be possible to
establish a type of `_` and hole cannot be propagated to
it from context if condition of switch is incorrect, so
`_` just like a named pattern should be allowed to become
a hole.

Resolves: rdar://96997534
Instead of `CTP_Initialization` transform should use `CTP_CaseStmt`
to identify the root correctly.
`_` pattern doesn't have a type, it's always contextual, so let's
allow it to assume a type of initializer expression instead of
creating a new type variable. This helps to makes sure that initializer
would never get a placeholder type from `_` pattern it's associated
with.
In cases like `.<name>(_)`, the `_` sub-pattern should not assume
locator of the parent paren, just like it doesn't in case of tuple
i.e. `.<name>(_, ...)` where each element gets a `PatternMatch(<elt>)`
locator.
@xedin
Copy link
Contributor Author

xedin commented Aug 4, 2022

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Aug 4, 2022

@swift-ci please test source compatibility

2 similar comments
@xedin
Copy link
Contributor Author

xedin commented Aug 4, 2022

@swift-ci please test source compatibility

@xedin
Copy link
Contributor Author

xedin commented Aug 4, 2022

@swift-ci please test source compatibility

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.

3 participants