Rollup merge of #111648 - Nilstrieb:language-items, r=WaffleLapkin

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:
Dylan DPC 2023-05-17 11:13:56 +05:30 committed by GitHub
commit e7176dbfd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 20 additions and 38 deletions

View file

@ -966,11 +966,7 @@ fn codegen_panic_inner<'tcx>(
args: &[Value],
span: Span,
) {
let def_id = fx
.tcx
.lang_items()
.require(lang_item)
.unwrap_or_else(|e| fx.tcx.sess.span_fatal(span, e.to_string()));
let def_id = fx.tcx.require_lang_item(lang_item, Some(span));
let instance = Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx);
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.
use crate::def_id::DefId;
use crate::errors::LangItemError;
use crate::{MethodKind, Target};
use rustc_ast as ast;
@ -42,13 +41,6 @@ pub fn set(&mut self, item: LangItem, def_id: DefId) {
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)> + '_ {
self.items
.iter()

View file

@ -30,7 +30,6 @@
pub mod def_path_hash_map;
pub mod definitions;
pub mod diagnostic_items;
pub mod errors;
pub use rustc_span::def_id;
mod hir;
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 unsize_trait = tcx.lang_items().require(LangItem::Unsize).unwrap_or_else(|err| {
tcx.sess.fatal(format!("`CoerceUnsized` implementation {}", err.to_string()));
});
let unsize_trait = tcx.require_lang_item(LangItem::Unsize, Some(span));
let source = tcx.type_of(impl_did).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
.label = due to this attribute
middle_requires_lang_item = requires `{$name}` lang_item
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

View file

@ -1,5 +1,5 @@
use rustc_macros::Diagnostic;
use rustc_span::Span;
use rustc_span::{Span, Symbol};
use crate::ty::Ty;
@ -73,6 +73,14 @@ pub(crate) struct StrictCoherenceNeedsNegativeCoherence {
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)]
#[diag(middle_const_not_used_in_type_alias)]
pub(super) struct ConstNotUsedTraitAlias {

View file

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

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 };
match expected_item {
Item::Lang(expected_lang_item) => {
let expected_id = cx.tcx.lang_items().require(expected_lang_item).unwrap();
cx.tcx.parent(id) == expected_id
},
Item::Lang(expected_lang_item) => cx
.tcx
.lang_items()
.get(expected_lang_item)
.map_or(false, |expected_id| cx.tcx.parent(id) == expected_id),
Item::Diag(expected_ty, expected_variant) => {
let ty = cx.typeck_results().pat_ty(pat);