Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Untagged variants: no warning for patterns shadowed by a literal case, and a spurious non-exhaustive warning for bool payloads

未关闭
#8,806 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
ocaml
领域
compilers

调研方向

Start by reading parmatch.ml, which the issue identifies as the site of the exhaustiveness check, and find the adjacent unused-case check for the warning proposal. Review the related issues #8631, #8632, and #8777 for runtime value comparisons and overlap cases. Done means dead untagged payload patterns are warned about and the bool example is exhaustive without an unreachable failure.

由索引模型根据 Issue 内容生成。

描述

Summary

An untagged variant may have a literal case whose runtime value a payload case can also hold (#8632, #8777):

@unboxed type t = | @as("a") A | S(string)

Matching tests declared literals first, so the pattern S("a") can never match. The value "a" always goes to A, or to a wildcard if there is no A arm. The type checker treats S("a") and A as different, which causes two problems.

1. No warning for patterns that can never match

Checked on master. None of these produce a warning, and each S(...) arm marked dead is dead in the generated JS:

let f1 = x => switch x { | A => 1 | S("a") => 2 | S(_) => 3 }  // S("a") dead
let f2 = x => switch x { | S("a") => 2 | A => 1 | S(_) => 3 }  // dead even as first arm
let f3 = x => switch x { | S("a") => 2 | _ => 3 }              // compiles to `return 3`
let f4 = x => switch x { | S("a" | "b") => 2 | _ => 3 }        // the "a" alternative is dead

The same applies when:

  • the literal comes from the constructor name (Primary | Color(string): Color("Primary") is dead);
  • an int literal overlaps a float payload (@as(1) One | F(float): F(1.0) is dead);
  • a nested untagged constant has the same value (@as("x") X | W(inner) where inner has @as("x") P: W(P) is dead).

In a refutable binding such as let l = (S("a") as v) => v, the pattern never matches at all, so every call with S("a") throws Match_failure.

Proposal: warn on a constructor pattern S(p) of an untagged variant when every value p can match is the runtime value of a literal case in the same variant. Use warning 11 (unused case) for a whole arm and warning 12 (redundant sub-pattern) for or-pattern alternatives. Suggested message: "this pattern can never match: "a" is the value of constructor A".

Notes:

  • The check is local to each pattern and doesn't depend on arm order, so it can run beside check_unused without touching the exhaustiveness algorithm.
  • Values must be compared the way they are at runtime, as in #8631: 1 and 1.0 are equal, bigint spellings differ (1_0n = 10n), ints use their 32-bit value, and a constructor without @as is represented by its own name.
  • Guards (S(s) if s == "a") are out of scope.

2. Spurious non-exhaustive warning for bool payloads

With #8777 a bool payload can partly overlap a literal:

@unboxed type b = | @as(true) Yes | Ans(bool)
let g = x => switch x { | Yes => 1 | Ans(false) => 2 }

This match covers every value, yet the compiler reports warning 8 with the counterexample Ans(true) and emits an unreachable Match_failure throw. The only way to silence it is to add the dead arm Ans(true) => ..., which point 1 would then flag. The two parts therefore need to land together, or the bool case ends up with no warning-free way to write the match.

Proposal: during the exhaustiveness check only, treat a literal-case pattern as also covering the payload values equal to it, e.g. check Yes as Yes | Ans(true). This is harder than point 1 because it changes parmatch.ml. For string and number payloads it rarely matters, since their domain is infinite, but for bool it does.

Related: #8632, #8777, #8631, rescript-lang/rescript-lang.org#1337

主要语言
OCaml
星标
7.5k
派生
484
平均合并
18 小时 19 分钟
30 天内合并 PR
116

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rescript-lang/rescript 的其他 Issue

查看 rescript-lang/rescript 的全部 Issue

相似的 Issue

更多 Compilers Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。