Skip to content

Commit 00c4dcd

Browse files
authored
Rollup merge of #78231 - LeSeulArtichaut:closure-target_feature, r=nikomatsakis
Make closures inherit the parent function's target features r? @ghost Closes #73631
2 parents 29461c1 + b4a9854 commit 00c4dcd

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

compiler/rustc_typeck/src/collect.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_middle::ty::query::Providers;
4040
use rustc_middle::ty::subst::InternalSubsts;
4141
use rustc_middle::ty::util::Discr;
4242
use rustc_middle::ty::util::IntTypeExt;
43-
use rustc_middle::ty::{self, AdtKind, Const, ToPolyTraitRef, Ty, TyCtxt};
43+
use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, ToPolyTraitRef, Ty, TyCtxt};
4444
use rustc_middle::ty::{ReprOptions, ToPredicate, WithConstness};
4545
use rustc_session::config::SanitizerSet;
4646
use rustc_session::lint;
@@ -2786,6 +2786,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27862786
}
27872787
});
27882788

2789+
// #73631: closures inherit `#[target_feature]` annotations
2790+
if tcx.features().target_feature_11 && tcx.is_closure(id) {
2791+
let owner_id = tcx.parent(id).expect("closure should have a parent");
2792+
codegen_fn_attrs
2793+
.target_features
2794+
.extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied())
2795+
}
2796+
27892797
// If a function uses #[target_feature] it can't be inlined into general
27902798
// purpose functions as they wouldn't have the right target features
27912799
// enabled. For that reason we also forbid #[inline(always)] as it can't be
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Tests #73631: closures inherit `#[target_feature]` annotations
2+
3+
// check-pass
4+
// only-x86_64
5+
6+
#![feature(target_feature_11)]
7+
8+
#[target_feature(enable="avx")]
9+
fn also_use_avx() {
10+
println!("Hello from AVX")
11+
}
12+
13+
#[target_feature(enable="avx")]
14+
fn use_avx() -> Box<dyn Fn()> {
15+
Box::new(|| also_use_avx())
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)