rust-lang / rust-lang/rfcs

unassigned values for enums should be smarter about finding new options

Open
#598 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Someone at the rust meetup pointed out to me that code like this does not compile today:

#[repr(u16)]
enum Foo {
    A,
    B = 14,
    C,
    D = 15,
}

The issue is that we attempt to assign the value 15 to C based on it falling immediately after B.

The workaround I suggested was to move the C to the end of the series (or elsewhere), so that it would be assigned a value other than 15.

It seems we could backwards-compatibly support this, by being smarter in how we search for values to assign to otherwise unassigned variants.

(However, it is noteworthy that the semantics of the above enum that I am suggesting does not match that of C semantics, at least not according to gcc, which happily assigns 15 to both C and D, as the C language has no problem with that.)

Contributor guide

No contributing guide indexed for this repository

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.

Research direction

The issue names no source file, test, or entry point. Start by locating Rust's enum discriminant-assignment rules and the relevant implementation; done means the backwards-compatible value-search semantics are agreed and the example behavior is covered by focused tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.