Skip to content

Commit 9af69fe

Browse files
committed
Auto merge of #50080 - klnusbaum:edition_49591, r=Manishearth
add --edition option This adds an official `edition` flag to the rust compiler
2 parents b78853b + c8c9bf9 commit 9af69fe

File tree

8 files changed

+78
-39
lines changed

8 files changed

+78
-39
lines changed

src/librustc/session/config.rs

+57-24
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, EDITION_NAME_LIST, 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 [TRACKED],
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,34 @@ 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+
1723+
if !edition.is_stable() && !nightly_options::is_nightly_build() {
1724+
early_error(
1725+
ErrorOutputType::default(),
1726+
&format!(
1727+
"Edition {} is unstable an only\
1728+
available for nightly builds of rustc.",
1729+
edition,
1730+
)
1731+
)
1732+
}
1733+
1734+
17181735
// We need the opts_present check because the driver will send us Matches
17191736
// with only stable options if no unstable options are used. Since error-format
17201737
// is unstable, it will not be present. We have to use opts_present not
@@ -2171,6 +2188,7 @@ pub fn build_session_options_and_crate_config(
21712188
cli_forced_codegen_units: codegen_units,
21722189
cli_forced_thinlto_off: disable_thinlto,
21732190
remap_path_prefix,
2191+
edition,
21742192
},
21752193
cfg,
21762194
)
@@ -2300,11 +2318,12 @@ mod dep_tracking {
23002318
use std::hash::Hash;
23012319
use std::path::PathBuf;
23022320
use std::collections::hash_map::DefaultHasher;
2303-
use super::{CrateType, DebugInfoLevel, Edition, ErrorOutputType, Lto, OptLevel, OutputTypes,
2321+
use super::{CrateType, DebugInfoLevel, ErrorOutputType, Lto, OptLevel, OutputTypes,
23042322
Passes, Sanitizer};
23052323
use syntax::feature_gate::UnstableFeatures;
23062324
use rustc_back::{PanicStrategy, RelroLevel};
23072325
use rustc_back::target::TargetTriple;
2326+
use syntax::edition::Edition;
23082327

23092328
pub trait DepTrackingHash {
23102329
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType);
@@ -2363,8 +2382,8 @@ mod dep_tracking {
23632382
impl_dep_tracking_hash_via_hash!(cstore::NativeLibraryKind);
23642383
impl_dep_tracking_hash_via_hash!(Sanitizer);
23652384
impl_dep_tracking_hash_via_hash!(Option<Sanitizer>);
2366-
impl_dep_tracking_hash_via_hash!(Edition);
23672385
impl_dep_tracking_hash_via_hash!(TargetTriple);
2386+
impl_dep_tracking_hash_via_hash!(Edition);
23682387

23692388
impl_dep_tracking_hash_for_sortable_vec_of!(String);
23702389
impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf);
@@ -2437,6 +2456,7 @@ mod tests {
24372456
use super::{Externs, OutputType, OutputTypes};
24382457
use rustc_back::{PanicStrategy, RelroLevel};
24392458
use syntax::symbol::Symbol;
2459+
use syntax::edition::{Edition, DEFAULT_EDITION};
24402460
use syntax;
24412461

24422462
fn optgroups() -> getopts::Options {
@@ -3081,4 +3101,17 @@ mod tests {
30813101
opts.debugging_opts.relro_level = Some(RelroLevel::Full);
30823102
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
30833103
}
3104+
3105+
#[test]
3106+
fn test_edition_parsing() {
3107+
// test default edition
3108+
let options = super::basic_options();
3109+
assert!(options.edition == DEFAULT_EDITION);
3110+
3111+
let matches = optgroups()
3112+
.parse(&["--edition=2018".to_string()])
3113+
.unwrap();
3114+
let (sessopts, _) = build_session_options_and_crate_config(&matches);
3115+
assert!(sessopts.edition == Edition::Edition2018)
3116+
}
30843117
}

src/librustc/session/mod.rs

+2-2
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/librustc_driver/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ where
691691
krate,
692692
&sess.parse_sess,
693693
sess.opts.test,
694-
sess.opts.debugging_opts.edition,
694+
sess.edition(),
695695
);
696696
// these need to be set "early" so that expansion sees `quote` if enabled.
697697
sess.init_features(features);

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ pub fn run_core(search_paths: SearchPaths,
155155
actually_rustdoc: true,
156156
debugging_opts: config::DebuggingOptions {
157157
force_unstable_if_unmarked,
158-
edition,
159158
..config::basic_debugging_options()
160159
},
161160
error_format,
161+
edition,
162162
..config::basic_options().clone()
163163
};
164164

src/librustdoc/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ pub fn run(input_path: &Path,
8080
lint_cap: Some(::rustc::lint::Level::Allow),
8181
actually_rustdoc: true,
8282
debugging_opts: config::DebuggingOptions {
83-
edition,
8483
..config::basic_debugging_options()
8584
},
85+
edition,
8686
..config::basic_options().clone()
8787
};
8888

@@ -223,9 +223,9 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
223223
test: as_test_harness,
224224
unstable_features: UnstableFeatures::from_environment(),
225225
debugging_opts: config::DebuggingOptions {
226-
edition,
227226
..config::basic_debugging_options()
228227
},
228+
edition,
229229
..config::basic_options().clone()
230230
};
231231

src/libsyntax/edition.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ pub enum Edition {
2424

2525
// when adding new editions, be sure to update:
2626
//
27-
// - the list in the `parse_edition` static in librustc::session::config
27+
// - Update the `ALL_EDITIONS` const
28+
// - Update the EDITION_NAME_LIST const
2829
// - add a `rust_####()` function to the session
2930
// - 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.
3631
}
3732

3833
// must be in order from oldest to newest
3934
pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
4035

36+
pub const EDITION_NAME_LIST: &'static str = "2015|2018";
37+
38+
pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
39+
4140
impl fmt::Display for Edition {
4241
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4342
let s = match *self {
@@ -62,6 +61,13 @@ impl Edition {
6261
Edition::Edition2018 => "rust_2018_preview",
6362
}
6463
}
64+
65+
pub fn is_stable(&self) -> bool {
66+
match *self {
67+
Edition::Edition2015 => true,
68+
Edition::Edition2018 => false,
69+
}
70+
}
6571
}
6672

6773
impl FromStr for Edition {

src/test/compile-fail/edition-raw-pointer-method-2015.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
// ignore-tidy-linelength
12-
// compile-flags: -Zedition=2015 -Zunstable-options
12+
// compile-flags: --edition=2015 -Zunstable-options
1313

1414
// tests that editions work with the tyvar warning-turned-error
1515

src/test/compile-fail/edition-raw-pointer-method-2018.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
// ignore-tidy-linelength
12-
// compile-flags: -Zedition=2018 -Zunstable-options
12+
// compile-flags: --edition=2018 -Zunstable-options
1313

1414
// tests that editions work with the tyvar warning-turned-error
1515

0 commit comments

Comments
 (0)