Skip to content

Commit ae2a72d

Browse files
committed
Refactor checking function target features during const-eval
* Split into its own function * Do not build a `Vec` of unavailable features
1 parent 3e8ce42 commit ae2a72d

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

compiler/rustc_const_eval/src/interpret/terminator.rs

+29-18
Original file line numberDiff line numberDiff line change
@@ -503,24 +503,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
503503
}
504504
}
505505

506-
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
507-
let missing_features: Vec<_> = attrs
508-
.target_features
509-
.iter()
510-
.copied()
511-
.filter(|feature| !self.tcx.sess.target_features.contains(feature))
512-
.collect();
513-
if !missing_features.is_empty() {
514-
let mut missing_features_str = String::from(missing_features[0].as_str());
515-
for missing_feature in missing_features[1..].iter() {
516-
missing_features_str.push(',');
517-
missing_features_str.push_str(missing_feature.as_str());
518-
}
519-
throw_ub_custom!(
520-
fluent::const_eval_unavailable_target_features_for_fn,
521-
unavailable_feats = missing_features_str,
522-
);
523-
}
506+
// Check that all target features required by the callee (i.e., from
507+
// the attribute `#[target_feature(enable = ...)]`) are enabled at
508+
// compile time.
509+
self.check_fn_target_features(instance)?;
524510

525511
if !callee_fn_abi.can_unwind {
526512
// The callee cannot unwind, so force the `Unreachable` unwind handling.
@@ -805,6 +791,31 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
805791
}
806792
}
807793

794+
fn check_fn_target_features(&self, instance: ty::Instance<'tcx>) -> InterpResult<'tcx, ()> {
795+
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
796+
if attrs
797+
.target_features
798+
.iter()
799+
.any(|feature| !self.tcx.sess.target_features.contains(feature))
800+
{
801+
throw_ub_custom!(
802+
fluent::const_eval_unavailable_target_features_for_fn,
803+
unavailable_feats = attrs
804+
.target_features
805+
.iter()
806+
.filter(|&feature| !self.tcx.sess.target_features.contains(feature))
807+
.fold(String::new(), |mut s, feature| {
808+
if !s.is_empty() {
809+
s.push_str(", ");
810+
}
811+
s.push_str(feature.as_str());
812+
s
813+
}),
814+
);
815+
}
816+
Ok(())
817+
}
818+
808819
fn drop_in_place(
809820
&mut self,
810821
place: &PlaceTy<'tcx, M::Provenance>,

0 commit comments

Comments
 (0)