Skip to content

Make lang-items private #301

Closed
Closed
@doctorn

Description

@doctorn

TL;DR

tcx.lang_items().my_lang_item().unwrap() is an anti-pattern which triggers ICEs in #![no_core]. This MCP proposes to refactor the current implementation of lang-items to prevent it.

Links and Details

In #![no_core], we cannot assume that any lang-items are present. At present, due to

the compiler should be free from these kind of ICEs. However, tcx.lang_items().my_lang_item().unwrap() is an anti-pattern that we want to avoid reintroducing in the future. Following some discussion the idea (credit to @ecstatic-morse) is to introduce an opaque option representing the presence (or lack thereof) of lang-items:

pub enum LangItemRecord {
    DefId(DefId),
    Missing(LangItem),
}

To avoid having to explicitly match on this enum everywhere where we normally do something like

if Some(def_id) == tcx.lang_items().my_lang_item() {
    // ...
}

methods would be implemented for LangItemRecord to allow for comparison with DefIds (silently returning false if the item is missing).

By including the Missing variant, I am hoping that the missing and items field of the LanguageItems struct can be unified and we can pre-allocate an array of LangItemRecords with each initially assumed Missing. This is likely a longer term proposal though and the immediate priority is to prevent unwraps.

The rustc-dev-guide is also sadly lacking with respect to descriptions of the various kinds of lang-items and how they're used so I'll be working on supporting documentation as part of this work.

Mentors or Reviewers

@oli-obk
@ecstatic-morse
(Assuming both of you are still interested 😅)

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-compilerAdd this label so rfcbot knows to poll the compiler teammajor-changeA proposal to make a major change to rustcmajor-change-acceptedA major change proposal that was accepted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions