Skip to content

Commit 314e3d6

Browse files
committed
Add rust language editions support
While not used in this PR, this will allow different code generation depending on the language target, e.g. for rust-lang#2996 Introduce a new `--rust-edition` parameter with allowed values 2015, 2018, 2021, and 2024.
1 parent d2e30fb commit 314e3d6

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

bindgen/codegen/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,6 +3901,53 @@ impl std::str::FromStr for MacroTypeVariation {
39013901
}
39023902
}
39033903

3904+
/// Enum for the edition of Rust language to use.
3905+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
3906+
pub enum RustEdition {
3907+
/// Rust 2015 language edition
3908+
Rust2015,
3909+
/// Rust 2018 language edition
3910+
#[default]
3911+
Rust2018,
3912+
/// Rust 2021 language edition
3913+
Rust2021,
3914+
/// Rust 2024 language edition
3915+
Rust2024,
3916+
}
3917+
3918+
impl fmt::Display for RustEdition {
3919+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3920+
match self {
3921+
Self::Rust2015 => "2015",
3922+
Self::Rust2018 => "2018",
3923+
Self::Rust2021 => "2021",
3924+
Self::Rust2024 => "2024",
3925+
}
3926+
.fmt(f)
3927+
}
3928+
}
3929+
3930+
impl FromStr for RustEdition {
3931+
type Err = std::io::Error;
3932+
3933+
/// Create a `RustEdition` from a string.
3934+
fn from_str(s: &str) -> Result<Self, Self::Err> {
3935+
match s {
3936+
"2015" => Ok(RustEdition::Rust2015),
3937+
"2018" => Ok(RustEdition::Rust2018),
3938+
"2021" => Ok(RustEdition::Rust2021),
3939+
"2024" => Ok(RustEdition::Rust2024),
3940+
_ => Err(std::io::Error::new(
3941+
std::io::ErrorKind::InvalidInput,
3942+
concat!(
3943+
"Got an invalid language edition. Accepted values ",
3944+
"are '2015', '2018', '2021', and '2024'"
3945+
),
3946+
)),
3947+
}
3948+
}
3949+
}
3950+
39043951
/// Enum for how aliases should be translated.
39053952
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
39063953
pub enum AliasVariation {

bindgen/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ use ir::item::Item;
6464
use options::BindgenOptions;
6565
use parse::ParseError;
6666

67+
use crate::codegen::RustEdition;
6768
use std::borrow::Cow;
6869
use std::collections::hash_map::Entry;
6970
use std::env;
@@ -528,6 +529,11 @@ impl BindgenOptions {
528529
}
529530
}
530531

532+
/// Update rust edition version
533+
pub fn set_rust_edition(&mut self, rust_edition: RustEdition) {
534+
self.rust_edition = rust_edition;
535+
}
536+
531537
/// Update rust target version
532538
pub fn set_rust_target(&mut self, rust_target: RustTarget) {
533539
self.rust_target = rust_target;
@@ -1291,6 +1297,8 @@ fn commandline_flag_unit_test_function() {
12911297
let test_cases = [
12921298
"--rust-target",
12931299
"input_header",
1300+
"--rust-edition",
1301+
"2021",
12941302
"--no-derive-default",
12951303
"--generate",
12961304
"functions,types,vars,methods,constructors,destructors",

bindgen/options/cli.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
regex_set::RegexSet,
88
Abi, AliasVariation, Builder, CodegenConfig, EnumVariation,
99
FieldVisibilityKind, Formatter, MacroTypeVariation, NonCopyUnionStyle,
10-
RustTarget,
10+
RustTarget, RustEdition,
1111
};
1212
use clap::{
1313
error::{Error, ErrorKind},
@@ -19,6 +19,13 @@ 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+
2229
fn rust_target_help() -> String {
2330
format!(
2431
"Version of the Rust compiler to target. Any Rust version after {EARLIEST_STABLE_RUST} is supported. Defaults to {}.",
@@ -332,6 +339,8 @@ struct BindgenCommand {
332339
/// Add a RAW_LINE of Rust code to a given module with name MODULE_NAME.
333340
#[arg(long, number_of_values = 2, value_names = ["MODULE_NAME", "RAW_LINE"])]
334341
module_raw_line: Vec<String>,
342+
#[arg(long, help = rust_edition_help())]
343+
rust_edition: Option<RustEdition>,
335344
#[arg(long, help = rust_target_help())]
336345
rust_target: Option<RustTarget>,
337346
/// Use types from Rust core instead of std.
@@ -587,6 +596,7 @@ where
587596
output,
588597
raw_line,
589598
module_raw_line,
599+
rust_edition,
590600
rust_target,
591601
use_core,
592602
conservative_inline_namespaces,
@@ -820,6 +830,7 @@ where
820830
exit(0)
821831
},
822832
header,
833+
rust_edition,
823834
rust_target,
824835
default_enum_style,
825836
bitfield_enum,

bindgen/options/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub(crate) mod cli;
1010
use crate::callbacks::ParseCallbacks;
1111
use crate::codegen::{
1212
AliasVariation, EnumVariation, MacroTypeVariation, NonCopyUnionStyle,
13+
RustEdition,
1314
};
1415
use crate::deps::DepfileSpec;
1516
use crate::features::{RustFeatures, RustTarget};
@@ -1594,6 +1595,23 @@ options! {
15941595
as_args: |value, args| (!value).as_args(args, "--no-prepend-enum-name"),
15951596
},
15961597
/// Version of the Rust compiler to target.
1598+
rust_edition: RustEdition {
1599+
default: RustEdition::default(),
1600+
methods: {
1601+
/// Specify the Rust edition version.
1602+
///
1603+
/// The default edition is 2018.
1604+
pub fn rust_edition(mut self, rust_edition: RustEdition) -> Self {
1605+
self.options.set_rust_edition(rust_edition);
1606+
self
1607+
}
1608+
},
1609+
as_args: |rust_edition, args| {
1610+
args.push("--rust-edition".to_owned());
1611+
args.push(rust_edition.to_string());
1612+
},
1613+
},
1614+
/// Version of the Rust compiler to target.
15971615
rust_target: RustTarget {
15981616
methods: {
15991617
/// Specify the Rust target version.

0 commit comments

Comments
 (0)