Skip to content

Keep the field name when renaming a variable bound by a pun or wildcard - #5192

Open
swinilohani616-dot wants to merge 10 commits into
haskell:masterfrom
swinilohani616-dot:fix-record-wildcard-rename
Open

swinilohani616-dot wants to merge 10 commits into
haskell:masterfrom
swinilohani616-dot:fix-record-wildcard-rename

Conversation

@swinilohani616-dot

@swinilohani616-dot swinilohani616-dot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5032.

If you rename a local variable that a record wildcard or a pun uses, HLS renames the record field too. For example, renaming foo in

makeFoo foo baz = MkFoo {..}

also renames the field foo in the data declaration, and everywhere else it is used.

This comes from the extra pass for NamedFieldPuns added in #3013. It renames every other name with the same spelling that sits at one of the variable's locations, and with {..} or a pun, the field sits at exactly the same place as the variable.

With this change, renaming a local variable leaves the field alone, and the record is changed to name the field explicitly instead:

  • MkFoo {..} becomes MkFoo {foo = bar, ..}. This works both when building a record and when matching on one.
  • Foo{field} becomes Foo{field = bleh}. This works in patterns, record construction and record updates.

Renaming the field itself works as before, and the puns are still renamed along with it.

A few things worth pointing out:

  • The record is edited in place rather than printed again, so its layout and comments are kept: a pun becomes a field binding, and for .. a new field binding is added in front of it. The new nodes are built by hand with the helpers from hls-exactprint-utils. Moving the .. needs different handling before and after GHC 9.10, which Note [Renaming variables bound by puns and wildcards] explains.
  • I added five tests: Record wildcard local variable, Record wildcard pattern variable, Record wildcard keeps layout, Field pun local variable and Record update pun. All five fail without this change. I ran the rename tests with GHC 9.8.4, 9.10.3 and 9.14.1, and am relying on CI for 9.6 and 9.12.
  • I read and followed our AI policy
  • I proposed a solution draft in the corresponding issue (for non-trivial changes and new features)
    • This is especially encouraged for new contributors

When a local variable shares a location with a record field, through a
pun or a record wildcard, renaming the variable also renamed the field.
Now the field keeps its name, and the record is rewritten to name the
field explicitly: Foo {field} becomes Foo {field = new}, and Foo {..}
becomes Foo {field = new, ..}.
Before GHC 9.10, graftWithM puts the new pattern right after the previous
token, so getFoo MkFoo {..} became getFooMkFoo {foo = bar, ..}. Graft
record patterns with a variant that keeps the spacing of the original
pattern.

@simonhorlick simonhorlick left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice PR, can't wait to use this! I have made some suggestions that could help resolve some of the issues.

Printing the whole record again lost its layout and comments. Now only
its fields change: a pun becomes a field binding, and for a record
wildcard a new field binding is added in front of the .., so the rest of
the record prints as it was. Moving the .. needs different handling
before and after GHC 9.10, see Note [Renaming variables bound by puns
and wildcards].

Also rewrite puns in record updates. They were left as puns, so after
renaming the variable they referred to the field selector instead.
renamingLocalVariable = all isInternalName directOldNames
isSharedField n = renamingLocalVariable && isExternalName n && matchesDirect n
sharedFields = [(ref, nameOccName n) | (ref, names) <- namesAtRefs, n <- names, isSharedField n]
oldNames = filter (\n -> matchesDirect n && not (isSharedField n)) indirectOldNames ++ directOldNames

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you define (<&&>) = liftA2 (&&) (or import it from) somewhere, you can rewrite this lambda as matchesDirect n <&&> not . isSharedField. I think it's a bit more readable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you define (<&&>) = liftA2 (&&) (or import it from) somewhere, you can rewrite this lambda as matchesDirect n <&&> not . isSharedField. I think it's a bit more readable.

Thanks! Rather than add an operator for this one place, I split it into two filters, which removes the lambda too: filter (not . isSharedField) (filter matchesDirect indirectOldNames) (4212a43). Happy to switch to <&&> if you prefer it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My suggestion was crippled, sorry, I meant that (\n -> matchesDirect n && not (isSharedField n)) would change to matchesDirect <&&> not . isSharedField. Anyway, I have little clue as to whether filter p1 (filter p2 xs) and filter (p1 <&&> p2) xs generated the same code or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My suggestion was crippled, sorry, I meant that (\n -> matchesDirect n && not (isSharedField n)) would change to matchesDirect <&&> not . isSharedField. Anyway, I have little clue as to whether filter p1 (filter p2 xs) and filter (p1 <&&> p2) xs generated the same code or not.

No worries, that is how I read it. They should come out the same: with optimisation on, base's filterFB rewrite rule fuses filter p (filter q xs) into a single filter with \x -> q x && p x, which here is exactly the original lambda. The list only holds the names found at the variable's references anyway, so it's tiny either way. Happy to switch to <&&> if you find it clearer.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These are the alternatives we've referred to in decreasing preference order:

  1. <&&>,
  2. the original code,
  3. the code that calls filter twice.

Obviously, my opinion is not law.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These are the alternatives we've referred to in decreasing preference order:

  1. <&&>,
  2. the original code,
  3. the code that calls filter twice.

Obviously, my opinion is not law.

Switched to <&&> in 0ad8a57, defined next to the plugin's other helpers with the same fixity as &&.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can't you just use it from https://hackage-content.haskell.org/package/ghc-9.14.2/docs/GHC-Utils-Misc.html#v:-60--38--38--62- ?

Good find, thanks. It is there with the same definition and fixity in every GHC we support (9.6 to 9.14), so I have dropped my copy and imported it from GHC.Utils.Misc instead.

Skipping names without a source location was only needed when the
record was rewritten with a graft.
@Aster89 Aster89 mentioned this pull request Dec 3, 2026
2 tasks done
@swinilohani616-dot

Copy link
Copy Markdown
Contributor Author

Fixes #5032.

If you rename a local variable that a record wildcard or a pun uses, HLS renames the record field too. For example, renaming foo in

makeFoo foo baz = MkFoo {..}

also renames the field foo in the data declaration, and everywhere else it is used.

This comes from the extra pass for NamedFieldPuns added in #3013. It renames every other name with the same spelling that sits at one of the variable's locations, and with {..} or a pun, the field sits at exactly the same place as the variable.

With this change, renaming a local variable leaves the field alone, and the record is changed to name the field explicitly instead:

  • MkFoo {..} becomes MkFoo {foo = bar, ..}. This works both when building a record and when matching on one.
  • Foo{field} becomes Foo{field = bleh}. This works in patterns, record construction and record updates.

Renaming the field itself works as before, and the puns are still renamed along with it.

A few things worth pointing out:

  • The record is edited in place rather than printed again, so its layout and comments are kept: a pun becomes a field binding, and for .. a new field binding is added in front of it. The new nodes are built by hand with the helpers from hls-exactprint-utils. Moving the .. needs different handling before and after GHC 9.10, which Note [Renaming variables bound by puns and wildcards] explains.

  • I added five tests: Record wildcard local variable, Record wildcard pattern variable, Record wildcard keeps layout, Field pun local variable and Record update pun. All five fail without this change. I ran the rename tests with GHC 9.8.4, 9.10.3 and 9.14.1, and am relying on CI for 9.6 and 9.12.

  • I read and followed our AI policy

  • I proposed a solution draft in the corresponding issue (for non-trivial changes and new features)

    • This is especially encouraged for new contributors

CI is green except test (9.12, ubuntu-latest, true), which failed after about 4 minutes, before it got to the tests. The same thing happened to test (9.10, ubuntu-latest, true) on the previous push, and that job passed this time. The other test jobs ran 20 to 30 minutes and passed, including 9.12 on macOS and Windows, so I think it's a flaky setup step rather than this change. Could someone rerun the failed job? Thanks!

A name at a reference of the renamed variable is now taken to be the field
of a pun or wildcard only if the HIE file gives it a RecField context
there, rather than if it is an external name.

Export noAnnSrcSpanDP0 from hls-exactprint-utils, next to
noAnnSrcSpanDP1, and use it for the field of a new field binding instead
of CPP. Before GHC 9.10 this needs a Monoid instance for NoEpAnns, which
GHC does not have, so add one to the orphans.

This branch has not been deployed

No deployments
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.

Renaming a variable that gets used in a record wild card will rename the field of that record.

3 participants