Skip to content

Commit f76ee88

Browse files
committed
Fix location issue for the treatment of async functions
Fixes https://github.com/rescript-lang/rescript-vscode/issues/698
1 parent dd4837d commit f76ee88

File tree

5 files changed

+37
-48
lines changed

5 files changed

+37
-48
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- Fix issue with using alias and default value together https://github.com/rescript-lang/syntax/pull/734
2929
- Fix issue in `Js.Promise2` where `then` and `catch` were returning `undefined` https://github.com/rescript-lang/rescript-compiler/pull/5996
3030
- Fix issue in the compiler back-end where async functions passed to an `@uncurry` external would be inlined and transformed in a way that loses async https://github.com/rescript-lang/rescript-compiler/pull/6011
31+
- Fix location issue for the treatment of `async` functions where hovering on the body with a type error would show `'a => promise<'a>` everywhere https://github.com/rescript-lang/rescript-compiler/pull/6014
3132

3233
#### :rocket: New Feature
3334

jscomp/frontend/ast_async.ml

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
let add_promise_type ~async (result : Parsetree.expression) =
1+
let add_promise_type ?(loc=Location.none) ~async (result : Parsetree.expression) =
22
if async then
3-
let txt =
4-
Longident.Ldot (Longident.Ldot (Lident "Js", "Promise"), "unsafe_async")
3+
let unsafe_async =
4+
Ast_helper.Exp.ident ~loc
5+
{ txt = Ldot (Ldot (Lident "Js", "Promise"), "unsafe_async"); loc }
56
in
6-
let pexp_desc = Parsetree.Pexp_ident { txt; loc = result.pexp_loc } in
7-
{
8-
result with
9-
pexp_desc = Pexp_apply ({ result with pexp_desc }, [ (Nolabel, result) ]);
10-
}
7+
Ast_helper.Exp.apply ~loc unsafe_async [ (Nolabel, result) ]
118
else result
129

1310
let add_async_attribute ~async (body : Parsetree.expression) =
@@ -20,16 +17,16 @@ let add_async_attribute ~async (body : Parsetree.expression) =
2017
}
2118
else body
2219

23-
let rec add_promise_to_result (e : Parsetree.expression) =
20+
let rec add_promise_to_result ~loc (e : Parsetree.expression) =
2421
match e.pexp_desc with
2522
| Pexp_fun (label, eo, pat, body) ->
26-
let body = add_promise_to_result body in
23+
let body = add_promise_to_result ~loc body in
2724
{ e with pexp_desc = Pexp_fun (label, eo, pat, body) }
28-
| _ -> add_promise_type ~async:true e
25+
| _ -> add_promise_type ~loc ~async:true e
2926

3027
let make_function_async ~async (e : Parsetree.expression) =
3128
if async then
3229
match e.pexp_desc with
33-
| Pexp_fun _ -> add_async_attribute ~async (add_promise_to_result e)
30+
| Pexp_fun (_, _, {ppat_loc}, _) -> add_promise_to_result ~loc:ppat_loc e
3431
| _ -> assert false
3532
else e

lib/4.06.1/unstable/js_compiler.ml

+9-12
Original file line numberDiff line numberDiff line change
@@ -265941,16 +265941,13 @@ end
265941265941
module Ast_async
265942265942
= struct
265943265943
#1 "ast_async.ml"
265944-
let add_promise_type ~async (result : Parsetree.expression) =
265944+
let add_promise_type ?(loc=Location.none) ~async (result : Parsetree.expression) =
265945265945
if async then
265946-
let txt =
265947-
Longident.Ldot (Longident.Ldot (Lident "Js", "Promise"), "unsafe_async")
265946+
let unsafe_async =
265947+
Ast_helper.Exp.ident ~loc
265948+
{ txt = Ldot (Ldot (Lident "Js", "Promise"), "unsafe_async"); loc }
265948265949
in
265949-
let pexp_desc = Parsetree.Pexp_ident { txt; loc = result.pexp_loc } in
265950-
{
265951-
result with
265952-
pexp_desc = Pexp_apply ({ result with pexp_desc }, [ (Nolabel, result) ]);
265953-
}
265950+
Ast_helper.Exp.apply ~loc unsafe_async [ (Nolabel, result) ]
265954265951
else result
265955265952

265956265953
let add_async_attribute ~async (body : Parsetree.expression) =
@@ -265963,17 +265960,17 @@ let add_async_attribute ~async (body : Parsetree.expression) =
265963265960
}
265964265961
else body
265965265962

265966-
let rec add_promise_to_result (e : Parsetree.expression) =
265963+
let rec add_promise_to_result ~loc (e : Parsetree.expression) =
265967265964
match e.pexp_desc with
265968265965
| Pexp_fun (label, eo, pat, body) ->
265969-
let body = add_promise_to_result body in
265966+
let body = add_promise_to_result ~loc body in
265970265967
{ e with pexp_desc = Pexp_fun (label, eo, pat, body) }
265971-
| _ -> add_promise_type ~async:true e
265968+
| _ -> add_promise_type ~loc ~async:true e
265972265969

265973265970
let make_function_async ~async (e : Parsetree.expression) =
265974265971
if async then
265975265972
match e.pexp_desc with
265976-
| Pexp_fun _ -> add_async_attribute ~async (add_promise_to_result e)
265973+
| Pexp_fun (_, _, {ppat_loc}, _) -> add_promise_to_result ~loc:ppat_loc e
265977265974
| _ -> assert false
265978265975
else e
265979265976

lib/4.06.1/unstable/js_playground_compiler.ml

+9-12
Original file line numberDiff line numberDiff line change
@@ -265941,16 +265941,13 @@ end
265941265941
module Ast_async
265942265942
= struct
265943265943
#1 "ast_async.ml"
265944-
let add_promise_type ~async (result : Parsetree.expression) =
265944+
let add_promise_type ?(loc=Location.none) ~async (result : Parsetree.expression) =
265945265945
if async then
265946-
let txt =
265947-
Longident.Ldot (Longident.Ldot (Lident "Js", "Promise"), "unsafe_async")
265946+
let unsafe_async =
265947+
Ast_helper.Exp.ident ~loc
265948+
{ txt = Ldot (Ldot (Lident "Js", "Promise"), "unsafe_async"); loc }
265948265949
in
265949-
let pexp_desc = Parsetree.Pexp_ident { txt; loc = result.pexp_loc } in
265950-
{
265951-
result with
265952-
pexp_desc = Pexp_apply ({ result with pexp_desc }, [ (Nolabel, result) ]);
265953-
}
265950+
Ast_helper.Exp.apply ~loc unsafe_async [ (Nolabel, result) ]
265954265951
else result
265955265952

265956265953
let add_async_attribute ~async (body : Parsetree.expression) =
@@ -265963,17 +265960,17 @@ let add_async_attribute ~async (body : Parsetree.expression) =
265963265960
}
265964265961
else body
265965265962

265966-
let rec add_promise_to_result (e : Parsetree.expression) =
265963+
let rec add_promise_to_result ~loc (e : Parsetree.expression) =
265967265964
match e.pexp_desc with
265968265965
| Pexp_fun (label, eo, pat, body) ->
265969-
let body = add_promise_to_result body in
265966+
let body = add_promise_to_result ~loc body in
265970265967
{ e with pexp_desc = Pexp_fun (label, eo, pat, body) }
265971-
| _ -> add_promise_type ~async:true e
265968+
| _ -> add_promise_type ~loc ~async:true e
265972265969

265973265970
let make_function_async ~async (e : Parsetree.expression) =
265974265971
if async then
265975265972
match e.pexp_desc with
265976-
| Pexp_fun _ -> add_async_attribute ~async (add_promise_to_result e)
265973+
| Pexp_fun (_, _, {ppat_loc}, _) -> add_promise_to_result ~loc:ppat_loc e
265977265974
| _ -> assert false
265978265975
else e
265979265976

lib/4.06.1/whole_compiler.ml

+9-12
Original file line numberDiff line numberDiff line change
@@ -276328,16 +276328,13 @@ end
276328276328
module Ast_async
276329276329
= struct
276330276330
#1 "ast_async.ml"
276331-
let add_promise_type ~async (result : Parsetree.expression) =
276331+
let add_promise_type ?(loc=Location.none) ~async (result : Parsetree.expression) =
276332276332
if async then
276333-
let txt =
276334-
Longident.Ldot (Longident.Ldot (Lident "Js", "Promise"), "unsafe_async")
276333+
let unsafe_async =
276334+
Ast_helper.Exp.ident ~loc
276335+
{ txt = Ldot (Ldot (Lident "Js", "Promise"), "unsafe_async"); loc }
276335276336
in
276336-
let pexp_desc = Parsetree.Pexp_ident { txt; loc = result.pexp_loc } in
276337-
{
276338-
result with
276339-
pexp_desc = Pexp_apply ({ result with pexp_desc }, [ (Nolabel, result) ]);
276340-
}
276337+
Ast_helper.Exp.apply ~loc unsafe_async [ (Nolabel, result) ]
276341276338
else result
276342276339

276343276340
let add_async_attribute ~async (body : Parsetree.expression) =
@@ -276350,17 +276347,17 @@ let add_async_attribute ~async (body : Parsetree.expression) =
276350276347
}
276351276348
else body
276352276349

276353-
let rec add_promise_to_result (e : Parsetree.expression) =
276350+
let rec add_promise_to_result ~loc (e : Parsetree.expression) =
276354276351
match e.pexp_desc with
276355276352
| Pexp_fun (label, eo, pat, body) ->
276356-
let body = add_promise_to_result body in
276353+
let body = add_promise_to_result ~loc body in
276357276354
{ e with pexp_desc = Pexp_fun (label, eo, pat, body) }
276358-
| _ -> add_promise_type ~async:true e
276355+
| _ -> add_promise_type ~loc ~async:true e
276359276356

276360276357
let make_function_async ~async (e : Parsetree.expression) =
276361276358
if async then
276362276359
match e.pexp_desc with
276363-
| Pexp_fun _ -> add_async_attribute ~async (add_promise_to_result e)
276360+
| Pexp_fun (_, _, {ppat_loc}, _) -> add_promise_to_result ~loc:ppat_loc e
276364276361
| _ -> assert false
276365276362
else e
276366276363

0 commit comments

Comments
 (0)