rust-lang / rust-lang/rfcs

Never types should implement everything

Open
#2,619 17 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I haven't thought this through fully, but I feel like it should be possible (by definition) for the compiler to act as if ! implemented every trait, or rather, to ignore errors about it not implementing a particular trait.

e.g. given this definition of an enum:

enum Style<T>
where 
    T: SomeTrait,
{
    Variant1,
    Variant2(T),
}

I propose that it should be valid to use Style<!> directly, so long as one is not instantiating an instance of type Style::Variant2, which would in turn make this legal:

enum Style<T=!>
where 
    T: SomeTrait,
{
    Variant1,
    Variant2(T),
}

Since ! cannot exist, all of Style::Variant2 cannot exist, in which case it doesn't matter if the generic constraint !: SomeTrait is not met; thus allowing the use of Style::Variant1 directly without needing to specify a type for T which will never be used.

With regards to the calculation of the size of the enum: it is provable that Style<!>::Variant2 cannot exist, therefore it should be precluded from the calculation of the size of the type, and treated as a phantom variant with no bearing on the object size. In fact, in the case of such a binary enum with only two types, one of which is purely phantom, the entire enum should be optimized away and it should be treated as a zero-size type altogether (or else the size of Variant1 should Variant1 have any associated values).

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

Start by reading the issue proposal and its discussion, then formalize the desired behavior for !, trait bounds, enum layout, and default type parameters. Done would require a precise, agreed design before implementation work can be scoped.

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
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.