Skip to content

Commit 04f5c07

Browse files
aplanasemilio
authored andcommitted
Drop 'static for pub const strings for rustc>1.17
Constant and static declaration have a 'static live time by default, that is already elided since 1.17. Clippy complains on this kind of strings that are present in the generated code. This patch remove the 'static live time for those strings when rustc > 1.17 via a new added RustFeature. Fix #1612 Signed-off-by: Alberto Planas <[email protected]>
1 parent 302b484 commit 04f5c07

File tree

9 files changed

+57
-14
lines changed

9 files changed

+57
-14
lines changed

src/codegen/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,21 @@ impl CodeGenerator for Var {
665665
match String::from_utf8(bytes.clone()) {
666666
Ok(string) => {
667667
let cstr = helpers::ast_ty::cstr_expr(string);
668-
result.push(quote! {
669-
#(#attrs)*
670-
pub const #canonical_ident : &'static #ty = #cstr ;
671-
});
668+
if ctx
669+
.options()
670+
.rust_features
671+
.static_lifetime_elision
672+
{
673+
result.push(quote! {
674+
#(#attrs)*
675+
pub const #canonical_ident : &#ty = #cstr ;
676+
});
677+
} else {
678+
result.push(quote! {
679+
#(#attrs)*
680+
pub const #canonical_ident : &'static #ty = #cstr ;
681+
});
682+
}
672683
}
673684
Err(..) => {
674685
let bytes = helpers::ast_ty::byte_array_expr(bytes);

src/features.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ macro_rules! rust_target_base {
8787
$x_macro!(
8888
/// Rust stable 1.0
8989
=> Stable_1_0 => 1.0;
90-
/// Rust stable 1.1
91-
=> Stable_1_1 => 1.1;
90+
/// Rust stable 1.17
91+
/// * Static lifetime elision ([RFC 1623](https://github.com/rust-lang/rfcs/blob/master/text/1623-static.md))
92+
=> Stable_1_17 => 1.17;
9293
/// Rust stable 1.19
9394
/// * Untagged unions ([RFC 1444](https://github.com/rust-lang/rfcs/blob/master/text/1444-union.md))
9495
=> Stable_1_19 => 1.19;
@@ -191,6 +192,9 @@ macro_rules! rust_feature_def {
191192
// documentation for the relevant variant in the rust_target_base macro
192193
// definition.
193194
rust_feature_def!(
195+
Stable_1_17 {
196+
=> static_lifetime_elision;
197+
}
194198
Stable_1_19 {
195199
=> untagged_union;
196200
}
@@ -249,7 +253,8 @@ mod test {
249253
fn target_features() {
250254
let f_1_0 = RustFeatures::from(RustTarget::Stable_1_0);
251255
assert!(
252-
!f_1_0.core_ffi_c_void &&
256+
!f_1_0.static_lifetime_elision &&
257+
!f_1_0.core_ffi_c_void &&
253258
!f_1_0.untagged_union &&
254259
!f_1_0.associated_const &&
255260
!f_1_0.builtin_clone_impls &&
@@ -258,7 +263,8 @@ mod test {
258263
);
259264
let f_1_21 = RustFeatures::from(RustTarget::Stable_1_21);
260265
assert!(
261-
!f_1_21.core_ffi_c_void &&
266+
f_1_21.static_lifetime_elision &&
267+
!f_1_21.core_ffi_c_void &&
262268
f_1_21.untagged_union &&
263269
f_1_21.associated_const &&
264270
f_1_21.builtin_clone_impls &&
@@ -267,7 +273,8 @@ mod test {
267273
);
268274
let f_nightly = RustFeatures::from(RustTarget::Nightly);
269275
assert!(
270-
f_nightly.core_ffi_c_void &&
276+
f_nightly.static_lifetime_elision &&
277+
f_nightly.core_ffi_c_void &&
271278
f_nightly.untagged_union &&
272279
f_nightly.associated_const &&
273280
f_nightly.builtin_clone_impls &&
@@ -286,6 +293,7 @@ mod test {
286293
#[test]
287294
fn str_to_target() {
288295
test_target("1.0", RustTarget::Stable_1_0);
296+
test_target("1.17", RustTarget::Stable_1_17);
289297
test_target("1.19", RustTarget::Stable_1_19);
290298
test_target("1.21", RustTarget::Stable_1_21);
291299
test_target("1.25", RustTarget::Stable_1_25);

tests/expectations/tests/libclang-3.9/constant-evaluate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ pub const BAZ: ::std::os::raw::c_longlong = 24;
2323
pub const fuzz: f64 = 51.0;
2424
pub const BAZZ: ::std::os::raw::c_char = 53;
2525
pub const WAT: ::std::os::raw::c_char = 0;
26-
pub const bytestring: &'static [u8; 4usize] = b"Foo\0";
26+
pub const bytestring: &[u8; 4usize] = b"Foo\0";
2727
pub const NOT_UTF8: [u8; 5usize] = [240u8, 40u8, 140u8, 40u8, 0u8];

tests/expectations/tests/libclang-4/constant-evaluate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ pub const BAZ: ::std::os::raw::c_longlong = 24;
2121
pub const fuzz: f64 = 51.0;
2222
pub const BAZZ: ::std::os::raw::c_char = 53;
2323
pub const WAT: ::std::os::raw::c_char = 0;
24-
pub const bytestring: &'static [u8; 4usize] = b"Foo\0";
24+
pub const bytestring: &[u8; 4usize] = b"Foo\0";
2525
pub const NOT_UTF8: [u8; 5usize] = [240u8, 40u8, 140u8, 40u8, 0u8];

tests/expectations/tests/libclang-5/constant-evaluate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ pub const BAZ: ::std::os::raw::c_longlong = 24;
2121
pub const fuzz: f64 = 51.0;
2222
pub const BAZZ: ::std::os::raw::c_char = 53;
2323
pub const WAT: ::std::os::raw::c_char = 0;
24-
pub const bytestring: &'static [u8; 4usize] = b"Foo\0";
24+
pub const bytestring: &[u8; 4usize] = b"Foo\0";
2525
pub const NOT_UTF8: [u8; 5usize] = [240u8, 40u8, 140u8, 40u8, 0u8];

tests/expectations/tests/libclang-9/constant-evaluate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ pub const BAZ: ::std::os::raw::c_longlong = 24;
2121
pub const fuzz: f64 = 51.0;
2222
pub const BAZZ: ::std::os::raw::c_char = 53;
2323
pub const WAT: ::std::os::raw::c_char = 0;
24-
pub const bytestring: &'static [u8; 4usize] = b"Foo\0";
24+
pub const bytestring: &[u8; 4usize] = b"Foo\0";
2525
pub const NOT_UTF8: [u8; 5usize] = [240u8, 40u8, 140u8, 40u8, 0u8];

tests/expectations/tests/macro_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
non_upper_case_globals
66
)]
77

8-
pub const foo: &'static [u8; 4usize] = b"bar\0";
8+
pub const foo: &[u8; 4usize] = b"bar\0";
99
pub const CHAR: u8 = 98u8;
1010
pub const CHARR: u8 = 0u8;
1111
pub const FLOAT: f64 = 5.09;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
pub const foo: &'static [u8; 4usize] = b"bar\0";
9+
pub const CHAR: u8 = 98u8;
10+
pub const CHARR: u8 = 0u8;
11+
pub const FLOAT: f64 = 5.09;
12+
pub const FLOAT_EXPR: f64 = 0.005;
13+
pub const LONG: u32 = 3;
14+
pub const INVALID_UTF8: [u8; 5usize] = [240u8, 40u8, 140u8, 40u8, 0u8];

tests/headers/macro_const_1_0.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// bindgen-flags: --rust-target 1.0
2+
3+
#define foo "bar"
4+
#define CHAR 'b'
5+
#define CHARR '\0'
6+
#define FLOAT 5.09f
7+
#define FLOAT_EXPR (5 / 1000.0f)
8+
#define LONG 3L
9+
10+
#define INVALID_UTF8 "\xf0\x28\x8c\x28"

0 commit comments

Comments
 (0)