Skip to content

Commit

Permalink
const-eval: allow calling functions with targat features disabled at …
Browse files Browse the repository at this point in the history
…compile time in WASM

This is not unsafe on WASM, see #84988
  • Loading branch information
eduardosm committed Oct 9, 2023
1 parent be581d9 commit 4974f89
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 6 additions & 4 deletions compiler/rustc_const_eval/src/interpret/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}

fn check_fn_target_features(&self, instance: ty::Instance<'tcx>) -> InterpResult<'tcx, ()> {
// Calling functions with `#[target_feature]` is not unsafe on WASM, see #84988
let attrs = self.tcx.codegen_fn_attrs(instance.def_id());
if attrs
.target_features
.iter()
.any(|feature| !self.tcx.sess.target_features.contains(feature))
if !self.tcx.sess.target.is_like_wasm
&& attrs
.target_features
.iter()
.any(|feature| !self.tcx.sess.target_features.contains(feature))
{
throw_ub_custom!(
fluent::const_eval_unavailable_target_features_for_fn,
Expand Down
11 changes: 11 additions & 0 deletions src/tools/miri/tests/pass/function_calls/target_feature_wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@only-target-wasm32: tests WASM-specific behavior
//@compile-flags: -C target-feature=-simd128

fn main() {
// Calling functions with `#[target_feature]` is not unsafe on WASM, see #84988
assert!(!cfg!(target_feature = "simd128"));
simd128_fn();
}

#[target_feature(enable = "simd128")]
fn simd128_fn() {}
14 changes: 14 additions & 0 deletions tests/ui/consts/const-eval/const_fn_target_feature_wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// only-wasm32
// compile-flags:-C target-feature=-simd128
// build-pass

#![crate_type = "lib"]

#[cfg(target_feature = "simd128")]
compile_error!("simd128 target feature should be disabled");

// Calling functions with `#[target_feature]` is not unsafe on WASM, see #84988
const A: () = simd128_fn();

#[target_feature(enable = "simd128")]
const fn simd128_fn() {}

0 comments on commit 4974f89

Please sign in to comment.