Skip to content

Commit

Permalink
Rollup merge of #72170 - lcnr:lang_item, r=oli-obk
Browse files Browse the repository at this point in the history
use `require_lang_item` over `unwrap`.

Does not yet replace all uses of `lang_items\(\)\.*\.unwrap\(\)`, as there are more
than I expected 😅

Fixes #72099

r? @RalfJung

*edit: The goal of this this PR is to change ICE from missing lang items to a fatal error.*
  • Loading branch information
Dylan-DPC authored May 14, 2020
2 parents d732aef + 9001a64 commit 62f1840
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 27 deletions.
7 changes: 4 additions & 3 deletions src/librustc_middle/ty/adjustment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::ty::subst::SubstsRef;
use crate::ty::{self, Ty, TyCtxt};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::{DerefMutTraitLangItem, DerefTraitLangItem};
use rustc_macros::HashStable;

#[derive(Clone, Copy, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
Expand Down Expand Up @@ -117,11 +118,11 @@ pub struct OverloadedDeref<'tcx> {
impl<'tcx> OverloadedDeref<'tcx> {
pub fn method_call(&self, tcx: TyCtxt<'tcx>, source: Ty<'tcx>) -> (DefId, SubstsRef<'tcx>) {
let trait_def_id = match self.mutbl {
hir::Mutability::Not => tcx.lang_items().deref_trait(),
hir::Mutability::Mut => tcx.lang_items().deref_mut_trait(),
hir::Mutability::Not => tcx.require_lang_item(DerefTraitLangItem, None),
hir::Mutability::Mut => tcx.require_lang_item(DerefMutTraitLangItem, None),
};
let method_def_id = tcx
.associated_items(trait_def_id.unwrap())
.associated_items(trait_def_id)
.in_definition_order()
.find(|m| m.kind == ty::AssocKind::Fn)
.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeFoldable};
use rustc_errors::ErrorReported;
use rustc_hir::def::Namespace;
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_hir::lang_items::DropInPlaceFnLangItem;
use rustc_hir::lang_items::{DropInPlaceFnLangItem, FnOnceTraitLangItem};
use rustc_macros::HashStable;

use std::fmt;
Expand Down Expand Up @@ -375,7 +375,7 @@ impl<'tcx> Instance<'tcx> {
substs: ty::SubstsRef<'tcx>,
) -> Instance<'tcx> {
debug!("fn_once_adapter_shim({:?}, {:?})", closure_did, substs);
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
let fn_once = tcx.require_lang_item(FnOnceTraitLangItem, None);
let call_once = tcx
.associated_items(fn_once)
.in_definition_order()
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_middle/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_ast::ast::{self, IntTy, UintTy};
use rustc_attr as attr;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir as hir;
use rustc_hir::lang_items::{GeneratorStateLangItem, PinTypeLangItem};
use rustc_index::bit_set::BitSet;
use rustc_index::vec::{Idx, IndexVec};
use rustc_session::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
Expand Down Expand Up @@ -2314,13 +2315,13 @@ impl<'tcx> ty::Instance<'tcx> {
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);

let pin_did = tcx.lang_items().pin_type().unwrap();
let pin_did = tcx.require_lang_item(PinTypeLangItem, None);
let pin_adt_ref = tcx.adt_def(pin_did);
let pin_substs = tcx.intern_substs(&[env_ty.into()]);
let env_ty = tcx.mk_adt(pin_adt_ref, pin_substs);

sig.map_bound(|sig| {
let state_did = tcx.lang_items().gen_state().unwrap();
let state_did = tcx.require_lang_item(GeneratorStateLangItem, None);
let state_adt_ref = tcx.adt_def(state_did);
let state_substs = tcx.intern_substs(&[
sig.yield_ty.into(),
Expand Down
17 changes: 12 additions & 5 deletions src/librustc_mir/borrow_check/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::{CoerceUnsizedTraitLangItem, CopyTraitLangItem, SizedTraitLangItem};
use rustc_index::vec::{Idx, IndexVec};
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::env::RegionBoundPairs;
Expand Down Expand Up @@ -502,7 +503,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) = context {
let tcx = self.tcx();
let trait_ref = ty::TraitRef {
def_id: tcx.lang_items().copy_trait().unwrap(),
def_id: tcx.require_lang_item(CopyTraitLangItem, Some(self.last_span)),
substs: tcx.mk_substs_trait(place_ty.ty, &[]),
};

Expand Down Expand Up @@ -1468,7 +1469,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
self.check_rvalue(body, rv, location);
if !self.tcx().features().unsized_locals {
let trait_ref = ty::TraitRef {
def_id: tcx.lang_items().sized_trait().unwrap(),
def_id: tcx.require_lang_item(SizedTraitLangItem, Some(self.last_span)),
substs: tcx.mk_substs_trait(place_ty, &[]),
};
self.prove_trait_ref(
Expand Down Expand Up @@ -2013,7 +2014,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
ty::Predicate::Trait(
ty::Binder::bind(ty::TraitPredicate {
trait_ref: ty::TraitRef::new(
self.tcx().lang_items().copy_trait().unwrap(),
self.tcx().require_lang_item(
CopyTraitLangItem,
Some(self.last_span),
),
tcx.mk_substs_trait(ty, &[]),
),
}),
Expand All @@ -2037,7 +2041,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}

let trait_ref = ty::TraitRef {
def_id: tcx.lang_items().sized_trait().unwrap(),
def_id: tcx.require_lang_item(SizedTraitLangItem, Some(self.last_span)),
substs: tcx.mk_substs_trait(ty, &[]),
};

Expand Down Expand Up @@ -2135,7 +2139,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
CastKind::Pointer(PointerCast::Unsize) => {
let &ty = ty;
let trait_ref = ty::TraitRef {
def_id: tcx.lang_items().coerce_unsized_trait().unwrap(),
def_id: tcx.require_lang_item(
CoerceUnsizedTraitLangItem,
Some(self.last_span),
),
substs: tcx.mk_substs_trait(op.ty(body, tcx), &[ty.into()]),
};

Expand Down
6 changes: 2 additions & 4 deletions src/librustc_mir/monomorphize/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,8 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
}
mir::Rvalue::NullaryOp(mir::NullOp::Box, _) => {
let tcx = self.tcx;
let exchange_malloc_fn_def_id = tcx
.lang_items()
.require(ExchangeMallocFnLangItem)
.unwrap_or_else(|e| tcx.sess.fatal(&e));
let exchange_malloc_fn_def_id =
tcx.require_lang_item(ExchangeMallocFnLangItem, None);
let instance = Instance::mono(tcx, exchange_malloc_fn_def_id);
if should_monomorphize_locally(tcx, &instance) {
self.output.push(create_fn_mono_item(instance));
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_mir/shim.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::FnMutTraitLangItem;
use rustc_middle::mir::*;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::subst::{InternalSubsts, Subst};
Expand Down Expand Up @@ -70,7 +71,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
build_call_shim(tcx, instance, None, CallKind::Direct(def_id), None)
}
ty::InstanceDef::ClosureOnceShim { call_once: _ } => {
let fn_mut = tcx.lang_items().fn_mut_trait().unwrap();
let fn_mut = tcx.require_lang_item(FnMutTraitLangItem, None);
let call_mut = tcx
.associated_items(fn_mut)
.in_definition_order()
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use crate::util::storage;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::{GeneratorStateLangItem, PinTypeLangItem};
use rustc_index::bit_set::{BitMatrix, BitSet};
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
Expand Down Expand Up @@ -381,7 +382,7 @@ fn make_generator_state_argument_indirect<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Bo
fn make_generator_state_argument_pinned<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let ref_gen_ty = body.local_decls.raw[1].ty;

let pin_did = tcx.lang_items().pin_type().unwrap();
let pin_did = tcx.require_lang_item(PinTypeLangItem, Some(body.span));
let pin_adt_ref = tcx.adt_def(pin_did);
let substs = tcx.intern_substs(&[ref_gen_ty.into()]);
let pin_ref_gen_ty = tcx.mk_adt(pin_adt_ref, substs);
Expand Down Expand Up @@ -1207,7 +1208,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
};

// Compute GeneratorState<yield_ty, return_ty>
let state_did = tcx.lang_items().gen_state().unwrap();
let state_did = tcx.require_lang_item(GeneratorStateLangItem, None);
let state_adt_ref = tcx.adt_def(state_did);
let state_substs = tcx.intern_substs(&[yield_ty.into(), body.return_ty().into()]);
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_mir/util/elaborate_drops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::util::patch::MirPatch;
use rustc_hir as hir;
use rustc_hir::lang_items;
use rustc_hir::lang_items::{BoxFreeFnLangItem, DropTraitLangItem};
use rustc_index::vec::Idx;
use rustc_middle::mir::*;
use rustc_middle::traits::Reveal;
Expand Down Expand Up @@ -535,7 +535,7 @@ where
fn destructor_call_block(&mut self, (succ, unwind): (BasicBlock, Unwind)) -> BasicBlock {
debug!("destructor_call_block({:?}, {:?})", self, succ);
let tcx = self.tcx();
let drop_trait = tcx.lang_items().drop_trait().unwrap();
let drop_trait = tcx.require_lang_item(DropTraitLangItem, None);
let drop_fn = tcx.associated_items(drop_trait).in_definition_order().next().unwrap();
let ty = self.place_ty(self.place);
let substs = tcx.mk_substs_trait(ty, &[]);
Expand Down Expand Up @@ -877,8 +877,7 @@ where
) -> BasicBlock {
let tcx = self.tcx();
let unit_temp = Place::from(self.new_temp(tcx.mk_unit()));
let free_func =
tcx.require_lang_item(lang_items::BoxFreeFnLangItem, Some(self.source_info.span));
let free_func = tcx.require_lang_item(BoxFreeFnLangItem, Some(self.source_info.span));
let args = adt.variants[VariantIdx::new(0)]
.fields
.iter()
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_mir_build/hair/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustc_hir as hir;
use rustc_hir::lang_items::EqTraitLangItem;
use rustc_index::vec::Idx;
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_middle::mir::Field;
Expand Down Expand Up @@ -140,7 +141,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
// code at the moment, because types like `for <'a> fn(&'a ())` do
// not *yet* implement `PartialEq`. So for now we leave this here.
let ty_is_partial_eq: bool = {
let partial_eq_trait_id = self.tcx().lang_items().eq_trait().unwrap();
let partial_eq_trait_id = self.tcx().require_lang_item(EqTraitLangItem, None);
let obligation: PredicateObligation<'_> = predicate_for_trait_def(
self.tcx(),
self.param_env,
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_trait_selection/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::traits::error_reporting::InferCtxtExt;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::ErrorReported;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::{FnOnceTraitLangItem, GeneratorTraitLangItem};
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::subst::{InternalSubsts, Subst};
use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};
Expand Down Expand Up @@ -1222,7 +1223,7 @@ fn confirm_generator_candidate<'cx, 'tcx>(

let tcx = selcx.tcx();

let gen_def_id = tcx.lang_items().gen_trait().unwrap();
let gen_def_id = tcx.require_lang_item(GeneratorTraitLangItem, None);

let predicate = super::util::generator_trait_ref_and_outputs(
tcx,
Expand Down Expand Up @@ -1309,7 +1310,7 @@ fn confirm_callable_candidate<'cx, 'tcx>(
debug!("confirm_callable_candidate({:?},{:?})", obligation, fn_sig);

// the `Output` associated type is declared on `FnOnce`
let fn_once_def_id = tcx.lang_items().fn_once_trait().unwrap();
let fn_once_def_id = tcx.require_lang_item(FnOnceTraitLangItem, None);

let predicate = super::util::closure_trait_ref_and_return_type(
tcx,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_trait_selection::traits::{self, ObligationCause};
use rustc_ast::util::parser::PREC_POSTFIX;
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::lang_items::DerefTraitLangItem;
use rustc_hir::{is_range_literal, Node};
use rustc_middle::ty::adjustment::AllowTwoPhase;
use rustc_middle::ty::{self, AssocItem, Ty, TypeAndMut};
Expand Down Expand Up @@ -634,7 +635,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ if sp == expr.span && !is_macro => {
// Check for `Deref` implementations by constructing a predicate to
// prove: `<T as Deref>::Output == U`
let deref_trait = self.tcx.lang_items().deref_trait().unwrap();
let deref_trait = self.tcx.require_lang_item(DerefTraitLangItem, Some(expr.span));
let item_def_id = self
.tcx
.associated_items(deref_trait)
Expand Down

0 comments on commit 62f1840

Please sign in to comment.