Skip to content

Commit 7512caa

Browse files
committed
Add edition default support
1 parent d347c45 commit 7512caa

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

bindgen/codegen/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ impl CodeGenerator for Var {
728728
if let Some(cstr) = cstr {
729729
let cstr_ty = quote! { ::#prefix::ffi::CStr };
730730
if rust_features.literal_cstr &&
731-
options.rust_edition >= RustEdition::Rust2021
731+
options.get_rust_edition() >=
732+
RustEdition::Rust2021
732733
{
733734
let cstr = proc_macro2::Literal::c_string(&cstr);
734735
result.push(quote! {
@@ -3917,12 +3918,11 @@ impl std::str::FromStr for MacroTypeVariation {
39173918
}
39183919

39193920
/// Enum for the edition of Rust language to use.
3920-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug, Default)]
3921+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug)]
39213922
pub enum RustEdition {
39223923
/// Rust 2015 language edition
39233924
Rust2015,
39243925
/// Rust 2018 language edition
3925-
#[default]
39263926
Rust2018,
39273927
/// Rust 2021 language edition
39283928
Rust2021,

bindgen/features.rs

+2
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,15 @@ define_rust_targets! {
167167
Stable_1_71(71) => { c_unwind_abi: #106075 },
168168
Stable_1_68(68) => { abi_efiapi: #105795 },
169169
Stable_1_64(64) => { core_ffi_c: #94503 },
170+
Stable_1_56(56) => { edition_2021: #88100 },
170171
Stable_1_51(51) => { raw_ref_macros: #80886 },
171172
Stable_1_59(59) => { const_cstr: #54745 },
172173
Stable_1_47(47) => { larger_arrays: #74060 },
173174
Stable_1_43(43) => { associated_constants: #68952 },
174175
Stable_1_40(40) => { non_exhaustive: #44109 },
175176
Stable_1_36(36) => { maybe_uninit: #60445 },
176177
Stable_1_33(33) => { repr_packed_n: #57049 },
178+
// Stable_1_31(31) => { edition_2018: #54057 },
177179
}
178180

179181
/// Latest stable release of Rust that is supported by bindgen

bindgen/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ impl BindgenOptions {
531531

532532
/// Update rust edition version
533533
pub fn set_rust_edition(&mut self, rust_edition: RustEdition) {
534-
self.rust_edition = rust_edition;
534+
self.rust_edition = Some(rust_edition);
535535
}
536536

537537
/// Update rust target version

bindgen/options/cli.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ use std::path::{Path, PathBuf};
1919
use std::str::FromStr;
2020
use std::{fs::File, process::exit};
2121

22-
fn rust_edition_help() -> String {
23-
format!(
24-
"Version of the Rust language edition. Defaults to {}.",
25-
RustEdition::default()
26-
)
27-
}
28-
2922
fn rust_target_help() -> String {
3023
format!(
3124
"Version of the Rust compiler to target. Any Rust version after {EARLIEST_STABLE_RUST} is supported. Defaults to {}.",
@@ -339,7 +332,8 @@ struct BindgenCommand {
339332
/// Add a RAW_LINE of Rust code to a given module with name MODULE_NAME.
340333
#[arg(long, number_of_values = 2, value_names = ["MODULE_NAME", "RAW_LINE"])]
341334
module_raw_line: Vec<String>,
342-
#[arg(long, help = rust_edition_help())]
335+
/// Version of the Rust language edition. Defaults to 2018 if used from CLI, unless target version does not support it. Defaults to current crate's edition if used from API.
336+
#[arg(long)]
343337
rust_edition: Option<RustEdition>,
344338
#[arg(long, help = rust_target_help())]
345339
rust_target: Option<RustTarget>,

bindgen/options/mod.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1595,8 +1595,7 @@ options! {
15951595
as_args: |value, args| (!value).as_args(args, "--no-prepend-enum-name"),
15961596
},
15971597
/// Version of the Rust compiler to target.
1598-
rust_edition: RustEdition {
1599-
default: RustEdition::default(),
1598+
rust_edition: Option<RustEdition> {
16001599
methods: {
16011600
/// Specify the Rust edition version.
16021601
///
@@ -1607,8 +1606,10 @@ options! {
16071606
}
16081607
},
16091608
as_args: |rust_edition, args| {
1610-
args.push("--rust-edition".to_owned());
1611-
args.push(rust_edition.to_string());
1609+
if let Some(rust_edition) = rust_edition {
1610+
args.push("--rust-edition".to_owned());
1611+
args.push(rust_edition.to_string());
1612+
}
16121613
},
16131614
},
16141615
/// Version of the Rust compiler to target.
@@ -2168,3 +2169,17 @@ options! {
21682169
as_args: "--clang-macro-fallback-build-dir",
21692170
}
21702171
}
2172+
2173+
impl BindgenOptions {
2174+
/// Get default Rust edition, unless it is set by the user
2175+
pub fn get_rust_edition(&self) -> RustEdition {
2176+
self.rust_edition.unwrap_or_else(|| {
2177+
if !self.rust_features.edition_2021 {
2178+
RustEdition::Rust2018
2179+
} else {
2180+
// For now, we default to 2018, but this might need to be rethought
2181+
RustEdition::Rust2018
2182+
}
2183+
})
2184+
}
2185+
}

0 commit comments

Comments
 (0)