@directive is dropped on @this functions

Aperta
#9,197 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, ocaml
Ambito
compilers

Direzione di ricerca

Start with the Pjs_fn_method case in compiler/core/lam_compile.ml, then follow how E.method_ builds functions in compiler/core/js_exp_make.ml and how the method branch prints them in Js_dump.pp_function. Check the frontend @this rewrite to confirm the directive stays on the inner function; printlambda does not show directives. Done means @directive appears first in the generated method body, before let self = this, for both examples while preserving async.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A @this function loses its @directive. Nothing is reported, and async on the same function is kept.

let plain = @this @directive("'use cache'") (self, x) => self + x
let action = @this @directive("'use server'") async (self, x) => self + x

compiles to

function plain(x) {
  let self = this ;
  return self + x | 0;
}

async function action(x) {
  let self = this ;
  return self + x | 0;
}

Neither directive appears in the output. Without @this, the directive is kept (function d(n) { 'use cache'; … }).

Cause

@this compiles through %unsafe_to_method to Pjs_fn_method, and that path drops the directive:

  • compiler/core/lam_compile.ml: the Pjs_fn_method case matches attr = {return_unit; async} and never reads directive.
  • compiler/core/js_exp_make.ml: E.method_ has no directive parameter and builds the function with directive = None.

Fixing it

Passing the directive through is not enough. In Js_dump.pp_function, the method branch prints let self = this before function_body ?directive. The directive would then not be the first statement in the body, so JavaScript would treat it as a plain string expression. The method branch has to print the directive before let self = this.

printlambda does not show directives, so check that the @this rewrite in the frontend keeps the attribute on the inner function.

Found while reviewing #9176, which fixes the related #9174 (forwarding async/directive functions reduced away). This bug was there before that PR.

Lingua principale
OCaml
Stelle
7.5k
Fork
485
Merge medio
17h 9m
PR unite (30g)
264

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rescript-lang/rescript

Tutte le issue di rescript-lang/rescript

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.