Skip to content

Commit

Permalink
Add edition default support
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Nov 29, 2024
1 parent d347c45 commit b98eb08
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
9 changes: 2 additions & 7 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ 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! {
Expand Down Expand Up @@ -3917,12 +3917,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,
Expand All @@ -3933,7 +3930,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",
Expand All @@ -3948,7 +3944,6 @@ impl FromStr for RustEdition {
/// Create a `RustEdition` from a string.
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"2015" => Ok(RustEdition::Rust2015),
"2018" => Ok(RustEdition::Rust2018),
"2021" => Ok(RustEdition::Rust2021),
"2024" => Ok(RustEdition::Rust2024),
Expand Down
2 changes: 2 additions & 0 deletions bindgen/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ 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 },
Stable_1_43(43) => { associated_constants: #68952 },
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
Expand Down
2 changes: 1 addition & 1 deletion bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions bindgen/options/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}.",
Expand Down Expand Up @@ -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<String>,
#[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<RustEdition>,
#[arg(long, help = rust_target_help())]
rust_target: Option<RustTarget>,
Expand Down
25 changes: 21 additions & 4 deletions bindgen/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RustEdition> {
methods: {
/// Specify the Rust edition version.
///
Expand All @@ -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.
Expand Down Expand Up @@ -2168,3 +2169,19 @@ 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
}
}
)
}
}

0 comments on commit b98eb08

Please sign in to comment.