Skip to content

Commit

Permalink
Remove LangItems::require
Browse files Browse the repository at this point in the history
It's just a short wrapper used by `tcx.require_lang_item`. Deleting it
gives us a negative diff.
  • Loading branch information
Noratrieb committed May 16, 2023
1 parent cba1407 commit c3efa51
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 38 deletions.
6 changes: 1 addition & 5 deletions compiler/rustc_codegen_cranelift/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 0 additions & 10 deletions compiler/rustc_hir/src/errors.rs

This file was deleted.

8 changes: 0 additions & 8 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,13 +41,6 @@ impl LanguageItems {
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()
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub mod def;
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;
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_hir_analysis/src/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion compiler/rustc_middle/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_macros::Diagnostic;
use rustc_span::Span;
use rustc_span::{Span, Symbol};

use crate::ty::Ty;

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_middle/src/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() });
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit c3efa51

Please sign in to comment.