Description
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 DefId
s (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 LangItemRecord
s with each initially assumed Missing
. This is likely a longer term proposal though and the immediate priority is to prevent unwrap
s.
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 😅)