1
Fork 0

Remove lang_items\(\).*\.unwrap\(\)

This commit is contained in:
Nathan Corbyn 2020-05-14 22:06:44 +01:00
parent a74d1862d4
commit 00268be9da
7 changed files with 34 additions and 27 deletions

View file

@ -2,6 +2,8 @@ use rustc_middle::traits;
use rustc_middle::ty::adjustment::CustomCoerceUnsized; use rustc_middle::ty::adjustment::CustomCoerceUnsized;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_hir::lang_items::CoerceUnsizedTraitLangItem;
pub mod collector; pub mod collector;
pub mod partitioning; pub mod partitioning;
@ -10,7 +12,7 @@ pub fn custom_coerce_unsize_info<'tcx>(
source_ty: Ty<'tcx>, source_ty: Ty<'tcx>,
target_ty: Ty<'tcx>, target_ty: Ty<'tcx>,
) -> CustomCoerceUnsized { ) -> CustomCoerceUnsized {
let def_id = tcx.lang_items().coerce_unsized_trait().unwrap(); let def_id = tcx.require_lang_item(CoerceUnsizedTraitLangItem, None);
let trait_ref = ty::Binder::bind(ty::TraitRef { let trait_ref = ty::Binder::bind(ty::TraitRef {
def_id, def_id,

View file

@ -141,7 +141,8 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
// code at the moment, because types like `for <'a> fn(&'a ())` do // code at the moment, because types like `for <'a> fn(&'a ())` do
// not *yet* implement `PartialEq`. So for now we leave this here. // not *yet* implement `PartialEq`. So for now we leave this here.
let ty_is_partial_eq: bool = { let ty_is_partial_eq: bool = {
let partial_eq_trait_id = self.tcx().require_lang_item(EqTraitLangItem, None); let partial_eq_trait_id =
self.tcx().require_lang_item(EqTraitLangItem, Some(self.span));
let obligation: PredicateObligation<'_> = predicate_for_trait_def( let obligation: PredicateObligation<'_> = predicate_for_trait_def(
self.tcx(), self.tcx(),
self.param_env, self.param_env,

View file

@ -4,6 +4,7 @@ use crate::traits::{self, ConstPatternStructural, TraitEngine};
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::lang_items::{StructuralPeqTraitLangItem, StructuralTeqTraitLangItem};
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt, TypeFoldable, TypeVisitor}; use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt, TypeFoldable, TypeVisitor};
use rustc_span::Span; use rustc_span::Span;
@ -69,7 +70,7 @@ pub fn type_marked_structural(
let mut fulfillment_cx = traits::FulfillmentContext::new(); let mut fulfillment_cx = traits::FulfillmentContext::new();
let cause = ObligationCause::new(span, id, ConstPatternStructural); let cause = ObligationCause::new(span, id, ConstPatternStructural);
// require `#[derive(PartialEq)]` // require `#[derive(PartialEq)]`
let structural_peq_def_id = infcx.tcx.lang_items().structural_peq_trait().unwrap(); let structural_peq_def_id = infcx.tcx.require_lang_item(StructuralPeqTraitLangItem, Some(span));
fulfillment_cx.register_bound( fulfillment_cx.register_bound(
infcx, infcx,
ty::ParamEnv::empty(), ty::ParamEnv::empty(),
@ -80,7 +81,7 @@ pub fn type_marked_structural(
// for now, require `#[derive(Eq)]`. (Doing so is a hack to work around // for now, require `#[derive(Eq)]`. (Doing so is a hack to work around
// the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.) // the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.)
let cause = ObligationCause::new(span, id, ConstPatternStructural); let cause = ObligationCause::new(span, id, ConstPatternStructural);
let structural_teq_def_id = infcx.tcx.lang_items().structural_teq_trait().unwrap(); let structural_teq_def_id = infcx.tcx.require_lang_item(StructuralTeqTraitLangItem, Some(span));
fulfillment_cx.register_bound( fulfillment_cx.register_bound(
infcx, infcx,
ty::ParamEnv::empty(), ty::ParamEnv::empty(),

View file

@ -6,7 +6,7 @@ use crate::astconv::AstConv;
use crate::middle::region; use crate::middle::region;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::lang_items; use rustc_hir::lang_items::{FutureTraitLangItem, GeneratorTraitLangItem};
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::LateBoundRegionConversionTime; use rustc_infer::infer::LateBoundRegionConversionTime;
use rustc_infer::infer::{InferOk, InferResult}; use rustc_infer::infer::{InferOk, InferResult};
@ -245,7 +245,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let trait_ref = projection.to_poly_trait_ref(tcx); let trait_ref = projection.to_poly_trait_ref(tcx);
let is_fn = tcx.fn_trait_kind_from_lang_item(trait_ref.def_id()).is_some(); let is_fn = tcx.fn_trait_kind_from_lang_item(trait_ref.def_id()).is_some();
let gen_trait = tcx.require_lang_item(lang_items::GeneratorTraitLangItem, cause_span); let gen_trait = tcx.require_lang_item(GeneratorTraitLangItem, cause_span);
let is_gen = gen_trait == trait_ref.def_id(); let is_gen = gen_trait == trait_ref.def_id();
if !is_fn && !is_gen { if !is_fn && !is_gen {
debug!("deduce_sig_from_projection: not fn or generator"); debug!("deduce_sig_from_projection: not fn or generator");
@ -678,7 +678,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Check that this is a projection from the `Future` trait. // Check that this is a projection from the `Future` trait.
let trait_ref = predicate.projection_ty.trait_ref(self.tcx); let trait_ref = predicate.projection_ty.trait_ref(self.tcx);
let future_trait = self.tcx.lang_items().future_trait().unwrap(); let future_trait = self.tcx.require_lang_item(FutureTraitLangItem, Some(cause_span));
if trait_ref.def_id != future_trait { if trait_ref.def_id != future_trait {
debug!("deduce_future_output_from_projection: not a future"); debug!("deduce_future_output_from_projection: not a future");
return None; return None;

View file

@ -7,7 +7,7 @@ use rustc_trait_selection::traits::{self, ObligationCause};
use rustc_ast::util::parser::PREC_POSTFIX; use rustc_ast::util::parser::PREC_POSTFIX;
use rustc_errors::{Applicability, DiagnosticBuilder}; use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::lang_items::DerefTraitLangItem; use rustc_hir::lang_items::{CloneTraitLangItem, DerefTraitLangItem};
use rustc_hir::{is_range_literal, Node}; use rustc_hir::{is_range_literal, Node};
use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::adjustment::AllowTwoPhase;
use rustc_middle::ty::{self, AssocItem, Ty, TypeAndMut}; use rustc_middle::ty::{self, AssocItem, Ty, TypeAndMut};
@ -456,8 +456,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}; };
if self.can_coerce(ref_ty, expected) { if self.can_coerce(ref_ty, expected) {
let mut sugg_sp = sp; let mut sugg_sp = sp;
if let hir::ExprKind::MethodCall(segment, _sp, args) = &expr.kind { if let hir::ExprKind::MethodCall(ref segment, sp, ref args) = expr.kind {
let clone_trait = self.tcx.lang_items().clone_trait().unwrap(); let clone_trait = self.tcx.require_lang_item(CloneTraitLangItem, Some(sp));
if let ([arg], Some(true), sym::clone) = ( if let ([arg], Some(true), sym::clone) = (
&args[..], &args[..],
self.tables.borrow().type_dependent_def_id(expr.hir_id).map(|did| { self.tables.borrow().type_dependent_def_id(expr.hir_id).map(|did| {
@ -635,7 +635,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ if sp == expr.span && !is_macro => { _ if sp == expr.span && !is_macro => {
// Check for `Deref` implementations by constructing a predicate to // Check for `Deref` implementations by constructing a predicate to
// prove: `<T as Deref>::Output == U` // prove: `<T as Deref>::Output == U`
let deref_trait = self.tcx.require_lang_item(DerefTraitLangItem, Some(expr.span)); let deref_trait = self.tcx.require_lang_item(DerefTraitLangItem, Some(sp));
let item_def_id = self let item_def_id = self
.tcx .tcx
.associated_items(deref_trait) .associated_items(deref_trait)

View file

@ -100,7 +100,9 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::lang_items; use rustc_hir::lang_items::{
FutureTraitLangItem, PinTypeLangItem, SizedTraitLangItem, VaListTypeLangItem,
};
use rustc_hir::{ExprKind, GenericArg, HirIdMap, Item, ItemKind, Node, PatKind, QPath}; use rustc_hir::{ExprKind, GenericArg, HirIdMap, Item, ItemKind, Node, PatKind, QPath};
use rustc_index::bit_set::BitSet; use rustc_index::bit_set::BitSet;
use rustc_index::vec::Idx; use rustc_index::vec::Idx;
@ -1342,10 +1344,8 @@ fn check_fn<'a, 'tcx>(
// C-variadic fns also have a `VaList` input that's not listed in `fn_sig` // C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
// (as it's created inside the body itself, not passed in from outside). // (as it's created inside the body itself, not passed in from outside).
let maybe_va_list = if fn_sig.c_variadic { let maybe_va_list = if fn_sig.c_variadic {
let va_list_did = tcx.require_lang_item( let va_list_did =
lang_items::VaListTypeLangItem, tcx.require_lang_item(VaListTypeLangItem, Some(body.params.last().unwrap().span));
Some(body.params.last().unwrap().span),
);
let region = tcx.mk_region(ty::ReScope(region::Scope { let region = tcx.mk_region(ty::ReScope(region::Scope {
id: body.value.hir_id.local_id, id: body.value.hir_id.local_id,
data: region::ScopeData::CallSite, data: region::ScopeData::CallSite,
@ -3303,7 +3303,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
code: traits::ObligationCauseCode<'tcx>, code: traits::ObligationCauseCode<'tcx>,
) { ) {
if !ty.references_error() { if !ty.references_error() {
let lang_item = self.tcx.require_lang_item(lang_items::SizedTraitLangItem, None); let lang_item = self.tcx.require_lang_item(SizedTraitLangItem, None);
self.require_type_meets(ty, span, code, lang_item); self.require_type_meets(ty, span, code, lang_item);
} }
} }
@ -5142,7 +5142,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => {} _ => {}
} }
let boxed_found = self.tcx.mk_box(found); let boxed_found = self.tcx.mk_box(found);
let new_found = self.tcx.mk_lang_item(boxed_found, lang_items::PinTypeLangItem).unwrap(); let new_found = self.tcx.mk_lang_item(boxed_found, PinTypeLangItem).unwrap();
if let (true, Ok(snippet)) = ( if let (true, Ok(snippet)) = (
self.can_coerce(new_found, expected), self.can_coerce(new_found, expected),
self.sess().source_map().span_to_snippet(expr.span), self.sess().source_map().span_to_snippet(expr.span),
@ -5298,7 +5298,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let sp = expr.span; let sp = expr.span;
// Check for `Future` implementations by constructing a predicate to // Check for `Future` implementations by constructing a predicate to
// prove: `<T as Future>::Output == U` // prove: `<T as Future>::Output == U`
let future_trait = self.tcx.lang_items().future_trait().unwrap(); let future_trait = self.tcx.require_lang_item(FutureTraitLangItem, Some(sp));
let item_def_id = self let item_def_id = self
.tcx .tcx
.associated_items(future_trait) .associated_items(future_trait)

View file

@ -4,7 +4,9 @@
use rustc_errors::struct_span_err; use rustc_errors::struct_span_err;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::UnsizeTraitLangItem; use rustc_hir::lang_items::{
CoerceUnsizedTraitLangItem, DispatchFromDynTraitLangItem, UnsizeTraitLangItem,
};
use rustc_hir::ItemKind; use rustc_hir::ItemKind;
use rustc_infer::infer; use rustc_infer::infer;
use rustc_infer::infer::outlives::env::OutlivesEnvironment; use rustc_infer::infer::outlives::env::OutlivesEnvironment;
@ -145,11 +147,11 @@ fn visit_implementation_of_coerce_unsized(tcx: TyCtxt<'tcx>, impl_did: LocalDefI
fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDefId) { fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did); debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did);
let dispatch_from_dyn_trait = tcx.lang_items().dispatch_from_dyn_trait().unwrap();
let impl_hir_id = tcx.hir().as_local_hir_id(impl_did); let impl_hir_id = tcx.hir().as_local_hir_id(impl_did);
let span = tcx.hir().span(impl_hir_id); let span = tcx.hir().span(impl_hir_id);
let dispatch_from_dyn_trait = tcx.require_lang_item(DispatchFromDynTraitLangItem, Some(span));
let source = tcx.type_of(impl_did); let source = tcx.type_of(impl_did);
assert!(!source.has_escaping_bound_vars()); assert!(!source.has_escaping_bound_vars());
let target = { let target = {
@ -314,22 +316,23 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedInfo { pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedInfo {
debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did); debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did);
let coerce_unsized_trait = tcx.lang_items().coerce_unsized_trait().unwrap();
// this provider should only get invoked for local def-ids
let impl_hir_id = tcx.hir().as_local_hir_id(impl_did.expect_local());
let span = tcx.hir().span(impl_hir_id);
let coerce_unsized_trait = tcx.require_lang_item(CoerceUnsizedTraitLangItem, Some(span));
let unsize_trait = tcx.lang_items().require(UnsizeTraitLangItem).unwrap_or_else(|err| { let unsize_trait = tcx.lang_items().require(UnsizeTraitLangItem).unwrap_or_else(|err| {
tcx.sess.fatal(&format!("`CoerceUnsized` implementation {}", err)); tcx.sess.fatal(&format!("`CoerceUnsized` implementation {}", err));
}); });
// this provider should only get invoked for local def-ids
let impl_hir_id = tcx.hir().as_local_hir_id(impl_did.expect_local());
let source = tcx.type_of(impl_did); let source = tcx.type_of(impl_did);
let trait_ref = tcx.impl_trait_ref(impl_did).unwrap(); let trait_ref = tcx.impl_trait_ref(impl_did).unwrap();
assert_eq!(trait_ref.def_id, coerce_unsized_trait); assert_eq!(trait_ref.def_id, coerce_unsized_trait);
let target = trait_ref.substs.type_at(1); let target = trait_ref.substs.type_at(1);
debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (bound)", source, target); debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (bound)", source, target);
let span = tcx.hir().span(impl_hir_id);
let param_env = tcx.param_env(impl_did); let param_env = tcx.param_env(impl_did);
assert!(!source.has_escaping_bound_vars()); assert!(!source.has_escaping_bound_vars());