From 769b4108e288c4074b502336a38a5c39bfff555a Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Thu, 22 Oct 2020 13:55:19 +0200 Subject: [PATCH 1/2] Make closures inherit the parent function's target features --- compiler/rustc_typeck/src/collect.rs | 10 +++++++++- .../closures-inherit-target_feature.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/rfcs/rfc-2396-target_feature-11/closures-inherit-target_feature.rs diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index ac2a4a464a782..b30fb7be273f1 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -40,7 +40,7 @@ use rustc_middle::ty::query::Providers; use rustc_middle::ty::subst::InternalSubsts; use rustc_middle::ty::util::Discr; use rustc_middle::ty::util::IntTypeExt; -use rustc_middle::ty::{self, AdtKind, Const, ToPolyTraitRef, Ty, TyCtxt}; +use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, ToPolyTraitRef, Ty, TyCtxt}; use rustc_middle::ty::{ReprOptions, ToPredicate, WithConstness}; use rustc_session::config::SanitizerSet; use rustc_session::lint; @@ -2786,6 +2786,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { } }); + // #73631: closures inherit `#[target_feature]` annotations + if tcx.features().target_feature_11 && tcx.is_closure(id) { + let owner_id = tcx.parent(id).expect("closure should have a parent"); + codegen_fn_attrs + .target_features + .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied()) + } + // If a function uses #[target_feature] it can't be inlined into general // purpose functions as they wouldn't have the right target features // enabled. For that reason we also forbid #[inline(always)] as it can't be diff --git a/src/test/ui/rfcs/rfc-2396-target_feature-11/closures-inherit-target_feature.rs b/src/test/ui/rfcs/rfc-2396-target_feature-11/closures-inherit-target_feature.rs new file mode 100644 index 0000000000000..e6c81f56ab863 --- /dev/null +++ b/src/test/ui/rfcs/rfc-2396-target_feature-11/closures-inherit-target_feature.rs @@ -0,0 +1,18 @@ +// Tests #73631: closures inherit `#[target_feature]` annotations + +// check-pass +// only-x86_64 + +#![feature(target_feature_11)] + +#[target_feature(enable="avx")] +fn also_use_avx() { + println!("Hello from AVX") +} + +#[target_feature(enable="avx")] +fn use_avx() -> Box { + Box::new(|| also_use_avx()) +} + +fn main() {} \ No newline at end of file From b4a9854afe0ab75f1b0e0c46a9342c835d5bf7b9 Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Thu, 22 Oct 2020 14:37:42 +0200 Subject: [PATCH 2/2] Fixup: add missing trailing newline --- .../closures-inherit-target_feature.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/rfcs/rfc-2396-target_feature-11/closures-inherit-target_feature.rs b/src/test/ui/rfcs/rfc-2396-target_feature-11/closures-inherit-target_feature.rs index e6c81f56ab863..af35bc2014bfe 100644 --- a/src/test/ui/rfcs/rfc-2396-target_feature-11/closures-inherit-target_feature.rs +++ b/src/test/ui/rfcs/rfc-2396-target_feature-11/closures-inherit-target_feature.rs @@ -15,4 +15,4 @@ fn use_avx() -> Box { Box::new(|| also_use_avx()) } -fn main() {} \ No newline at end of file +fn main() {}