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

Preserve integer literal formatting in type hint #10818

Merged
merged 1 commit into from Jul 14, 2022

Conversation

curiousleo
Copy link
Contributor

Closes #10766, implementing "Proposal 1".

This is a more localised implementation than the implementation proposed in #10767, which changes Asttype.constant. It is based on this comment.

User-visible changes

The goal of this PR is to preserve the original formatting of integer literals (int32, int64 and nativeint) in the "Did you mean ...?" hint.

Before

    1 | let _ = Int64.add 0xAA_BBl 2L;;
                          ^^^^^^^^
    Error: This expression has type int32 but an expression was expected of type
             int64
      Hint: Did you mean `43707L'?

After

    1 | let _ = Int64.add 0xAA_BBl 2L;;
                          ^^^^^^^^
    Error: This expression has type int32 but an expression was expected of type
             int64
      Hint: Did you mean `0xAA_BBL'?

API changes

In Typecore.error:

  • Pattern_type_clash contains an optional Parsetree.pattern_desc instead of Typetree.pattern_desc
  • Expr_type_clash contains an optional Parsetree.expression_desc instead of Typetree.expression_desc

In both cases, the description was and is only used to output the type hint. The difference is that the Parsetree descriptions contain the original formatting of the integer literal, but the Typetree descriptions do not. Using the Parsetree descriptions instead of the Typetree descriptions allows us to output the integer literals with their original formatting in the type hint.

@curiousleo
Copy link
Contributor Author

Ready for review. @gasche and @Octachron, you have the context from #10767, perhaps you would be willing to take a look?

@Octachron
Copy link
Member

Unsurprisingly, I prefer this implementation to #10767 .

Changes Outdated Show resolved Hide resolved
typing/typecore.ml Outdated Show resolved Hide resolved
@curiousleo curiousleo force-pushed the preserve-integer-literal-format-2 branch from 7113323 to 52b49bf Compare December 12, 2021 14:58
@curiousleo
Copy link
Contributor Author

Thanks for the suggestions for improvement! All comments addressed, ready for another look.

@curiousleo curiousleo force-pushed the preserve-integer-literal-format-2 branch from 52b49bf to 6b83fe0 Compare December 13, 2021 13:44
@Octachron
Copy link
Member

The current state looks good to me, I am adding this PR to the post-freeze milestone.

@Octachron Octachron added this to the post-freeze milestone Jan 28, 2022
@curiousleo
Copy link
Contributor Author

The current state looks good to me, I am adding this PR to the post-freeze milestone.

Awesome! I'll rebase on top of trunk and resolve the conflicts.

@Octachron
Copy link
Member

Octachron commented Jan 28, 2022

Note that you may want to wait before resolving conflicts, there is still many weeks left before the end of the freeze and Changes is quite conflict-prone.

@curiousleo curiousleo force-pushed the preserve-integer-literal-format-2 branch from 49ce179 to 0d4a77a Compare January 28, 2022 17:30
@curiousleo
Copy link
Contributor Author

Note that you may want to wait before resolving conflicts, there is still many weeks left before the end of the freeze and Changes is quite conflict-prone.

Cool, good to know. I only saw your message after rebasing, but I'll keep in mind that conflicts in Changes are to be expected.

Will I be notified when I should rebase to get the PR ready for merging? (This is my first contribution to the OCaml compiler.)

@Octachron
Copy link
Member

Yes, I will notify you when the merge windows reopen.

@Octachron
Copy link
Member

@curiousleo : the merge windows has reopened if you wish to rebase your PR it can be merged.

@curiousleo curiousleo force-pushed the preserve-integer-literal-format-2 branch from 0d4a77a to af9bf21 Compare July 5, 2022 10:46
@curiousleo curiousleo requested a review from Octachron July 5, 2022 10:52
@curiousleo
Copy link
Contributor Author

Rebase is done!

@curiousleo curiousleo requested a review from gasche July 6, 2022 01:08
Copy link
Member

@Octachron Octachron left a comment

Choose a reason for hiding this comment

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

The PR looks good to me: it improves error messages by doing less work overall.

typing/typecore.ml Show resolved Hide resolved
typing/typecore.ml Show resolved Hide resolved
@curiousleo curiousleo force-pushed the preserve-integer-literal-format-2 branch from af9bf21 to 0996df2 Compare July 13, 2022 01:31
@curiousleo
Copy link
Contributor Author

All review comments addressed.

@curiousleo
Copy link
Contributor Author

@Octachron thanks for taking a look! What's the next step?

@gasche
Copy link
Member

gasche commented Jul 14, 2022

Octachron is on holidays today and tomorrow, I'll go ahead and merge if the CI is happy.

Before:

    1 | let _ = Int64.add 0xAA_BBl 2L;;
                          ^^^^^^^^
    Error: This expression has type int32 but an expression was expected of type
             int64
      Hint: Did you mean `43707L'?

After:

    1 | let _ = Int64.add 0xAA_BBl 2L;;
                          ^^^^^^^^
    Error: This expression has type int32 but an expression was expected of type
             int64
      Hint: Did you mean `0xAA_BBL'?
@curiousleo curiousleo force-pushed the preserve-integer-literal-format-2 branch from 0996df2 to ef84311 Compare July 14, 2022 13:28
@curiousleo
Copy link
Contributor Author

curiousleo commented Jul 14, 2022

Octachron is on holidays today and tomorrow, I'll go ahead and merge if the CI is happy.

Thanks! I fixed the line length issue that was flagged by CI:

./typing/typecore.ml:386.81: [long-line] line is over 80 columns
./typing/typecore.ml:2785.81: [long-line] line is over 80 columns

You may need to re-trigger CI so it runs on the new commit, sorry.

@Octachron Octachron merged commit a8c2d61 into ocaml:trunk Jul 14, 2022
@Octachron
Copy link
Member

Merged, thanks for your contribution and patience!

@curiousleo
Copy link
Contributor Author

Merged, thanks for your contribution and patience!

My pleasure, thanks for the thoughtful reviews!

@curiousleo curiousleo deleted the preserve-integer-literal-format-2 branch July 16, 2022 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preserve original formatting in integer literal type hint
4 participants