Skip to content

Commit

Permalink
Merge pull request #540 from patricoferris/match-compilers-dummy-loc
Browse files Browse the repository at this point in the history
Match compilers dummy loc
  • Loading branch information
patricoferris authored Feb 18, 2025
2 parents 3cc021c + f8c8650 commit 75b2411
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 95 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
unreleased
----------

- Change `Location.none` to match the compiler's `Location.none` as of OCaml
4.08. This fixes a bug in `loc_of_attribute` (#540, @ncik-roberts, @patricoferris)

- Bump ppxlib's AST to 5.2.0 (#514, @patricoferris)

- Add a `-raise-embedded-errors` flag to the driver. Setting this flag raises the first
Expand Down
4 changes: 2 additions & 2 deletions src/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ let loc_of_attribute { attr_name; attr_payload; attr_loc = _ } =
from older asts. *)
(* "ocaml.doc" attributes are generated with [Location.none], which is not helpful for
error messages. *)
if Poly.( = ) attr_name.loc Location.none then
if Location.is_none attr_name.loc then
loc_of_name_and_payload attr_name attr_payload
else
{
Expand All @@ -158,7 +158,7 @@ let loc_of_attribute { attr_name; attr_payload; attr_loc = _ } =
}

let loc_of_extension (name, payload) =
if Poly.( = ) name.loc Location.none then loc_of_name_and_payload name payload
if Location.is_none name.loc then loc_of_name_and_payload name payload
else
{ name.loc with loc_end = (loc_of_name_and_payload name payload).loc_end }

Expand Down
3 changes: 2 additions & 1 deletion src/location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type t = location = {
}

let in_file name =
let loc = { pos_fname = name; pos_lnum = 1; pos_bol = 0; pos_cnum = -1 } in
let loc = { pos_fname = name; pos_lnum = 0; pos_bol = 0; pos_cnum = -1 } in
{ loc_start = loc; loc_end = loc; loc_ghost = true }

let set_filename loc fn =
Expand All @@ -17,6 +17,7 @@ let set_filename loc fn =
{ loc with loc_start; loc_end }

let none = in_file "_none_"
let is_none v = Poly.( = ) v none

let init lexbuf fname =
let open Lexing in
Expand Down
3 changes: 3 additions & 0 deletions src/location.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ val set_filename : t -> string -> t
val none : t
(** An arbitrary value of type [t]; describes an empty ghost range. *)

val is_none : t -> bool
(** Checks whether a given location is equal to [none] *)

val init : Lexing.lexbuf -> string -> unit
(** Set the file name and line number of the [lexbuf] to be the start of the
named file. *)
Expand Down
7 changes: 7 additions & 0 deletions test/location/attributes/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(executable
(name pp)
(libraries ppxlib))

(cram
(applies_to print_attr_loc)
(deps pp.exe))
18 changes: 18 additions & 0 deletions test/location/attributes/pp.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
open Ppxlib

let pp_attr str =
let iter =
object
inherit Ast_traverse.iter as super

method! attribute v =
let loc = loc_of_attribute v in
Format.printf "%a %s" Location.print loc v.attr_name.txt;
super#attribute v
end
in
iter#structure str;
str

let () = Driver.register_transformation ~impl:pp_attr "print-attributes"
let () = Ppxlib.Driver.standalone ()
15 changes: 15 additions & 0 deletions test/location/attributes/print_attr_loc.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
The compiler inserts documentation comments with their location set to
`Location.none`. The value for `Location.none` has changed in the compiler (at
4.08.0). We provide a function, `loc_of_attribute` to handle deriving better location
errors for attributes with a none location.

$ cat > test.ml << EOF
> let v = 1
> (** A documentation comment! *)
> EOF

We run an identity driver that prints the locations of attributes.

$ ./pp.exe --impl test.ml -o ignore.ml
File "test.ml", line 2, characters 0-31: ocaml.doc

Loading

0 comments on commit 75b2411

Please sign in to comment.