react / react/react

[Compiler Bug]: Post Increment Assignment Compiling to Pre Increment Assignment

Ouverte
#35,205 2 commentaires 2 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Component: React Compiler Type: Bug
Langage dominant
JavaScript
Étoiles
251k
Forks
51.4k
Merge moyen
2 j 4 h
PR mergées (30 j)
53

Description

What kind of issue is this?
  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-hooks (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)
Link to repro

https://github.com/kbitgood/rc-bug-repro-increment-assign

Repro steps
Description

When using the post increment operator in an assignment (y = x++) the value is incremented before being assigned when using the React Compiler.

Steps to Reproduce
  1. Check out the reproduction repository
  2. Run npm run dev, or npm run build && npm run start as the problem exists in both dev mode and production
  3. See the index field of each item in the next list is off by one. Every item in the list should have a sequential "Count" value starting at 1, not
Details in the Reproduction Code

The code in src/app/page.tsx uses Array.reduce to build an array of nodes, and keeps track of a counter in the aggregated value.

When we use this code:

const count = agg.itemCounter++;

The compiled output translates to:

agg.itemCounter = agg.itemCounter + 1;
const count = agg.itemCounter;

Which is not equivalent because it increments before assigning, rather than assigning and then incrementing.

The issue goes away by taking the counter out of the aggregate object and into a normal variable outside of the reduce function.

Conclusion

I realize that this way of keeping the count in the aggregated value is not the best way, but this is not strictly against the "Rules of React". If it is, it should probably be added to the ESLint rules.

This issue was found on a large codebase that was working just fine before enabling the compiler. And we had no indication that this code would not work after enabling the compiler.

How often does this bug happen?

Every time

What version of React are you using?

19.2.0

What version of React Compiler are you using?

1.0.0

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par la reproduction liée et inspectez src/app/page.tsx, puis exécutez npm run dev ou npm run build && npm run start afin de comparer le comportement du code source avec la sortie compilée. Suivez le chemin de React Compiler qui transforme agg.itemCounter++ et vérifiez que le code généré préserve la sémantique d’affectation du post-incrément, y compris les valeurs séquentielles de Count.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, react, typescript
Domaine
compilers, frontend
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.