diff --git a/src/codegen/bitfield_unit.rs b/src/codegen/bitfield_unit.rs index 5c7a09bd6b..caab2dc69a 100755 --- a/src/codegen/bitfield_unit.rs +++ b/src/codegen/bitfield_unit.rs @@ -9,7 +9,7 @@ pub struct __BindgenBitfieldUnit impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [], diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index c3b8698319..551c0bdb91 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -410,7 +410,7 @@ impl CodeGenerator for Module { utils::prepend_objc_header(ctx, &mut *result); } if result.saw_bitfield_unit { - utils::prepend_bitfield_unit_type(&mut *result); + utils::prepend_bitfield_unit_type(ctx, &mut *result); } } }; @@ -3591,11 +3591,21 @@ mod utils { use ir::item::{Item, ItemCanonicalPath}; use ir::ty::TypeKind; use proc_macro2; + use std::borrow::Cow; use std::mem; use std::str::FromStr; - pub fn prepend_bitfield_unit_type(result: &mut Vec) { - let bitfield_unit_type = proc_macro2::TokenStream::from_str(include_str!("./bitfield_unit.rs")).unwrap(); + pub fn prepend_bitfield_unit_type( + ctx: &BindgenContext, + result: &mut Vec + ) { + let bitfield_unit_src = include_str!("./bitfield_unit.rs"); + let bitfield_unit_src = if ctx.options().rust_features().min_const_fn { + Cow::Borrowed(bitfield_unit_src) + } else { + Cow::Owned(bitfield_unit_src.replace("const fn ", "fn ")) + }; + let bitfield_unit_type = proc_macro2::TokenStream::from_str(&bitfield_unit_src).unwrap(); let bitfield_unit_type = quote!(#bitfield_unit_type); let items = vec![bitfield_unit_type]; diff --git a/src/features.rs b/src/features.rs index 50759c3189..05f5dc422e 100644 --- a/src/features.rs +++ b/src/features.rs @@ -102,6 +102,8 @@ macro_rules! rust_target_base { => Stable_1_27 => 1.27; /// Rust stable 1.28 => Stable_1_28 => 1.28; + /// Rust stable 1.30 + => Stable_1_30 => 1.30; /// Rust stable 1.33 => Stable_1_33 => 1.33; /// Nightly rust @@ -192,6 +194,11 @@ rust_feature_def!( /// repr(transparent) ([PR](https://github.com/rust-lang/rust/pull/51562)) => repr_transparent; } + Stable_1_30 { + /// `const fn` support for limited cases + /// ([PR](https://github.com/rust-lang/rust/pull/54835/) + => min_const_fn; + } Stable_1_33 { /// repr(packed(N)) ([PR](https://github.com/rust-lang/rust/pull/57049)) => repr_packed_n; diff --git a/tests/expectations/tests/bitfield-32bit-overflow.rs b/tests/expectations/tests/bitfield-32bit-overflow.rs index 904a6ccc8d..361b0e3ae0 100644 --- a/tests/expectations/tests/bitfield-32bit-overflow.rs +++ b/tests/expectations/tests/bitfield-32bit-overflow.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/bitfield-large.rs b/tests/expectations/tests/bitfield-large.rs index 0e069b0130..6597e9672c 100644 --- a/tests/expectations/tests/bitfield-large.rs +++ b/tests/expectations/tests/bitfield-large.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/bitfield-method-same-name.rs b/tests/expectations/tests/bitfield-method-same-name.rs index bbb3ac60d1..a105f026b5 100644 --- a/tests/expectations/tests/bitfield-method-same-name.rs +++ b/tests/expectations/tests/bitfield-method-same-name.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/bitfield_align.rs b/tests/expectations/tests/bitfield_align.rs index e11751aab5..7fd1f2eb2a 100644 --- a/tests/expectations/tests/bitfield_align.rs +++ b/tests/expectations/tests/bitfield_align.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/bitfield_align_2.rs b/tests/expectations/tests/bitfield_align_2.rs index eb4e32b848..ee3cfea5e5 100644 --- a/tests/expectations/tests/bitfield_align_2.rs +++ b/tests/expectations/tests/bitfield_align_2.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/bitfield_method_mangling.rs b/tests/expectations/tests/bitfield_method_mangling.rs index e9887ce95f..9989bfe09e 100644 --- a/tests/expectations/tests/bitfield_method_mangling.rs +++ b/tests/expectations/tests/bitfield_method_mangling.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/derive-bitfield-method-same-name.rs b/tests/expectations/tests/derive-bitfield-method-same-name.rs index d00429cb62..abe57781a5 100644 --- a/tests/expectations/tests/derive-bitfield-method-same-name.rs +++ b/tests/expectations/tests/derive-bitfield-method-same-name.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/derive-debug-bitfield-core.rs b/tests/expectations/tests/derive-debug-bitfield-core.rs index 3f07d8ffc0..882bb9d632 100644 --- a/tests/expectations/tests/derive-debug-bitfield-core.rs +++ b/tests/expectations/tests/derive-debug-bitfield-core.rs @@ -17,7 +17,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/derive-debug-bitfield.rs b/tests/expectations/tests/derive-debug-bitfield.rs index b9e14337c7..d72dd98772 100644 --- a/tests/expectations/tests/derive-debug-bitfield.rs +++ b/tests/expectations/tests/derive-debug-bitfield.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/derive-partialeq-bitfield.rs b/tests/expectations/tests/derive-partialeq-bitfield.rs index 857e0414c0..c183fc0f84 100644 --- a/tests/expectations/tests/derive-partialeq-bitfield.rs +++ b/tests/expectations/tests/derive-partialeq-bitfield.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/divide-by-zero-in-struct-layout.rs b/tests/expectations/tests/divide-by-zero-in-struct-layout.rs index a85c3930d4..fcfa962bdc 100644 --- a/tests/expectations/tests/divide-by-zero-in-struct-layout.rs +++ b/tests/expectations/tests/divide-by-zero-in-struct-layout.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/issue-1034.rs b/tests/expectations/tests/issue-1034.rs index db9ce77c47..4db46afe0d 100644 --- a/tests/expectations/tests/issue-1034.rs +++ b/tests/expectations/tests/issue-1034.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/issue-1076-unnamed-bitfield-alignment.rs b/tests/expectations/tests/issue-1076-unnamed-bitfield-alignment.rs index 6ec97f96b5..eef609a28d 100644 --- a/tests/expectations/tests/issue-1076-unnamed-bitfield-alignment.rs +++ b/tests/expectations/tests/issue-1076-unnamed-bitfield-alignment.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/issue-739-pointer-wide-bitfield.rs b/tests/expectations/tests/issue-739-pointer-wide-bitfield.rs index aae1cffa34..213b6812da 100644 --- a/tests/expectations/tests/issue-739-pointer-wide-bitfield.rs +++ b/tests/expectations/tests/issue-739-pointer-wide-bitfield.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/issue-816.rs b/tests/expectations/tests/issue-816.rs index 9fbfd9b399..e55bbb8ca0 100644 --- a/tests/expectations/tests/issue-816.rs +++ b/tests/expectations/tests/issue-816.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/jsval_layout_opaque.rs b/tests/expectations/tests/jsval_layout_opaque.rs index 491a6500ce..a2e8044748 100644 --- a/tests/expectations/tests/jsval_layout_opaque.rs +++ b/tests/expectations/tests/jsval_layout_opaque.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/layout_align.rs b/tests/expectations/tests/layout_align.rs index a6e3b8b8cd..c57e97423a 100644 --- a/tests/expectations/tests/layout_align.rs +++ b/tests/expectations/tests/layout_align.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/layout_eth_conf.rs b/tests/expectations/tests/layout_eth_conf.rs index 3c2d5f9b44..d3b22e8122 100644 --- a/tests/expectations/tests/layout_eth_conf.rs +++ b/tests/expectations/tests/layout_eth_conf.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/layout_mbuf.rs b/tests/expectations/tests/layout_mbuf.rs index cbb70d7a98..6f95065dd9 100644 --- a/tests/expectations/tests/layout_mbuf.rs +++ b/tests/expectations/tests/layout_mbuf.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/only_bitfields.rs b/tests/expectations/tests/only_bitfields.rs index f8f2c5de71..b5db31affd 100644 --- a/tests/expectations/tests/only_bitfields.rs +++ b/tests/expectations/tests/only_bitfields.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/struct_with_bitfields.rs b/tests/expectations/tests/struct_with_bitfields.rs index aff22429dd..3f99eb0268 100644 --- a/tests/expectations/tests/struct_with_bitfields.rs +++ b/tests/expectations/tests/struct_with_bitfields.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/union_bitfield.rs b/tests/expectations/tests/union_bitfield.rs index 0afc927084..3bcd9ba461 100644 --- a/tests/expectations/tests/union_bitfield.rs +++ b/tests/expectations/tests/union_bitfield.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/union_with_anon_struct_bitfield.rs b/tests/expectations/tests/union_with_anon_struct_bitfield.rs index c451219659..d8aaf22191 100644 --- a/tests/expectations/tests/union_with_anon_struct_bitfield.rs +++ b/tests/expectations/tests/union_with_anon_struct_bitfield.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } } diff --git a/tests/expectations/tests/weird_bitfields.rs b/tests/expectations/tests/weird_bitfields.rs index 7c2cc1bcab..a91aae684f 100644 --- a/tests/expectations/tests/weird_bitfields.rs +++ b/tests/expectations/tests/weird_bitfields.rs @@ -15,7 +15,7 @@ pub struct __BindgenBitfieldUnit { } impl __BindgenBitfieldUnit { #[inline] - pub fn new(storage: Storage) -> Self { + pub const fn new(storage: Storage) -> Self { Self { storage, align: [] } } }