Use tcx.require_lang_item instead of unwrapping

This commit is contained in:
Maybe Waffle 2022-11-22 17:19:19 +00:00
parent 0f7d81754d
commit b80356a5ab
9 changed files with 14 additions and 12 deletions

View File

@ -7,7 +7,7 @@
};
use rustc_hir as hir;
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, LangItem};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::ObligationCause;
use rustc_middle::mir::tcx::PlaceTy;
@ -601,7 +601,7 @@ fn suggest_adding_copy_bounds(&self, err: &mut Diagnostic, ty: Ty<'tcx>, span: S
else { return; };
// Try to find predicates on *generic params* that would allow copying `ty`
let infcx = tcx.infer_ctxt().build();
let copy_did = infcx.tcx.lang_items().copy_trait().unwrap();
let copy_did = infcx.tcx.require_lang_item(LangItem::Copy, Some(span));
let cause = ObligationCause::new(
span,
self.mir_hir_id(),

View File

@ -1,7 +1,7 @@
//! Concrete error types for all operations which may be invalid in a certain const context.
use hir::def_id::LocalDefId;
use hir::ConstContext;
use hir::{ConstContext, LangItem};
use rustc_errors::{
error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed,
};
@ -304,7 +304,7 @@ macro_rules! error {
err.span_note(deref_target, "deref defined here");
}
diag_trait(&mut err, self_ty, tcx.lang_items().deref_trait().unwrap());
diag_trait(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, Some(span)));
err
}
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentV1Methods) => {

View File

@ -3,7 +3,7 @@
//! context.
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items;
use rustc_hir::{lang_items, LangItem};
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, AssocItemContainer, DefIdTree, Instance, ParamEnv, Ty, TyCtxt};
use rustc_span::symbol::Ident;
@ -26,7 +26,7 @@ pub fn trait_def_id(self, tcx: TyCtxt<'_>) -> DefId {
match self {
Self::ForLoopIntoIter => tcx.get_diagnostic_item(sym::IntoIterator).unwrap(),
Self::QuestionBranch | Self::TryBlockFromOutput => {
tcx.lang_items().try_trait().unwrap()
tcx.require_lang_item(LangItem::Try, None)
}
Self::QuestionFromResidual => tcx.get_diagnostic_item(sym::FromResidual).unwrap(),
}

View File

@ -114,7 +114,7 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
traits::ObligationCause::dummy_with_span(field_ty_span),
param_env,
ty,
tcx.lang_items().copy_trait().unwrap(),
tcx.require_lang_item(LangItem::Copy, Some(span)),
) {
let error_predicate = error.obligation.predicate;
// Only note if it's not the root obligation, otherwise it's trivial and

View File

@ -1118,7 +1118,7 @@ fn check_expr_assign(
let lhs_deref_ty_is_sized = self
.infcx
.type_implements_trait(
self.tcx.lang_items().sized_trait().unwrap(),
self.tcx.require_lang_item(LangItem::Sized, None),
[lhs_deref_ty],
self.param_env,
)

View File

@ -2293,7 +2293,7 @@ pub fn trait_may_define_assoc_type(self, trait_def_id: DefId, assoc_name: Ident)
/// Given a `ty`, return whether it's an `impl Future<...>`.
pub fn ty_is_opaque_future(self, ty: Ty<'_>) -> bool {
let ty::Opaque(def_id, _) = ty.kind() else { return false };
let future_trait = self.lang_items().future_trait().unwrap();
let future_trait = self.require_lang_item(LangItem::Future, None);
self.explicit_item_bounds(def_id).iter().any(|(predicate, _)| {
let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() else {

View File

@ -11,6 +11,7 @@
use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
use rustc_hir::LangItem;
use rustc_session::config::TrimmedDefPaths;
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
use rustc_session::Limit;
@ -889,7 +890,7 @@ fn pretty_print_opaque_impl_type(
// Group the return ty with its def id, if we had one.
entry
.return_ty
.map(|ty| (tcx.lang_items().fn_once_output().unwrap(), ty)),
.map(|ty| (tcx.require_lang_item(LangItem::FnOnce, None), ty)),
);
}
if let Some(trait_ref) = entry.fn_mut_trait_ref {

View File

@ -17,6 +17,7 @@
use rustc_data_structures::intern::Interned;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::LangItem;
use rustc_index::vec::Idx;
use rustc_macros::HashStable;
use rustc_span::symbol::{kw, sym, Symbol};
@ -2108,7 +2109,7 @@ pub fn ptr_metadata_ty(
ty::Str | ty::Slice(_) => (tcx.types.usize, false),
ty::Dynamic(..) => {
let dyn_metadata = tcx.lang_items().dyn_metadata().unwrap();
let dyn_metadata = tcx.require_lang_item(LangItem::DynMetadata, None);
(tcx.bound_type_of(dyn_metadata).subst(tcx, &[tail.into()]), false)
},

View File

@ -2625,7 +2625,7 @@ fn note_obligation_cause_code<T>(
}
};
let from_generator = tcx.lang_items().from_generator_fn().unwrap();
let from_generator = tcx.require_lang_item(LangItem::FromGenerator, None);
// Don't print the tuple of capture types
'print: {