Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e57221d

Browse files
committed
perf: use is_lang_item over as_lang_item
1 parent e1ad8d3 commit e57221d

File tree

5 files changed

+109
-136
lines changed

5 files changed

+109
-136
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,24 +1083,21 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10831083
ty::ClauseKind::Trait(pred) => {
10841084
// With `feature(sized_hierarchy)`, don't print `?Sized` as an alias for
10851085
// `MetaSized`, and skip sizedness bounds to be added at the end.
1086-
match tcx.as_lang_item(pred.def_id()) {
1087-
Some(LangItem::Sized) => match pred.polarity {
1086+
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
1087+
match pred.polarity {
10881088
ty::PredicatePolarity::Positive => {
10891089
has_sized_bound = true;
10901090
continue;
10911091
}
10921092
ty::PredicatePolarity::Negative => has_negative_sized_bound = true,
1093-
},
1094-
Some(LangItem::MetaSized) => {
1095-
has_meta_sized_bound = true;
1096-
continue;
1097-
}
1098-
Some(LangItem::PointeeSized) => {
1099-
// Unexpected - `PointeeSized` is the absence of bounds.
1100-
has_pointee_sized_bound = true;
1101-
continue;
11021093
}
1103-
_ => (),
1094+
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1095+
has_meta_sized_bound = true;
1096+
continue;
1097+
} else if tcx.is_lang_item(pred.def_id(), LangItem::PointeeSized) {
1098+
// Unexpected - `PointeeSized` is the absence of bounds.
1099+
has_pointee_sized_bound = true;
1100+
continue;
11041101
}
11051102

11061103
self.insert_trait_and_projection(

compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
199199
// avoid inundating the user with unnecessary errors, but we now
200200
// check upstream for type errors and don't add the obligations to
201201
// begin with in those cases.
202-
if matches!(
203-
self.tcx.as_lang_item(trait_pred.def_id()),
204-
Some(LangItem::Sized | LangItem::MetaSized | LangItem::PointeeSized)
205-
) {
202+
if self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Sized)
203+
|| self.tcx.is_lang_item(trait_pred.def_id(), LangItem::MetaSized)
204+
|| self.tcx.is_lang_item(trait_pred.def_id(), LangItem::PointeeSized)
205+
{
206206
match self.tainted_by_errors() {
207207
None => {
208208
let err = self.emit_inference_failure_err(

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

Lines changed: 74 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -64,109 +64,82 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
6464
// and applicable impls. There is a certain set of precedence rules here.
6565
let def_id = obligation.predicate.def_id();
6666
let tcx = self.tcx();
67-
68-
match tcx.as_lang_item(def_id) {
69-
Some(LangItem::Copy) => {
70-
debug!(obligation_self_ty = ?obligation.predicate.skip_binder().self_ty());
71-
72-
// User-defined copy impls are permitted, but only for
73-
// structs and enums.
74-
self.assemble_candidates_from_impls(obligation, &mut candidates);
75-
76-
// For other types, we'll use the builtin rules.
77-
let copy_conditions = self.copy_clone_conditions(obligation);
78-
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates);
79-
}
80-
Some(LangItem::DiscriminantKind) => {
81-
// `DiscriminantKind` is automatically implemented for every type.
82-
candidates.vec.push(BuiltinCandidate { has_nested: false });
83-
}
84-
Some(LangItem::PointeeTrait) => {
85-
// `Pointee` is automatically implemented for every type.
86-
candidates.vec.push(BuiltinCandidate { has_nested: false });
87-
}
88-
Some(LangItem::Sized) => {
89-
self.assemble_builtin_sized_candidate(
90-
obligation,
91-
&mut candidates,
92-
SizedTraitKind::Sized,
93-
);
94-
}
95-
Some(LangItem::MetaSized) => {
96-
self.assemble_builtin_sized_candidate(
97-
obligation,
98-
&mut candidates,
99-
SizedTraitKind::MetaSized,
100-
);
101-
}
102-
Some(LangItem::PointeeSized) => {
103-
bug!("`PointeeSized` is removed during lowering");
104-
}
105-
Some(LangItem::Unsize) => {
106-
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
107-
}
108-
Some(LangItem::Destruct) => {
109-
self.assemble_const_destruct_candidates(obligation, &mut candidates);
110-
}
111-
Some(LangItem::TransmuteTrait) => {
112-
// User-defined transmutability impls are permitted.
113-
self.assemble_candidates_from_impls(obligation, &mut candidates);
114-
self.assemble_candidates_for_transmutability(obligation, &mut candidates);
115-
}
116-
Some(LangItem::Tuple) => {
117-
self.assemble_candidate_for_tuple(obligation, &mut candidates);
118-
}
119-
Some(LangItem::FnPtrTrait) => {
120-
self.assemble_candidates_for_fn_ptr_trait(obligation, &mut candidates);
121-
}
122-
Some(LangItem::BikeshedGuaranteedNoDrop) => {
123-
self.assemble_candidates_for_bikeshed_guaranteed_no_drop_trait(
124-
obligation,
125-
&mut candidates,
126-
);
67+
if tcx.is_lang_item(def_id, LangItem::Copy) {
68+
debug!(obligation_self_ty = ?obligation.predicate.skip_binder().self_ty());
69+
// User-defined copy impls are permitted, but only for
70+
// structs and enums.
71+
self.assemble_candidates_from_impls(obligation, &mut candidates);
72+
// For other types, we'll use the builtin rules.
73+
let copy_conditions = self.copy_clone_conditions(obligation);
74+
self.assemble_builtin_bound_candidates(copy_conditions, &mut candidates);
75+
} else if tcx.is_lang_item(def_id, LangItem::DiscriminantKind) {
76+
// `DiscriminantKind` is automatically implemented for every type.
77+
candidates.vec.push(BuiltinCandidate { has_nested: false });
78+
} else if tcx.is_lang_item(def_id, LangItem::PointeeTrait) {
79+
// `Pointee` is automatically implemented for every type.
80+
candidates.vec.push(BuiltinCandidate { has_nested: false });
81+
} else if tcx.is_lang_item(def_id, LangItem::Sized) {
82+
self.assemble_builtin_sized_candidate(
83+
obligation,
84+
&mut candidates,
85+
SizedTraitKind::Sized,
86+
);
87+
} else if tcx.is_lang_item(def_id, LangItem::MetaSized) {
88+
self.assemble_builtin_sized_candidate(
89+
obligation,
90+
&mut candidates,
91+
SizedTraitKind::MetaSized,
92+
);
93+
} else if tcx.is_lang_item(def_id, LangItem::PointeeSized) {
94+
self.assemble_builtin_sized_candidate(
95+
obligation,
96+
&mut candidates,
97+
SizedTraitKind::PointeeSized,
98+
);
99+
} else if tcx.is_lang_item(def_id, LangItem::Unsize) {
100+
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
101+
} else if tcx.is_lang_item(def_id, LangItem::Destruct) {
102+
self.assemble_const_destruct_candidates(obligation, &mut candidates);
103+
} else if tcx.is_lang_item(def_id, LangItem::TransmuteTrait) {
104+
// User-defined transmutability impls are permitted.
105+
self.assemble_candidates_from_impls(obligation, &mut candidates);
106+
self.assemble_candidates_for_transmutability(obligation, &mut candidates);
107+
} else if tcx.is_lang_item(def_id, LangItem::Tuple) {
108+
self.assemble_candidate_for_tuple(obligation, &mut candidates);
109+
} else if tcx.is_lang_item(def_id, LangItem::FnPtrTrait) {
110+
self.assemble_candidates_for_fn_ptr_trait(obligation, &mut candidates);
111+
} else if tcx.is_lang_item(def_id, LangItem::BikeshedGuaranteedNoDrop) {
112+
self.assemble_candidates_for_bikeshed_guaranteed_no_drop_trait(
113+
obligation,
114+
&mut candidates,
115+
);
116+
} else {
117+
if tcx.is_lang_item(def_id, LangItem::Clone) {
118+
// Same builtin conditions as `Copy`, i.e., every type which has builtin support
119+
// for `Copy` also has builtin support for `Clone`, and tuples/arrays of `Clone`
120+
// types have builtin support for `Clone`.
121+
let clone_conditions = self.copy_clone_conditions(obligation);
122+
self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates);
127123
}
128-
lang_item => {
129-
if matches!(lang_item, Some(LangItem::Clone)) {
130-
// Same builtin conditions as `Copy`, i.e., every type which has builtin support
131-
// for `Copy` also has builtin support for `Clone`, and tuples/arrays of `Clone`
132-
// types have builtin support for `Clone`.
133-
let clone_conditions = self.copy_clone_conditions(obligation);
134-
self.assemble_builtin_bound_candidates(clone_conditions, &mut candidates);
135-
}
136-
137-
match lang_item {
138-
Some(LangItem::Coroutine) => {
139-
self.assemble_coroutine_candidates(obligation, &mut candidates);
140-
}
141-
Some(LangItem::Future) => {
142-
self.assemble_future_candidates(obligation, &mut candidates);
143-
}
144-
Some(LangItem::Iterator) => {
145-
self.assemble_iterator_candidates(obligation, &mut candidates);
146-
}
147-
Some(LangItem::FusedIterator) => {
148-
self.assemble_fused_iterator_candidates(obligation, &mut candidates);
149-
}
150-
Some(LangItem::AsyncIterator) => {
151-
self.assemble_async_iterator_candidates(obligation, &mut candidates);
152-
}
153-
Some(LangItem::AsyncFnKindHelper) => {
154-
self.assemble_async_fn_kind_helper_candidates(
155-
obligation,
156-
&mut candidates,
157-
);
158-
}
159-
_ => (),
160-
}
161-
162-
// FIXME: Put these into `else if` blocks above, since they're built-in.
163-
self.assemble_closure_candidates(obligation, &mut candidates);
164-
self.assemble_async_closure_candidates(obligation, &mut candidates);
165-
self.assemble_fn_pointer_candidates(obligation, &mut candidates);
166-
167-
self.assemble_candidates_from_impls(obligation, &mut candidates);
168-
self.assemble_candidates_from_object_ty(obligation, &mut candidates);
124+
if tcx.is_lang_item(def_id, LangItem::Coroutine) {
125+
self.assemble_coroutine_candidates(obligation, &mut candidates);
126+
} else if tcx.is_lang_item(def_id, LangItem::Future) {
127+
self.assemble_future_candidates(obligation, &mut candidates);
128+
} else if tcx.is_lang_item(def_id, LangItem::Iterator) {
129+
self.assemble_iterator_candidates(obligation, &mut candidates);
130+
} else if tcx.is_lang_item(def_id, LangItem::FusedIterator) {
131+
self.assemble_fused_iterator_candidates(obligation, &mut candidates);
132+
} else if tcx.is_lang_item(def_id, LangItem::AsyncIterator) {
133+
self.assemble_async_iterator_candidates(obligation, &mut candidates);
134+
} else if tcx.is_lang_item(def_id, LangItem::AsyncFnKindHelper) {
135+
self.assemble_async_fn_kind_helper_candidates(obligation, &mut candidates);
169136
}
137+
// FIXME: Put these into `else if` blocks above, since they're built-in.
138+
self.assemble_closure_candidates(obligation, &mut candidates);
139+
self.assemble_async_closure_candidates(obligation, &mut candidates);
140+
self.assemble_fn_pointer_candidates(obligation, &mut candidates);
141+
self.assemble_candidates_from_impls(obligation, &mut candidates);
142+
self.assemble_candidates_from_object_ty(obligation, &mut candidates);
170143
}
171144

172145
self.assemble_candidates_from_projected_tys(obligation, &mut candidates);

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,20 +261,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
261261
let tcx = self.tcx();
262262
let obligations = if has_nested {
263263
let trait_def = obligation.predicate.def_id();
264-
let conditions = match tcx.as_lang_item(trait_def) {
265-
Some(LangItem::Sized) => {
266-
self.sizedness_conditions(obligation, SizedTraitKind::Sized)
267-
}
268-
Some(LangItem::MetaSized) => {
269-
self.sizedness_conditions(obligation, SizedTraitKind::MetaSized)
270-
}
271-
Some(LangItem::PointeeSized) => {
272-
self.sizedness_conditions(obligation, SizedTraitKind::PointeeSized)
273-
}
274-
Some(LangItem::Copy) => self.copy_clone_conditions(obligation),
275-
Some(LangItem::Clone) => self.copy_clone_conditions(obligation),
276-
Some(LangItem::FusedIterator) => self.fused_iterator_conditions(obligation),
277-
_ => bug!("unexpected builtin trait {:?}", trait_def),
264+
let conditions = if tcx.is_lang_item(trait_def, LangItem::Sized) {
265+
self.sizedness_conditions(obligation, SizedTraitKind::Sized)
266+
} else if tcx.is_lang_item(trait_def, LangItem::MetaSized) {
267+
self.sizedness_conditions(obligation, SizedTraitKind::MetaSized)
268+
} else if tcx.is_lang_item(trait_def, LangItem::PointeeSized) {
269+
self.sizedness_conditions(obligation, SizedTraitKind::PointeeSized)
270+
} else if tcx.is_lang_item(trait_def, LangItem::Copy) {
271+
self.copy_clone_conditions(obligation)
272+
} else if tcx.is_lang_item(trait_def, LangItem::Clone) {
273+
self.copy_clone_conditions(obligation)
274+
} else if tcx.is_lang_item(trait_def, LangItem::FusedIterator) {
275+
self.fused_iterator_conditions(obligation)
276+
} else {
277+
bug!("unexpected builtin trait {:?}", trait_def)
278278
};
279279
let BuiltinImplConditions::Where(types) = conditions else {
280280
bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);

compiler/rustc_trait_selection/src/traits/util.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,14 @@ pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
516516
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_ref)) =
517517
predicate.kind().skip_binder()
518518
{
519-
let sizedness = match tcx.as_lang_item(trait_ref.def_id()) {
520-
Some(LangItem::Sized) => SizedTraitKind::Sized,
521-
Some(LangItem::MetaSized) => SizedTraitKind::MetaSized,
522-
Some(LangItem::PointeeSized) => SizedTraitKind::PointeeSized,
523-
_ => return false,
519+
let sizedness = if tcx.is_lang_item(trait_ref.def_id(), LangItem::Sized) {
520+
SizedTraitKind::Sized
521+
} else if tcx.is_lang_item(trait_ref.def_id(), LangItem::MetaSized) {
522+
SizedTraitKind::MetaSized
523+
} else if tcx.is_lang_item(trait_ref.def_id(), LangItem::PointeeSized) {
524+
SizedTraitKind::PointeeSized
525+
} else {
526+
return false;
524527
};
525528

526529
if trait_ref.self_ty().has_trivial_sizedness(tcx, sizedness) {

0 commit comments

Comments
 (0)