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 😅)
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 torequire_lang_itemoverunwrap. rust#72170lang_items\(\).*\.unwrap\(\)rust#72216the 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:To avoid having to explicitly match on this
enumeverywhere where we normally do something likemethods would be implemented for
LangItemRecordto allow for comparison withDefIds (silently returningfalseif the item is missing).By including the
Missingvariant, I am hoping that themissinganditemsfield of theLanguageItemsstruct can be unified and we can pre-allocate an array ofLangItemRecords with each initially assumedMissing. This is likely a longer term proposal though and the immediate priority is to preventunwraps.The
rustc-dev-guideis 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 😅)