Skip to content

Commit b0266a5

Browse files
committed
Propagate #[target_feature] attribute due to changes in Rust 1.86
1 parent cc3db71 commit b0266a5

6 files changed

Lines changed: 69 additions & 2 deletions

File tree

bon-macros/src/builder/builder_gen/finish_fn.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,23 @@ impl super::BuilderGenCtx {
157157
let state_var = &self.state_var;
158158
let const_ = &self.const_;
159159

160+
// `#[target_feature]` is not compatible with `#[inline(always)]`,
161+
// so we need to downgrade it to `#[inline]
162+
let inline_attr = self
163+
.finish_fn
164+
.special_attrs
165+
.iter()
166+
.find_map(|attr| {
167+
attr.meta
168+
.path()
169+
.is_ident("target_feature")
170+
.then(|| quote! { #[inline] })
171+
})
172+
.unwrap_or_else(|| quote! { #[inline(always)] });
173+
160174
quote! {
161175
#(#attrs)*
162-
#[inline(always)]
176+
#inline_attr
163177
#[allow(
164178
// This is intentional. We want the builder syntax to compile away
165179
clippy::inline_always,

bon-macros/src/builder/builder_gen/input_fn/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ fn merge_generic_params(
485485
.collect()
486486
}
487487

488-
const PROPAGATED_ATTRIBUTES: &[&str] = &["must_use", "track_caller"];
488+
const PROPAGATED_ATTRIBUTES: &[&str] = &["must_use", "track_caller", "target_feature"];
489489

490490
fn get_propagated_attrs(attrs: &[syn::Attribute]) -> Result<Vec<syn::Attribute>> {
491491
PROPAGATED_ATTRIBUTES

bon/tests/integration/builder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod orig_fn_naming;
2525
mod positional_members;
2626
mod raw_idents;
2727
mod smoke;
28+
mod target_feature;
2829
mod track_caller;
2930

3031
use crate::prelude::*;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#[rustversion::since(1.86.0)]
2+
mod msrv_1_86 {
3+
#![allow(dead_code)]
4+
use crate::prelude::*;
5+
6+
#[test]
7+
fn target_feature_fn() {
8+
#[builder]
9+
#[target_feature(enable = "avx2")]
10+
fn building_but_wider(_x: [u8; 32], _y: [u32; 8]) {}
11+
12+
#[target_feature(enable = "avx2")]
13+
#[allow(unsafe_code, reason = "we don't do anything with it")]
14+
unsafe fn wider() {
15+
building_but_wider().x([0; 32]).y([1; 8]).call();
16+
}
17+
}
18+
19+
#[test]
20+
fn target_feature_impl_block() {
21+
#[repr(C, align(32))]
22+
struct Brick([u8; 32]);
23+
struct Senti;
24+
25+
#[bon]
26+
impl Senti {
27+
#[builder(finish_fn = yatta_but_wide)]
28+
#[target_feature(enable = "avx2")]
29+
fn new(brick: Brick) -> Self {
30+
let Brick(_) = brick;
31+
Self
32+
}
33+
}
34+
35+
#[target_feature(enable = "avx2")]
36+
#[allow(unsafe_code, reason = "we don't do anything with it")]
37+
unsafe fn briiick() {
38+
Senti::builder().brick(Brick([0; 32])).yatta_but_wide();
39+
}
40+
}
41+
}

bon/tests/integration/ui/compile_fail/attr_builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,8 @@ fn destructuring2((_, _): (u32, u32)) {}
7272
#[track_caller]
7373
#[track_caller]
7474
fn double_track_caller() {}
75+
76+
#[builder]
77+
#[target_feature(enable = "sse2")]
78+
#[target_feature(enable = "avx")]
79+
fn double_target_feature() {}

bon/tests/integration/ui/compile_fail/attr_builder.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,9 @@ error: found multiple #[track_caller], but bon only works with exactly one or ze
8989
|
9090
73 | #[track_caller]
9191
| ^
92+
93+
error: found multiple #[target_feature], but bon only works with exactly one or zero.
94+
--> tests/integration/ui/compile_fail/attr_builder.rs:78:1
95+
|
96+
78 | #[target_feature(enable = "avx")]
97+
| ^

0 commit comments

Comments
 (0)