I am currently trying to develop a ppx rewriter that inline code based
on a string payload:
let f x y = ..
[@@foo {| payload |}]
In my ppx, it tries to apply f with arguments, and can sometimes produce
ill-typed expressions, for example:
let f x y = x + y
let _ = f 42 "hello"
And then produce an error:
File "_none_", line 1:
Error: This expression has type string but an expression was expected of type
int
I was wondering if there was a possibility to type check the expression f 42 "foo" before, in order
to generate an error message based on the generated code instead of File "_none_".
Indeed thank you, I was using Location.noneeverywhere. Now that I use the location of my attribute I can produce that kind of error:
File "foo.ml"
1 | let f x y = x + y [@@something {| payload |}]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This expression has type string but an expression
was expected of type int
This second question might be a naive as the previous one but, is there a possibility to create a location on the generated code in order to show the error ?
You can in theory, but most of the time if you just want to specificy something, you can use a more specific loc. It seems like you’re using the loc of the entire structure_item, probably you want to use the loc of the attribute.