@directive is dropped on @this functions

Aberta
#9,197 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
55/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
javascript, ocaml
Domínio
compilers

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.

Linguagem predominante
OCaml
Estrelas
7.5k
Forks
485
Merge médio
17h 9min
PRs com merge (30d)
264

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de rescript-lang/rescript

Todas as issues de rescript-lang/rescript

Issues semelhantes

Mais issues de Compilers

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.