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

parsing: Attach a location to the RHS of Ptyp_alias #12639

Merged
merged 4 commits into from
Oct 18, 2023

Conversation

Julow
Copy link
Contributor

@Julow Julow commented Oct 6, 2023

Hi,

This patch is part of OCamlformat's extended parser, which is OCaml's parser with modifications to add missing locations and represent the different concrete syntaxes.

Merging some of these modifications back would help us keep the two parsers in sync but I think would also improve the parser for other uses (eg. error messages, PPX, etc..).

In this case, the change improves an error message.

@Octachron Octachron added the parsetree-change Track changes to the parsetree for that affects ppxs label Oct 6, 2023
1 | external cast : int -> 'self nat as 'self = "%identity"
^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^
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 find it weird that the location does not span the quote but this is the same as in let-bindings.

I propose to change the location to include the quote. The quote is not included in the AST but is logically part of the ident as suggested by this error message:

1 | fun (x : 'a t as 'a) -> ();;
                      ^
Error: This alias is bound to type "'a t" but is used as an instance of type "'a"
       The type variable "'a" occurs inside "'a t"

Copy link
Member

Choose a reason for hiding this comment

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

I tend to agree that it would be better to include the quote in the location.

typing/typetexp.ml Outdated Show resolved Hide resolved
check_variable var_names t.ptyp_loc string;
Ptyp_alias(loop core_type, string)
| Ptyp_alias(core_type, alias) ->
check_variable var_names t.ptyp_loc alias.txt;
Copy link
Member

Choose a reason for hiding this comment

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

The variable is really the alias here, so t.ptyp_localias.loc. If you want to test the change in the error message, this case is triggered by:

let none: type a. (_ as 'a) option = None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in a new commit.

@Octachron
Copy link
Member

Overall, I like the change: it improves a handful of error messages and it makes the parsetree more precise on a meaningful location. I have few fine tuning remarks left however.

Julow and others added 3 commits October 9, 2023 14:59
Before:

    1 | type t = (int as 'a) * (float as 'a)
                                ^^^^^^^^^^^
    Error: This alias is bound to type float but is used as an instance of type
             int

After:

    1 | type t = (int as 'a) * (float as 'a)
                                          ^
    Error: This alias is bound to type float but is used as an instance of type
             int
Use the location of the alias instead of the `_ as 'a` type expression:

    2 | let none: type a. (_ as 'a) option = None
                                 ^
    Error: In this scoped type, variable 'a is reserved for the local type a.

Co-authored-by: Florian Angeletti <florian.angeletti@inria.fr>
@Octachron
Copy link
Member

The current state looks good to me and I don't see foresee much problems for ppxlib, cc @panglesd ?

@Octachron Octachron merged commit e397ed2 into ocaml:trunk Oct 18, 2023
9 checks passed
@Octachron
Copy link
Member

Merged after checking with @panglesd and @pitag-ha that the change should be fine for ppxlib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parsetree-change Track changes to the parsetree for that affects ppxs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants