Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Untagged variant switch with a default arm matches instances and literals in the wrong case

Abierto
#8,995 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
1-2 días
Aptitud para principiantes
55/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
javascript, ocaml
Área
compilers

Línea de trabajo

Start with compiler/ml/lam_compile.ml, especially compile_untagged_cases and compile_general_cases, then compare the behavior with rules 7 and 8 in compiler/ml/UntaggedVariants.md. Run or add compiler cases for the instance and literal examples in the issue; done means both produce other for the shown inputs while preserving the documented matching rules.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

A switch on an untagged variant with a _ arm can match a value in the wrong case. This happens when the default survives into the untagged dispatch, which takes a variant with several cases; with fewer cases the match compiler expands _ into the remaining cases and the result is correct.

An instance lands in the object case.

@unboxed type t = D(Date.t) | R(RegExp.t) | O({x: int}) | S(string) | I(int) | F(int => int)
@inline(never) let id = (x: t) => x
let f = v => switch v { | O(_) => "O" | S(_) => "S" | _ => "other" }
Console.log(f(id(D(Date.make()))))
function f(v) {
  switch (typeof v) {
    case "object" :
      return "O";
    case "string" :
      return "S";
    default:
      return "other";
  }
}

Output: O; expected other. The case "object" lacks the !(v instanceof Date) && !(v instanceof RegExp) exclusion that the object check gets elsewhere.

A literal lands in the string case.

@unboxed type t = | @as("lit") Lit | S(string) | I(int) | B(bool) | O({x: int}) | F(int => int)
@inline(never) let id = (x: t) => x
let f = v => switch v { | S(_) => "S" | O(_) => "O" | _ => "other" }
Console.log(f(id(Lit)))
function f(v) {
  switch (typeof v) {
    case "string" :
      return "S";
    case "object" :
      return "O";
    default:
      return "other";
  }
}

Output: S; expected other. No test for the literal "lit" is emitted before the typeof switch.

Both contradict compiler/ml/UntaggedVariants.md: rule 7 ("A value equal to a literal therefore belongs to the literal's case") and rule 8 ("In both forms a value of an instance case never lands in the object case"). The cause is presumably in lam_compile.ml:compile_untagged_cases / compile_general_cases when the switch keeps a Default and the instance and literal cases it would need to exclude have no arm of their own (not yet confirmed).

Lenguaje dominante
OCaml
Estrellas
7.5k
Forks
483
Merge medio
21 h 8 min
PR fusionados (30 d)
183

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rescript-lang/rescript

Todos los issues de rescript-lang/rescript

Issues similares

Más issues de Compilers

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.