From b4d00c49149b20adfb954a5e7494923953d85c9a Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 29 Nov 2024 14:51:58 -0500 Subject: [PATCH] Add edition default support --- bindgen/codegen/mod.rs | 10 +++------- bindgen/features.rs | 2 ++ bindgen/lib.rs | 2 +- bindgen/options/cli.rs | 10 ++-------- bindgen/options/mod.rs | 23 +++++++++++++++++++---- 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index b4c9ddfd9d..6c6f199884 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -728,7 +728,8 @@ impl CodeGenerator for Var { if let Some(cstr) = cstr { let cstr_ty = quote! { ::#prefix::ffi::CStr }; if rust_features.literal_cstr && - options.rust_edition >= RustEdition::Rust2021 + options.get_rust_edition() >= + RustEdition::Rust2021 { let cstr = proc_macro2::Literal::c_string(&cstr); result.push(quote! { @@ -3917,12 +3918,9 @@ impl std::str::FromStr for MacroTypeVariation { } /// Enum for the edition of Rust language to use. -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug, Default)] +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug)] pub enum RustEdition { - /// Rust 2015 language edition - Rust2015, /// Rust 2018 language edition - #[default] Rust2018, /// Rust 2021 language edition Rust2021, @@ -3933,7 +3931,6 @@ pub enum RustEdition { impl fmt::Display for RustEdition { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s = match self { - RustEdition::Rust2015 => "2015", RustEdition::Rust2018 => "2018", RustEdition::Rust2021 => "2021", RustEdition::Rust2024 => "2024", @@ -3948,7 +3945,6 @@ impl FromStr for RustEdition { /// Create a `RustEdition` from a string. fn from_str(s: &str) -> Result { match s { - "2015" => Ok(RustEdition::Rust2015), "2018" => Ok(RustEdition::Rust2018), "2021" => Ok(RustEdition::Rust2021), "2024" => Ok(RustEdition::Rust2024), diff --git a/bindgen/features.rs b/bindgen/features.rs index aa61ee9cc5..d734a63ea8 100644 --- a/bindgen/features.rs +++ b/bindgen/features.rs @@ -167,6 +167,7 @@ define_rust_targets! { Stable_1_71(71) => { c_unwind_abi: #106075 }, Stable_1_68(68) => { abi_efiapi: #105795 }, Stable_1_64(64) => { core_ffi_c: #94503 }, + Stable_1_56(56) => { edition_2021: #88100 }, Stable_1_51(51) => { raw_ref_macros: #80886 }, Stable_1_59(59) => { const_cstr: #54745 }, Stable_1_47(47) => { larger_arrays: #74060 }, @@ -174,6 +175,7 @@ define_rust_targets! { Stable_1_40(40) => { non_exhaustive: #44109 }, Stable_1_36(36) => { maybe_uninit: #60445 }, Stable_1_33(33) => { repr_packed_n: #57049 }, + // Stable_1_31(31) => { edition_2018: #54057 }, } /// Latest stable release of Rust that is supported by bindgen diff --git a/bindgen/lib.rs b/bindgen/lib.rs index 544eb1f769..88a93b712f 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -531,7 +531,7 @@ impl BindgenOptions { /// Update rust edition version pub fn set_rust_edition(&mut self, rust_edition: RustEdition) { - self.rust_edition = rust_edition; + self.rust_edition = Some(rust_edition); } /// Update rust target version diff --git a/bindgen/options/cli.rs b/bindgen/options/cli.rs index 4474c5d263..9ce5f9d804 100644 --- a/bindgen/options/cli.rs +++ b/bindgen/options/cli.rs @@ -19,13 +19,6 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use std::{fs::File, process::exit}; -fn rust_edition_help() -> String { - format!( - "Version of the Rust language edition. Defaults to {}.", - RustEdition::default() - ) -} - fn rust_target_help() -> String { format!( "Version of the Rust compiler to target. Any Rust version after {EARLIEST_STABLE_RUST} is supported. Defaults to {}.", @@ -339,7 +332,8 @@ struct BindgenCommand { /// Add a RAW_LINE of Rust code to a given module with name MODULE_NAME. #[arg(long, number_of_values = 2, value_names = ["MODULE_NAME", "RAW_LINE"])] module_raw_line: Vec, - #[arg(long, help = rust_edition_help())] + /// 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. + #[arg(long)] rust_edition: Option, #[arg(long, help = rust_target_help())] rust_target: Option, diff --git a/bindgen/options/mod.rs b/bindgen/options/mod.rs index 0439c385bd..9713b470de 100644 --- a/bindgen/options/mod.rs +++ b/bindgen/options/mod.rs @@ -1595,8 +1595,7 @@ options! { as_args: |value, args| (!value).as_args(args, "--no-prepend-enum-name"), }, /// Version of the Rust compiler to target. - rust_edition: RustEdition { - default: RustEdition::default(), + rust_edition: Option { methods: { /// Specify the Rust edition version. /// @@ -1607,8 +1606,10 @@ options! { } }, as_args: |rust_edition, args| { - args.push("--rust-edition".to_owned()); - args.push(rust_edition.to_string()); + if let Some(rust_edition) = rust_edition { + args.push("--rust-edition".to_owned()); + args.push(rust_edition.to_string()); + } }, }, /// Version of the Rust compiler to target. @@ -2168,3 +2169,17 @@ options! { as_args: "--clang-macro-fallback-build-dir", } } + +impl BindgenOptions { + /// Get default Rust edition, unless it is set by the user + pub fn get_rust_edition(&self) -> RustEdition { + self.rust_edition.unwrap_or_else(|| { + if !self.rust_features.edition_2021 { + RustEdition::Rust2018 + } else { + // For now, we default to 2018, but this might need to be rethought + RustEdition::Rust2018 + } + }) + } +}