From 8e899b1cbe6209842e112f0b7ec450a3cdfdb36d Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sat, 27 Jun 2020 15:51:51 -0400 Subject: [PATCH] Don't implement Fn* for unsafe #[target_feature] functions --- .../traits/error_reporting/mod.rs | 8 +++----- .../ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 39530853318..76e01a40a23 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -292,11 +292,9 @@ fn report_selection_error( self.tcx.lang_items().fn_once_trait(), ] .contains(&Some(trait_ref.def_id())); - let is_safe_target_feature_fn = + let is_target_feature_fn = if let ty::FnDef(def_id, _) = trait_ref.skip_binder().self_ty().kind { - trait_ref.skip_binder().self_ty().fn_sig(self.tcx).unsafety() - == hir::Unsafety::Normal - && !self.tcx.codegen_fn_attrs(def_id).target_features.is_empty() + !self.tcx.codegen_fn_attrs(def_id).target_features.is_empty() } else { false }; @@ -441,7 +439,7 @@ fn report_selection_error( ); } - if is_fn_trait && is_safe_target_feature_fn { + if is_fn_trait && is_target_feature_fn { err.note(&format!( "`{}` has `#[target_feature]` and is unsafe to call", trait_ref.skip_binder().self_ty(), diff --git a/src/test/ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr b/src/test/ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr index 3ae85af76d3..ba3d2eac1a6 100644 --- a/src/test/ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr +++ b/src/test/ui/rfcs/rfc-2396-target_feature-11/fn-traits.stderr @@ -48,6 +48,7 @@ LL | call(foo_unsafe); | = help: the trait `std::ops::Fn<()>` is not implemented for `unsafe fn() {foo_unsafe}` = note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ } + = note: `unsafe fn() {foo_unsafe}` has `#[target_feature]` and is unsafe to call error[E0277]: expected a `std::ops::FnMut<()>` closure, found `unsafe fn() {foo_unsafe}` --> $DIR/fn-traits.rs:30:14 @@ -60,6 +61,7 @@ LL | call_mut(foo_unsafe); | = help: the trait `std::ops::FnMut<()>` is not implemented for `unsafe fn() {foo_unsafe}` = note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ } + = note: `unsafe fn() {foo_unsafe}` has `#[target_feature]` and is unsafe to call error[E0277]: expected a `std::ops::FnOnce<()>` closure, found `unsafe fn() {foo_unsafe}` --> $DIR/fn-traits.rs:32:15 @@ -72,6 +74,7 @@ LL | call_once(foo_unsafe); | = help: the trait `std::ops::FnOnce<()>` is not implemented for `unsafe fn() {foo_unsafe}` = note: wrap the `unsafe fn() {foo_unsafe}` in a closure with no arguments: `|| { /* code */ } + = note: `unsafe fn() {foo_unsafe}` has `#[target_feature]` and is unsafe to call error: aborting due to 6 previous errors -- 2.44.0