Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async function with @this attribute #6100

Closed
Mepy opened this issue Mar 27, 2023 · 5 comments
Closed

async function with @this attribute #6100

Mepy opened this issue Mar 27, 2023 · 5 comments
Labels
Milestone

Comments

@Mepy
Copy link

Mepy commented Mar 27, 2023

// ReScript
let add = x=>async(y)=> x+y
let method = @this this=> async arg=>await this->add(arg)

In rescript v10.1.3, the example above will fail as the following :

FAILED: 
  We've found a bug for you!

  1 │ // ReScript
  2 │ let add = x=>async(y)=> x+y
  3 │ let method = @this this=> async arg=>await this->add(arg) 
  4 │ 

  Await on expression not in an async context

By the way, the following ReScript code will generate JavaScript code without async:

// ReScript
let method = @this this=> async arg=>this+arg
// JavaScript
function method(arg) {
  var $$this = this ;
  return $$this + arg | 0;
}
@cristianoc
Copy link
Collaborator

Thank you for reporting this.
Likely the compiler's ppx implementation of @this eats up the async attribute. And it always did. Only we did not use attributes before (an attribute is how async looks like in the AST).

@cristianoc cristianoc added this to the v11.0 milestone Apr 3, 2023
@cristianoc
Copy link
Collaborator

To verify the hypothesis, let's put the second example in tst.res and do:

./bsc -dsource tst.res   
let method =
  Js_OO.unsafe_to_method
    ({ I2 = (fun this -> fun arg -> this + arg) } : _ Js_OO.Callback.arity2)

there's no attribute.

Instead a single async function would look like this internally:

let foo = ((fun arg -> Js.Promise.unsafe_async (arg + 1))[@res.async ])

So this seems to confirm the hypothesis.

@cristianoc
Copy link
Collaborator

cristianoc commented Apr 3, 2023

Looking at this cheat sheet, the @this decorator was left out. As it's the most complex of them all.
In a future clean up that decorator would be the first to go.
But for now, let's look into fixing it.

@cristianoc
Copy link
Collaborator

Looks like this https://github.com/rescript-lang/rescript-compiler/blob/master/jscomp/frontend/ast_core_type_class_type.ml#L84 is the pace where it is handled.

@cristianoc
Copy link
Collaborator

Here's the fix:
#6105

The issue and fix are as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants