# Ppxlib: Type check the generated code?

**URL:** <https://discuss.ocaml.org/t/ppxlib-type-check-the-generated-code/7792>\
**Category:** Learning\
**Created:** [May 6, 2021, 7:25am UTC](https://discuss.ocaml.org/t/ppxlib-type-check-the-generated-code/7792 "2021-05-06T07:25:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vch9](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vch9/32/2885_2.png) [@vch9](https://discuss.ocaml.org/u/vch9)\
**Post date:** [May 6, 2021, 7:25am UTC](https://discuss.ocaml.org/t/ppxlib-type-check-the-generated-code/7792/1 "2021-05-06T07:25:52Z")

</div>

Hi everyone.

I am currently trying to develop a ppx rewriter that inline code based  
on a string payload:

```ocaml
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:

```ocaml
let f x y = x + y
let _ = f 42 "hello"

```

And then produce an error:

```ocaml
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_"`.

---

<div class="post-metadata">

**Author:** ![EduardoRFS](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/eduardorfs/32/2259_2.png) [@EduardoRFS](https://discuss.ocaml.org/u/EduardoRFS)\
**Post date:** [May 6, 2021, 7:34am UTC](https://discuss.ocaml.org/t/ppxlib-type-check-the-generated-code/7792/2 "2021-05-06T07:34:36Z")

</div>

The error message is related how you handle locations, almost always `_none_` happens because you’re using `Location.none` somewhere

---

<div class="post-metadata">

**Author:** ![vch9](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vch9/32/2885_2.png) [@vch9](https://discuss.ocaml.org/u/vch9)\
**Post date:** [May 6, 2021, 7:56am UTC](https://discuss.ocaml.org/t/ppxlib-type-check-the-generated-code/7792/3 "2021-05-06T07:56:52Z")

</div>

Indeed thank you, I was using `Location.none` **everywhere**. Now that I use the location of my attribute I can produce that kind of error:

```ocaml
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 ?

---

<div class="post-metadata">

**Author:** ![EduardoRFS](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/eduardorfs/32/2259_2.png) [@EduardoRFS](https://discuss.ocaml.org/u/EduardoRFS)\
**Post date:** [May 6, 2021, 8:03am UTC](https://discuss.ocaml.org/t/ppxlib-type-check-the-generated-code/7792/4 "2021-05-06T08:03:34Z")

</div>

> [@vch9](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![vch9](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vch9/32/2885_2.png) [@vch9](https://discuss.ocaml.org/u/vch9)\
**Post date:** [May 6, 2021, 8:22am UTC](https://discuss.ocaml.org/t/ppxlib-type-check-the-generated-code/7792/5 "2021-05-06T08:22:34Z")

</div>

In theory I would want to create a location on the generated code:

```ocaml
let f x y = x + y
let _ f "42" foo
        ^^^^
Error: This expression has type string but an expression
          was expected of type int

```

But that doesn’t seems to be possible, I might use my attribute location as you suggested.  
Thank you very much for the help !
