Skip to content

Commit ba87be0

Browse files
Short-circuit some trivially const Drop types
1 parent 33e5efb commit ba87be0

File tree

2 files changed

+45
-10
lines changed
  • compiler

2 files changed

+45
-10
lines changed

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,10 @@ impl Qualif for NeedsNonConstDrop {
146146
qualifs.needs_non_const_drop
147147
}
148148

149-
fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, mut ty: Ty<'tcx>) -> bool {
150-
// Avoid selecting for simple cases.
151-
match ty::util::needs_drop_components(ty, &cx.tcx.data_layout).as_deref() {
152-
Ok([]) => return false,
153-
Err(ty::util::AlwaysRequiresDrop) => return true,
154-
// If we've got a single component, select with that
155-
// to increase the chance that we hit the selection cache.
156-
Ok([t]) => ty = t,
157-
Ok([..]) => {}
149+
fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
150+
// Avoid selecting for simple cases, such as builtin types.
151+
if ty::util::trivial_const_drop(ty) {
152+
return false;
158153
}
159154

160155
let Some(drop_trait) = cx.tcx.lang_items().drop_trait() else {
@@ -187,11 +182,15 @@ impl Qualif for NeedsNonConstDrop {
187182
impl_src,
188183
ImplSource::ConstDrop(_) | ImplSource::Param(_, ty::BoundConstness::ConstIfConst)
189184
) {
190-
// If our const drop candidate is not ConstDrop or implied by param,
185+
// If our const drop candidate is not ConstDrop or implied by the param env,
191186
// then it's bad
192187
return true;
193188
}
194189

190+
if impl_src.borrow_nested_obligations().is_empty() {
191+
return false;
192+
}
193+
195194
// If we successfully found one, then select all of the predicates
196195
// implied by our const drop impl.
197196
let mut fcx = FulfillmentContext::new();

compiler/rustc_middle/src/ty/util.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,42 @@ pub fn needs_drop_components<'tcx>(
10411041
}
10421042
}
10431043

1044+
pub fn trivial_const_drop<'tcx>(ty: Ty<'tcx>) -> bool {
1045+
match *ty.kind() {
1046+
ty::Bool
1047+
| ty::Char
1048+
| ty::Int(_)
1049+
| ty::Uint(_)
1050+
| ty::Float(_)
1051+
| ty::Infer(ty::IntVar(_))
1052+
| ty::Infer(ty::FloatVar(_))
1053+
| ty::Str
1054+
| ty::RawPtr(_)
1055+
| ty::Ref(..)
1056+
| ty::FnDef(..)
1057+
| ty::FnPtr(_) => true,
1058+
1059+
ty::Opaque(..)
1060+
| ty::Dynamic(..)
1061+
| ty::Error(_)
1062+
| ty::Bound(..)
1063+
| ty::Param(_)
1064+
| ty::Placeholder(_)
1065+
| ty::Never
1066+
| ty::Foreign(_)
1067+
| ty::Projection(_)
1068+
| ty::Infer(_) => false,
1069+
1070+
// Not trivial because they have components, and instead of looking inside,
1071+
// we'll just perform trait selection.
1072+
ty::Closure(..) | ty::Generator(..) | ty::GeneratorWitness(_) | ty::Adt(..) => false,
1073+
1074+
ty::Array(ty, _) | ty::Slice(ty) => trivial_const_drop(ty),
1075+
1076+
ty::Tuple(tys) => tys.iter().all(|ty| trivial_const_drop(ty.expect_ty())),
1077+
}
1078+
}
1079+
10441080
// Does the equivalent of
10451081
// ```
10461082
// let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();

0 commit comments

Comments
 (0)