Skip to content

Conversation

@WardBrian
Copy link
Contributor

Adds option --let-binding-punning={preserve|always|never} to control whether ocamlformat outputs
let punning (open to suggestions on naming).

Default is preserve in all profiles to match the current release's behavior.

Closes #2743

Copy link
Collaborator

@EmileTrotignon EmileTrotignon left a comment

Choose a reason for hiding this comment

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

I think this is good, except maybe the documentation which could have an exemple and more explicit wording.

Name punning in extended let bindings preserve uses let-punning
only when it exists in the source. always uses let-punning whenever
possible. never never uses let-punning. The default value is
preserve.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know this is not the style of the current documentation, but I think an example of what let punning is should included.

Also regarding the name, I thought a let-binding is any use of let, but this only concerns bindings with let operators ? This does not have to be reflected in the name, but the documentation should probably say something.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem is that it's not possible to add line breaks in the documentation, forcing the explanation to be concise. This is the output of cmdliner, which removes line breaks and reflow the text.

I'd be totally in favor of a new way of generating the list of option in the documentation and taking advantage of Odoc markup (paragraph, lists, text blocks). The cmdliner output must stay thought.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed the name to your suggestion from the issue, letop-punning. I also made an attempt at including an example in this doc, let me know what you think!

Copy link
Collaborator

Choose a reason for hiding this comment

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

The doc looks good to me ! It's a bit messed up because of the way we pass it through cmdliner but that can be improved independently.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem is that it's not possible to add line breaks in the documentation, forcing the explanation to be concise. This is the output of cmdliner, which removes line breaks and reflow the text.
I wasn't aware, thanks. I agree it would be better with improvement

Copy link
Collaborator

@Julow Julow left a comment

Choose a reason for hiding this comment

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

Other than the discussion on documentation, I'm totally in favor of this change :)

Name punning in extended let bindings preserve uses let-punning
only when it exists in the source. always uses let-punning whenever
possible. never never uses let-punning. The default value is
preserve.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The problem is that it's not possible to add line breaks in the documentation, forcing the explanation to be concise. This is the output of cmdliner, which removes line breaks and reflow the text.

I'd be totally in favor of a new way of generating the list of option in the documentation and taking advantage of Odoc markup (paragraph, lists, text blocks). The cmdliner output must stay thought.

lib/Conf.ml Outdated
; Decl.Value.make ~name:"always" `Always
"$(b,always) uses let-punning whenever possible."
; Decl.Value.make ~name:"never" `Never
"$(b,never) never uses let-punning." ]
Copy link
Collaborator

Choose a reason for hiding this comment

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

To make the difference between never and preserve clearer, I think you should say that existing code using let-punning will be rewritten without.

@WardBrian
Copy link
Contributor Author

Question:
I have the code prepared to also have this setting work for extension lets (e.g. let%bar x = x in ... becomes let%bar x in ....

Would this be better in this PR or in another PR? I don't currently have 'preserve' working for these, because that requires parser changes, but I think I could get that done

@Julow
Copy link
Collaborator

Julow commented Oct 30, 2025

I have the code prepared to also have this setting work for extension lets (e.g. let%bar x = x in ... becomes let%bar x in ....

That's fantastic !
I usually prefer several smaller PRs instead of a large one. Now that this one is reviewed, let's merge it :)

@EmileTrotignon
Copy link
Collaborator

Would this be better in this PR or in another PR? I don't currently have 'preserve' working for these, because that requires parser changes, but I think I could get that done

This is the type of parser changes that we try to do, so please do that ! Don't forget to have the loc of every node you add (maybe in that case all the locs are here already)

Thats because comments moving around are caused by not knowing the loc of certain tokens, and therefore having no way of knowing if the comment was before or after said token.

@EmileTrotignon
Copy link
Collaborator

I am merging once the CI is green (except for that benchmark which always fails)

@WardBrian
Copy link
Contributor Author

Thanks @EmileTrotignon, I was wondering about that one. The rest all look like they passed

@EmileTrotignon
Copy link
Collaborator

Yeah, I think the benchmark has a dependency on libpcre3dev which is a recently defunct debian package.

Lets merge !

@EmileTrotignon EmileTrotignon merged commit 1609f04 into ocaml-ppx:main Oct 30, 2025
11 of 12 checks passed
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.

Bug: let punning changed in 0.27/0.28

3 participants