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

Adjust several locations where required types are discussed #2884

Merged
merged 7 commits into from
Mar 15, 2023

Conversation

eernstg
Copy link
Member

@eernstg eernstg commented Mar 3, 2023

This PR changes several locations in the patterns feature specification where the 'required type' of a pattern is discussed. It adds rules about the required type in some cases where it was left unspecified. In particular, it says that the constant expression in a relationalPattern is type checked with the context type _ (I can't see that we have anything else). The required type of a relationalPattern is kept open (Object?). The actual checks associated with this kind of pattern are performed during the traversal of the pattern, and there is no single type T would would make the same checks occur just by being the matched value type.

The changes include a somewhat controversial part: The definition of "assignable". The new text changes the order of the two bullet points where assignability is discussed: First we check whether there is a need for a coercion, and insert it if needed, and obtain a new M (so M might have been X Function<X>(X) and is now int Function(int)). Next, we check that M is assignable to the required type of the pattern.

This approach is compatible with both definitions of 'assignable' (both the one in the nnbd feature specification and the language specification, and the one that includes coercions): We just don't rely on 'assignability' to take coercions into account, and then it doesn't matter whether or not they are part of 'assignability'. ;-)

Other changes:

I changed all references to primitive equality to use the phrase 'primitive equality'. There are several locations where 'operator ==' is mentioned specifically, and some of them have been preserved, because it was actually specifically about this operator.

I added a few words to indicate that 'pattern type checking' can include provision of missing parts (so [var j] could be transformed into <double>[var j] at this time, based on M).

Copy link
Member

@stereotype441 stereotype441 left a comment

Choose a reason for hiding this comment

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

lgtm assuming some clarifying informative text is added, see my comment.

Copy link
Member

@munificent munificent left a comment

Choose a reason for hiding this comment

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

This looks great. Thank you for clarifying it!

Copy link
Member Author

@eernstg eernstg left a comment

Choose a reason for hiding this comment

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

Review response.

The paragraph about type checks applied to relational expressions had to be rewritten extensively, because of the ordering dependencies (e.g., we need to find the operator and the type of the operator parameter before we can type check the constant expression).

I used the text from @stereotype441 as a starting point for a small section about coercions.

I've weaseled around the distinction between the two models (coercion == transformation of an expression in-situ, vs. coercion == semantic step where the same operation is performed, but presumably on a temporary variable which is a built-in property of the semantics of the given pattern matching, assignment, etc.).

@eernstg
Copy link
Member Author

eernstg commented Mar 10, 2023

Thanks for feedback!

@lrhn, do you have further comments?

Copy link
Member Author

@eernstg eernstg left a comment

Choose a reason for hiding this comment

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

Review response

Copy link
Member Author

@eernstg eernstg left a comment

Choose a reason for hiding this comment

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

Review response.

I don't think we're in a position to introduce a notion of "promoted matched value type after failed match" with || patterns. Otherwise I think the issues have been resolved.

Copy link
Member

@munificent munificent left a comment

Choose a reason for hiding this comment

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

Still LGTM.

Copy link
Member Author

@eernstg eernstg left a comment

Choose a reason for hiding this comment

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

Latest review response: Logical-or patterns now do require promotions based on a failing match in the first operand to be taken into account.

@eernstg eernstg merged commit abca38c into master Mar 15, 2023
@eernstg eernstg deleted the spec_required_type_mar23 branch March 15, 2023 10:13
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