match: an enum with no variants has no values, so a match needs no arm for it

Open
#3,393 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
compilers

Research direction

Reproduce the Never example from the issue and find where the E3020 "does not cover" error is emitted in the exhaustiveness check for match. Read how a variant's payload type is examined, then add an "uninhabited type" test covering an empty enum, a struct or tuple with such a field, and an enum whose variants all hold such types. Done when the example compiles and references to empty types still require an arm, as the issue specifies. The layout question tied to #2327 should be checked separately.

Written by the indexing model from the issue text.

Description

core-lang enhancement

An enum with no variants has no values. Vx accepts the declaration, but match does not use the fact:

import core::result;

enum Never {}

fn parse() -> Result<i32, Never> {
    return Result<i32, Never>::Ok(3);
}

fn main() -> i32 {
    return match parse() { Result<i32, Never>::Ok(v) => { v - 3 } };
}
Error[E3020] at never.vx:10:12: this `match` does not cover Err; add an arm for each, or a `_` arm

No program can make the Err value, so the arm is code that cannot run.

The change

In the exhaustiveness check of match, a variant is covered when its payload has a type with no values. A type has no values when it is:

  • an enum with no variants, or
  • a struct or tuple with a field of such a type, or
  • an enum whose variants each hold such a type.

Rust made the same change in 1.82 (min_exhaustive_patterns). Stable Rust writes Result<T, Infallible>, and Infallible is an empty enum.

Leave out for now:

  • A core type such as Infallible. Add it when a library API needs an error type that cannot occur.
  • A value of such a type behind a reference or a pointer. Rust still asks for the arm there, because unsafe code can make such a reference.

Questions

  • Whether a function may take a parameter of such a type, and whether the call can then be removed. Rust allows the parameter.
  • The layout: #2327 gives a zero-size type its rules. An enum with no variants is zero-size too, but no value of it exists. Check that #2327's rules (Z6: never by value across an ABI) do not need a special case.

Part of #3391.

Dominant language
Rust
Stars
35
Forks
5
Avg merge
12h 26m
Merged PRs (30d)
799

Getting set up

We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vx-lang/Vx

All issues in vx-lang/Vx

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.