Skip to content

Commit d5c48eb

Browse files
Add TyCtxt::is_lang_item
1 parent f8e5660 commit d5c48eb

File tree

6 files changed

+42
-40
lines changed

6 files changed

+42
-40
lines changed

compiler/rustc_middle/src/middle/lang_items.rs

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ impl<'tcx> TyCtxt<'tcx> {
2323
})
2424
}
2525

26+
pub fn is_lang_item(self, def_id: DefId, lang_item: LangItem) -> bool {
27+
self.lang_items().get(lang_item) == Some(def_id)
28+
}
29+
2630
/// Given a [`DefId`] of one of the [`Fn`], [`FnMut`] or [`FnOnce`] traits,
2731
/// returns a corresponding [`ty::ClosureKind`].
2832
/// For any other [`DefId`] return `None`.

compiler/rustc_monomorphize/src/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ fn visit_instance_use<'tcx>(
945945
// be lowered in codegen to nothing or a call to panic_nounwind. So if we encounter any
946946
// of those intrinsics, we need to include a mono item for panic_nounwind, else we may try to
947947
// codegen a call to that function without generating code for the function itself.
948-
let def_id = tcx.lang_items().get(LangItem::PanicNounwind).unwrap();
948+
let def_id = tcx.require_lang_item(LangItem::PanicNounwind, None);
949949
let panic_instance = Instance::mono(tcx, def_id);
950950
if should_codegen_locally(tcx, panic_instance) {
951951
output.push(create_fn_mono_item(tcx, panic_instance, source));

compiler/rustc_trait_selection/src/solve/assembly/mod.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Code shared by trait and projection goals for candidate assembly.
22
33
use rustc_hir::def_id::DefId;
4+
use rustc_hir::LangItem;
45
use rustc_infer::infer::InferCtxt;
56
use rustc_infer::traits::query::NoSolution;
67
use rustc_middle::bug;
@@ -481,7 +482,6 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
481482
candidates: &mut Vec<Candidate<'tcx>>,
482483
) {
483484
let tcx = self.interner();
484-
let lang_items = tcx.lang_items();
485485
let trait_def_id = goal.predicate.trait_def_id(tcx);
486486

487487
// N.B. When assembling built-in candidates for lang items that are also
@@ -497,43 +497,43 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
497497
G::consider_auto_trait_candidate(self, goal)
498498
} else if tcx.trait_is_alias(trait_def_id) {
499499
G::consider_trait_alias_candidate(self, goal)
500-
} else if lang_items.sized_trait() == Some(trait_def_id) {
500+
} else if tcx.is_lang_item(trait_def_id, LangItem::Sized) {
501501
G::consider_builtin_sized_candidate(self, goal)
502-
} else if lang_items.copy_trait() == Some(trait_def_id)
503-
|| lang_items.clone_trait() == Some(trait_def_id)
502+
} else if tcx.is_lang_item(trait_def_id, LangItem::Copy)
503+
|| tcx.is_lang_item(trait_def_id, LangItem::Clone)
504504
{
505505
G::consider_builtin_copy_clone_candidate(self, goal)
506-
} else if lang_items.pointer_like() == Some(trait_def_id) {
506+
} else if tcx.is_lang_item(trait_def_id, LangItem::PointerLike) {
507507
G::consider_builtin_pointer_like_candidate(self, goal)
508-
} else if lang_items.fn_ptr_trait() == Some(trait_def_id) {
508+
} else if tcx.is_lang_item(trait_def_id, LangItem::FnPtrTrait) {
509509
G::consider_builtin_fn_ptr_trait_candidate(self, goal)
510510
} else if let Some(kind) = self.interner().fn_trait_kind_from_def_id(trait_def_id) {
511511
G::consider_builtin_fn_trait_candidates(self, goal, kind)
512512
} else if let Some(kind) = self.interner().async_fn_trait_kind_from_def_id(trait_def_id) {
513513
G::consider_builtin_async_fn_trait_candidates(self, goal, kind)
514-
} else if lang_items.async_fn_kind_helper() == Some(trait_def_id) {
514+
} else if tcx.is_lang_item(trait_def_id, LangItem::AsyncFnKindHelper) {
515515
G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
516-
} else if lang_items.tuple_trait() == Some(trait_def_id) {
516+
} else if tcx.is_lang_item(trait_def_id, LangItem::Tuple) {
517517
G::consider_builtin_tuple_candidate(self, goal)
518-
} else if lang_items.pointee_trait() == Some(trait_def_id) {
518+
} else if tcx.is_lang_item(trait_def_id, LangItem::PointeeTrait) {
519519
G::consider_builtin_pointee_candidate(self, goal)
520-
} else if lang_items.future_trait() == Some(trait_def_id) {
520+
} else if tcx.is_lang_item(trait_def_id, LangItem::Future) {
521521
G::consider_builtin_future_candidate(self, goal)
522-
} else if lang_items.iterator_trait() == Some(trait_def_id) {
522+
} else if tcx.is_lang_item(trait_def_id, LangItem::Iterator) {
523523
G::consider_builtin_iterator_candidate(self, goal)
524-
} else if lang_items.fused_iterator_trait() == Some(trait_def_id) {
524+
} else if tcx.is_lang_item(trait_def_id, LangItem::FusedIterator) {
525525
G::consider_builtin_fused_iterator_candidate(self, goal)
526-
} else if lang_items.async_iterator_trait() == Some(trait_def_id) {
526+
} else if tcx.is_lang_item(trait_def_id, LangItem::AsyncIterator) {
527527
G::consider_builtin_async_iterator_candidate(self, goal)
528-
} else if lang_items.coroutine_trait() == Some(trait_def_id) {
528+
} else if tcx.is_lang_item(trait_def_id, LangItem::Coroutine) {
529529
G::consider_builtin_coroutine_candidate(self, goal)
530-
} else if lang_items.discriminant_kind_trait() == Some(trait_def_id) {
530+
} else if tcx.is_lang_item(trait_def_id, LangItem::DiscriminantKind) {
531531
G::consider_builtin_discriminant_kind_candidate(self, goal)
532-
} else if lang_items.async_destruct_trait() == Some(trait_def_id) {
532+
} else if tcx.is_lang_item(trait_def_id, LangItem::AsyncDestruct) {
533533
G::consider_builtin_async_destruct_candidate(self, goal)
534-
} else if lang_items.destruct_trait() == Some(trait_def_id) {
534+
} else if tcx.is_lang_item(trait_def_id, LangItem::Destruct) {
535535
G::consider_builtin_destruct_candidate(self, goal)
536-
} else if lang_items.transmute_trait() == Some(trait_def_id) {
536+
} else if tcx.is_lang_item(trait_def_id, LangItem::TransmuteTrait) {
537537
G::consider_builtin_transmute_candidate(self, goal)
538538
} else {
539539
Err(NoSolution)
@@ -543,7 +543,7 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
543543

544544
// There may be multiple unsize candidates for a trait with several supertraits:
545545
// `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
546-
if lang_items.unsize_trait() == Some(trait_def_id) {
546+
if tcx.is_lang_item(trait_def_id, LangItem::Unsize) {
547547
candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
548548
}
549549
}

compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,8 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
392392
output_coroutine_ty,
393393
coroutine_return_ty,
394394
}| {
395-
let lang_items = tcx.lang_items();
396-
let (projection_term, term) = if Some(goal.predicate.def_id())
397-
== lang_items.call_once_future()
395+
let (projection_term, term) = if tcx
396+
.is_lang_item(goal.predicate.def_id(), LangItem::CallOnceFuture)
398397
{
399398
(
400399
ty::AliasTerm::new(
@@ -404,7 +403,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
404403
),
405404
output_coroutine_ty.into(),
406405
)
407-
} else if Some(goal.predicate.def_id()) == lang_items.call_ref_future() {
406+
} else if tcx.is_lang_item(goal.predicate.def_id(), LangItem::CallRefFuture) {
408407
(
409408
ty::AliasTerm::new(
410409
tcx,
@@ -417,7 +416,8 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
417416
),
418417
output_coroutine_ty.into(),
419418
)
420-
} else if Some(goal.predicate.def_id()) == lang_items.async_fn_once_output() {
419+
} else if tcx.is_lang_item(goal.predicate.def_id(), LangItem::AsyncFnOnceOutput)
420+
{
421421
(
422422
ty::AliasTerm::new(
423423
tcx,
@@ -719,10 +719,9 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
719719

720720
let coroutine = args.as_coroutine();
721721

722-
let lang_items = tcx.lang_items();
723-
let term = if Some(goal.predicate.def_id()) == lang_items.coroutine_return() {
722+
let term = if tcx.is_lang_item(goal.predicate.def_id(), LangItem::CoroutineReturn) {
724723
coroutine.return_ty().into()
725-
} else if Some(goal.predicate.def_id()) == lang_items.coroutine_yield() {
724+
} else if tcx.is_lang_item(goal.predicate.def_id(), LangItem::CoroutineYield) {
726725
coroutine.yield_ty().into()
727726
} else {
728727
bug!(

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -802,14 +802,13 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
802802
);
803803

804804
// The type must be `Sized` to be unsized.
805-
if let Some(sized_def_id) = tcx.lang_items().sized_trait() {
806-
ecx.add_goal(
807-
GoalSource::ImplWhereBound,
808-
goal.with(tcx, ty::TraitRef::new(tcx, sized_def_id, [a_ty])),
809-
);
810-
} else {
811-
return Err(NoSolution);
812-
}
805+
ecx.add_goal(
806+
GoalSource::ImplWhereBound,
807+
goal.with(
808+
tcx,
809+
ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::Sized, None), [a_ty]),
810+
),
811+
);
813812

814813
// The type must outlive the lifetime of the `dyn` we're unsizing into.
815814
ecx.add_goal(GoalSource::Misc, goal.with(tcx, ty::OutlivesPredicate(a_ty, b_region)));
@@ -991,7 +990,7 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
991990
tcx,
992991
ty::TraitRef::new(
993992
tcx,
994-
tcx.lang_items().unsize_trait().unwrap(),
993+
tcx.require_lang_item(LangItem::Unsize, None),
995994
[a_tail_ty, b_tail_ty],
996995
),
997996
),
@@ -1034,7 +1033,7 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
10341033
tcx,
10351034
ty::TraitRef::new(
10361035
tcx,
1037-
tcx.lang_items().unsize_trait().unwrap(),
1036+
tcx.require_lang_item(LangItem::Unsize, None),
10381037
[a_last_ty, b_last_ty],
10391038
),
10401039
),
@@ -1076,7 +1075,7 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
10761075
// takes precedence over the structural auto trait candidate being
10771076
// assembled.
10781077
ty::Coroutine(def_id, _)
1079-
if Some(goal.predicate.def_id()) == self.interner().lang_items().unpin_trait() =>
1078+
if self.interner().is_lang_item(goal.predicate.def_id(), LangItem::Unpin) =>
10801079
{
10811080
match self.interner().coroutine_movability(def_id) {
10821081
Movability::Static => Some(Err(NoSolution)),

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
338338
let make_freeze_obl = |ty| {
339339
let trait_ref = ty::TraitRef::new(
340340
tcx,
341-
tcx.lang_items().freeze_trait().unwrap(),
341+
tcx.require_lang_item(LangItem::Freeze, None),
342342
[ty::GenericArg::from(ty)],
343343
);
344344
Obligation::with_depth(

0 commit comments

Comments
 (0)