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

Make typechecking indexed setters with tuples on the right more consistent #17017

Merged
merged 12 commits into from
Apr 19, 2024

Conversation

brianrourkeboll
Copy link
Contributor

@brianrourkeboll brianrourkeboll commented Apr 10, 2024

Description

Fixes #16987.

  • Wrap the value expression in indexed setters in extra parentheses in 3 more cases.

    In the scenarios where assigning an unparenthesized tuple using an indexed setter already works, MakeDelayedSet is actually being called twice — once before TcIndexingThen is called, and once inside of it. For SynExpr.NamedIndexedPropertySet, SynExpr.DotIndexedSet, and, SynExpr.DotNamedIndexedPropertySet, it was not being called twice, because we don't actually want to delay it; the expression does, however, need an extra set of parens in order to receive the same treatment later on as the value expression from SynExpr.Set. Whether double parens should really be needed there is a different question (that I did not investigate).

    Extra parens were already added around the value expr for SynExpr.Set

    // synExpr1 <- synExpr2
    | SynExpr.Set (synExpr1, synExpr2, mStmt) ->
    TcNonControlFlowExpr env <| fun env ->
    TcExprThen cenv overallTy env tpenv false synExpr1 [MakeDelayedSet(synExpr2, mStmt)]

    Extra parens were not added for other indexed setters

    // Type.Items(synExpr1) <- synExpr2
    | SynExpr.NamedIndexedPropertySet (synLongId, synExpr1, synExpr2, mStmt) ->
    TcNonControlFlowExpr env <| fun env ->
    TcExprNamedIndexPropertySet cenv overallTy env tpenv (synLongId, synExpr1, synExpr2, mStmt)

    Extra parens are (also) always added here, which is reached from both of the above paths

    | Some (expr3, mOfLeftOfSet) ->
    if isIndex then
    [ DelayedDotLookup([ident(nm, mOfLeftOfSet)], mOfLeftOfSet)
    DelayedApp(ExprAtomicFlag.Atomic, true, synLeftExprOpt, MakeIndexParam None, mOfLeftOfSet)
    MakeDelayedSet(expr3, mWholeExpr)
    yield! delayed ]

    In this PR, I am simply adding the extra parens inline when SynExpr.NamedIndexedPropertySet, SynExpr.DotIndexedSet, and, SynExpr.DotNamedIndexedPropertySet are encountered during typechecking. I could alternatively have updated MakeDelayedSet itself to always wrap in double parens, although that would mean that, in the codepaths where that function is already being called twice, we'd be adding extra parens unnecessarily. While excess parens do not affect typechecking in this case (unlike insufficient parens), it seems undesirable to allocate more nodes than we really need, only to strip them later anyway.

Checklist

  • Test cases added.
  • Release notes entry updated.

  * In the scenarios where assigning an unparenthesized tuple using an
    indexed setter already works, `MakeDelayedSet` is actually being
    called twice — once before `TcIndexingThen` is called, and once inside
    of it. For `SynExpr.NamedIndexedPropertySet`, `SynExpr.DotIndexedSet`,
    and, `SynExpr.DotNamedIndexedPropertySet`, we don't actually want to
    delay it; it just needs an extra set of parens (whether it should
    really need double parens is a different question). In this commit,
    I am simply adding the extra parens inline. I could alternatively
    have updated `MakeDelayedSet` itself to always wrap in double
    parens, although that would mean that in the codepaths where that
    function is itself already being called twice, we'd be adding extra
    parens parens unnecessarily. While extra parens do not affect
    typechecking (unlike insufficient parens), it seems undesirable to
    allocate more nodes than we really need, only to strip them later
    anyway.
Copy link
Contributor

github-actions bot commented Apr 10, 2024

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/8.0.400.md

@brianrourkeboll brianrourkeboll changed the title Make typechecking of indexed setters with tuples on the right more consistent Make typechecking indexed setters with tuples on the right more consistent Apr 10, 2024
@brianrourkeboll brianrourkeboll marked this pull request as ready for review April 10, 2024 17:24
@brianrourkeboll brianrourkeboll requested a review from a team as a code owner April 10, 2024 17:24
@psfinaki
Copy link
Member

Gathering mental energy for this, hope to review this tomorrow ;)

Copy link
Member

@psfinaki psfinaki left a comment

Choose a reason for hiding this comment

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

Okay this wasn't that hard to understand in the end 🤦

Thanks for the fix and for the thorough testing, consistency in one of the focus areas for F# these days.

@psfinaki psfinaki enabled auto-merge (squash) April 17, 2024 13:46
auto-merge was automatically disabled April 17, 2024 15:23

Head branch was pushed to by a user without write access

@T-Gro T-Gro merged commit 3e326fe into dotnet:main Apr 19, 2024
32 checks passed
@brianrourkeboll brianrourkeboll deleted the indexed-set-tuples branch May 8, 2024 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Inconsistent treatment of <- with tuples
4 participants