Remove LangItems::require

It's just a short wrapper used by `tcx.require_lang_item`. Deleting it
gives us a negative diff.
This commit is contained in:
Nilstrieb 2023-05-16 19:23:38 +02:00
parent cba14074bb
commit c3efa51947
9 changed files with 20 additions and 38 deletions

View File

@ -966,11 +966,7 @@ fn codegen_panic_inner<'tcx>(
args: &[Value], args: &[Value],
span: Span, span: Span,
) { ) {
let def_id = fx let def_id = fx.tcx.require_lang_item(lang_item, Some(span));
.tcx
.lang_items()
.require(lang_item)
.unwrap_or_else(|e| fx.tcx.sess.span_fatal(span, e.to_string()));
let instance = Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx); let instance = Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx);
let symbol_name = fx.tcx.symbol_name(instance).name; let symbol_name = fx.tcx.symbol_name(instance).name;

View File

@ -1,10 +0,0 @@
use crate::LangItem;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
pub struct LangItemError(pub LangItem);
impl ToString for LangItemError {
fn to_string(&self) -> String {
format!("requires `{}` lang_item", self.0.name())
}
}

View File

@ -8,7 +8,6 @@
//! * Functions called by the compiler itself. //! * Functions called by the compiler itself.
use crate::def_id::DefId; use crate::def_id::DefId;
use crate::errors::LangItemError;
use crate::{MethodKind, Target}; use crate::{MethodKind, Target};
use rustc_ast as ast; use rustc_ast as ast;
@ -42,13 +41,6 @@ impl LanguageItems {
self.items[item as usize] = Some(def_id); self.items[item as usize] = Some(def_id);
} }
/// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
/// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
/// returns an error encapsulating the `LangItem`.
pub fn require(&self, it: LangItem) -> Result<DefId, LangItemError> {
self.get(it).ok_or_else(|| LangItemError(it))
}
pub fn iter(&self) -> impl Iterator<Item = (LangItem, DefId)> + '_ { pub fn iter(&self) -> impl Iterator<Item = (LangItem, DefId)> + '_ {
self.items self.items
.iter() .iter()

View File

@ -30,7 +30,6 @@ pub mod def;
pub mod def_path_hash_map; pub mod def_path_hash_map;
pub mod definitions; pub mod definitions;
pub mod diagnostic_items; pub mod diagnostic_items;
pub mod errors;
pub use rustc_span::def_id; pub use rustc_span::def_id;
mod hir; mod hir;
pub mod hir_id; pub mod hir_id;

View File

@ -298,9 +298,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) -> Coe
let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span)); let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span));
let unsize_trait = tcx.lang_items().require(LangItem::Unsize).unwrap_or_else(|err| { let unsize_trait = tcx.require_lang_item(LangItem::Unsize, Some(span));
tcx.sess.fatal(format!("`CoerceUnsized` implementation {}", err.to_string()));
});
let source = tcx.type_of(impl_did).subst_identity(); let source = tcx.type_of(impl_did).subst_identity();
let trait_ref = tcx.impl_trait_ref(impl_did).unwrap().subst_identity(); let trait_ref = tcx.impl_trait_ref(impl_did).unwrap().subst_identity();

View File

@ -39,5 +39,7 @@ middle_strict_coherence_needs_negative_coherence =
to use `strict_coherence` on this trait, the `with_negative_coherence` feature must be enabled to use `strict_coherence` on this trait, the `with_negative_coherence` feature must be enabled
.label = due to this attribute .label = due to this attribute
middle_requires_lang_item = requires `{$name}` lang_item
middle_const_not_used_in_type_alias = middle_const_not_used_in_type_alias =
const parameter `{$ct}` is part of concrete type but not used in parameter list for the `impl Trait` type alias const parameter `{$ct}` is part of concrete type but not used in parameter list for the `impl Trait` type alias

View File

@ -1,5 +1,5 @@
use rustc_macros::Diagnostic; use rustc_macros::Diagnostic;
use rustc_span::Span; use rustc_span::{Span, Symbol};
use crate::ty::Ty; use crate::ty::Ty;
@ -73,6 +73,14 @@ pub(crate) struct StrictCoherenceNeedsNegativeCoherence {
pub attr_span: Option<Span>, pub attr_span: Option<Span>,
} }
#[derive(Diagnostic)]
#[diag(middle_requires_lang_item)]
pub(crate) struct RequiresLangItem {
#[primary_span]
pub span: Option<Span>,
pub name: Symbol,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(middle_const_not_used_in_type_alias)] #[diag(middle_const_not_used_in_type_alias)]
pub(super) struct ConstNotUsedTraitAlias { pub(super) struct ConstNotUsedTraitAlias {

View File

@ -18,12 +18,8 @@ impl<'tcx> TyCtxt<'tcx> {
/// Returns the `DefId` for a given `LangItem`. /// Returns the `DefId` for a given `LangItem`.
/// If not found, fatally aborts compilation. /// If not found, fatally aborts compilation.
pub fn require_lang_item(self, lang_item: LangItem, span: Option<Span>) -> DefId { pub fn require_lang_item(self, lang_item: LangItem, span: Option<Span>) -> DefId {
self.lang_items().require(lang_item).unwrap_or_else(|err| { self.lang_items().get(lang_item).unwrap_or_else(|| {
if let Some(span) = span { self.sess.emit_fatal(crate::error::RequiresLangItem { span, name: lang_item.name() });
self.sess.span_fatal(span, err.to_string())
} else {
self.sess.fatal(err.to_string())
}
}) })
} }

View File

@ -289,10 +289,11 @@ fn is_pat_variant(cx: &LateContext<'_>, pat: &Pat<'_>, path: &QPath<'_>, expecte
let Some(id) = cx.typeck_results().qpath_res(path, pat.hir_id).opt_def_id() else { return false }; let Some(id) = cx.typeck_results().qpath_res(path, pat.hir_id).opt_def_id() else { return false };
match expected_item { match expected_item {
Item::Lang(expected_lang_item) => { Item::Lang(expected_lang_item) => cx
let expected_id = cx.tcx.lang_items().require(expected_lang_item).unwrap(); .tcx
cx.tcx.parent(id) == expected_id .lang_items()
}, .get(expected_lang_item)
.map_or(false, |expected_id| cx.tcx.parent(id) == expected_id),
Item::Diag(expected_ty, expected_variant) => { Item::Diag(expected_ty, expected_variant) => {
let ty = cx.typeck_results().pat_ty(pat); let ty = cx.typeck_results().pat_ty(pat);