Skip to content

Commit 51f5110

Browse files
committed
add --edition option
1 parent 8830a03 commit 51f5110

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-32
lines changed

src/librustc/session/config.rs

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub use self::CrateType::*;
1616
pub use self::Passes::*;
1717
pub use self::DebugInfoLevel::*;
1818

19+
use std::str::FromStr;
20+
1921
use session::{early_error, early_warn, Session};
2022
use session::search_paths::SearchPaths;
2123

@@ -28,7 +30,7 @@ use middle::cstore;
2830

2931
use syntax::ast::{self, IntTy, UintTy};
3032
use syntax::codemap::{FileName, FilePathMapping};
31-
use syntax::edition::Edition;
33+
use syntax::edition::{Edition, ALL_EDITIONS, DEFAULT_EDITION};
3234
use syntax::parse::token;
3335
use syntax::parse;
3436
use syntax::symbol::Symbol;
@@ -410,6 +412,7 @@ top_level_options!(
410412

411413
// Remap source path prefixes in all output (messages, object files, debug, etc)
412414
remap_path_prefix: Vec<(PathBuf, PathBuf)> [UNTRACKED],
415+
edition: Edition [UNTRACKED],
413416
}
414417
);
415418

@@ -589,6 +592,7 @@ pub fn basic_options() -> Options {
589592
cli_forced_codegen_units: None,
590593
cli_forced_thinlto_off: false,
591594
remap_path_prefix: Vec::new(),
595+
edition: DEFAULT_EDITION,
592596
}
593597
}
594598

@@ -773,16 +777,13 @@ macro_rules! options {
773777
Some("`string` or `string=string`");
774778
pub const parse_lto: Option<&'static str> =
775779
Some("one of `thin`, `fat`, or omitted");
776-
pub const parse_edition: Option<&'static str> =
777-
Some("one of: `2015`, `2018`");
778780
}
779781

780782
#[allow(dead_code)]
781783
mod $mod_set {
782784
use super::{$struct_name, Passes, SomePasses, AllPasses, Sanitizer, Lto};
783785
use rustc_back::{LinkerFlavor, PanicStrategy, RelroLevel};
784786
use std::path::PathBuf;
785-
use syntax::edition::Edition;
786787

787788
$(
788789
pub fn $opt(cg: &mut $struct_name, v: Option<&str>) -> bool {
@@ -985,20 +986,6 @@ macro_rules! options {
985986
true
986987
}
987988

988-
fn parse_edition(slot: &mut Edition, v: Option<&str>) -> bool {
989-
match v {
990-
Some(s) => {
991-
let edition = s.parse();
992-
if let Ok(parsed) = edition {
993-
*slot = parsed;
994-
true
995-
} else {
996-
false
997-
}
998-
}
999-
_ => false,
1000-
}
1001-
}
1002989
}
1003990
) }
1004991

@@ -1292,10 +1279,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12921279
`everybody_loops` (all function bodies replaced with `loop {}`),
12931280
`hir` (the HIR), `hir,identified`, or
12941281
`hir,typed` (HIR with types for each node)."),
1295-
edition: Edition = (Edition::Edition2015, parse_edition, [TRACKED],
1296-
"The edition to build Rust with. Newer editions may include features
1297-
that require breaking changes. The default edition is 2015 (the first
1298-
edition). Crates compiled with different editions can be linked together."),
12991282
run_dsymutil: Option<bool> = (None, parse_opt_bool, [TRACKED],
13001283
"run `dsymutil` and delete intermediate object files"),
13011284
ui_testing: bool = (false, parse_bool, [UNTRACKED],
@@ -1656,6 +1639,12 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
16561639
`expanded,identified` (fully parenthesized, AST nodes with IDs).",
16571640
"TYPE",
16581641
),
1642+
opt::opt_s(
1643+
"",
1644+
"edition",
1645+
"Specify which edition of the compiler to use when compiling code.",
1646+
&edition_name_list(),
1647+
),
16591648
opt::multi_s(
16601649
"",
16611650
"remap-path-prefix",
@@ -1715,6 +1704,22 @@ pub fn build_session_options_and_crate_config(
17151704
),
17161705
};
17171706

1707+
let edition = match matches.opt_str("edition") {
1708+
Some(arg) => match Edition::from_str(&arg){
1709+
Ok(edition) => edition,
1710+
Err(_) => early_error(
1711+
ErrorOutputType::default(),
1712+
&format!(
1713+
"argument for --edition must be one of: \
1714+
{}. (instead was `{}`)",
1715+
edition_name_list(),
1716+
arg
1717+
),
1718+
),
1719+
}
1720+
None => DEFAULT_EDITION,
1721+
};
1722+
17181723
// We need the opts_present check because the driver will send us Matches
17191724
// with only stable options if no unstable options are used. Since error-format
17201725
// is unstable, it will not be present. We have to use opts_present not
@@ -2171,6 +2176,7 @@ pub fn build_session_options_and_crate_config(
21712176
cli_forced_codegen_units: codegen_units,
21722177
cli_forced_thinlto_off: disable_thinlto,
21732178
remap_path_prefix,
2179+
edition,
21742180
},
21752181
cfg,
21762182
)
@@ -2300,7 +2306,7 @@ mod dep_tracking {
23002306
use std::hash::Hash;
23012307
use std::path::PathBuf;
23022308
use std::collections::hash_map::DefaultHasher;
2303-
use super::{CrateType, DebugInfoLevel, Edition, ErrorOutputType, Lto, OptLevel, OutputTypes,
2309+
use super::{CrateType, DebugInfoLevel, ErrorOutputType, Lto, OptLevel, OutputTypes,
23042310
Passes, Sanitizer};
23052311
use syntax::feature_gate::UnstableFeatures;
23062312
use rustc_back::{PanicStrategy, RelroLevel};
@@ -2363,7 +2369,6 @@ mod dep_tracking {
23632369
impl_dep_tracking_hash_via_hash!(cstore::NativeLibraryKind);
23642370
impl_dep_tracking_hash_via_hash!(Sanitizer);
23652371
impl_dep_tracking_hash_via_hash!(Option<Sanitizer>);
2366-
impl_dep_tracking_hash_via_hash!(Edition);
23672372
impl_dep_tracking_hash_via_hash!(TargetTriple);
23682373

23692374
impl_dep_tracking_hash_for_sortable_vec_of!(String);
@@ -2422,6 +2427,11 @@ mod dep_tracking {
24222427
}
24232428
}
24242429

2430+
pub fn edition_name_list() -> String {
2431+
let names: Vec<String> = ALL_EDITIONS.iter().map(|e| format!("{}", e)).collect();
2432+
names.join("|")
2433+
}
2434+
24252435
#[cfg(test)]
24262436
mod tests {
24272437
use errors;
@@ -3081,4 +3091,17 @@ mod tests {
30813091
opts.debugging_opts.relro_level = Some(RelroLevel::Full);
30823092
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
30833093
}
3094+
3095+
#[test]
3096+
fn test_edition_parsing() {
3097+
// test default edition
3098+
let options = super::basic_options();
3099+
assert!(options.edition == Edition::DEFAULT_EDITION);
3100+
3101+
let matches = optgroups()
3102+
.parse(&["--edition=2018".to_string()])
3103+
.unwrap();
3104+
let (sessopts, _) = build_session_options_and_crate_config(&matches);
3105+
assert!(sessopts.edition == Edition::Edition2018)
3106+
}
30843107
}

src/librustc/session/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,11 +934,11 @@ impl Session {
934934

935935
/// Are we allowed to use features from the Rust 2018 edition?
936936
pub fn rust_2018(&self) -> bool {
937-
self.opts.debugging_opts.edition >= Edition::Edition2018
937+
self.opts.edition >= Edition::Edition2018
938938
}
939939

940940
pub fn edition(&self) -> Edition {
941-
self.opts.debugging_opts.edition
941+
self.opts.edition
942942
}
943943
}
944944

src/libsyntax/edition.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ pub enum Edition {
2727
// - the list in the `parse_edition` static in librustc::session::config
2828
// - add a `rust_####()` function to the session
2929
// - update the enum in Cargo's sources as well
30-
//
31-
// When -Zedition becomes --edition, there will
32-
// also be a check for the edition being nightly-only
33-
// somewhere. That will need to be updated
34-
// whenever we're stabilizing/introducing a new edition
35-
// as well as changing the default Cargo template.
3630
}
3731

3832
// must be in order from oldest to newest
3933
pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
4034

35+
pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
36+
4137
impl fmt::Display for Edition {
4238
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4339
let s = match *self {

0 commit comments

Comments
 (0)