Skip to content

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

Merged
Octachron merged 4 commits intoocaml:trunkfrom
Julow:loc-ptyp_as
Oct 18, 2023
Merged

parsing: Attach a location to the RHS of Ptyp_alias#12639
Octachron merged 4 commits intoocaml:trunkfrom
Julow:loc-ptyp_as

Conversation

@Julow
Copy link
Copy Markdown
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
Line 1, characters 37-41:
1 | external cast : int -> 'self nat as 'self = "%identity"
^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^
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.

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
Copy Markdown
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.

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@Octachron
Copy link
Copy Markdown
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