@@ -16,6 +16,8 @@ pub use self::CrateType::*;
1616pub use self :: Passes :: * ;
1717pub use self :: DebugInfoLevel :: * ;
1818
19+ use std:: str:: FromStr ;
20+
1921use session:: { early_error, early_warn, Session } ;
2022use session:: search_paths:: SearchPaths ;
2123
@@ -28,7 +30,7 @@ use middle::cstore;
2830
2931use syntax:: ast:: { self , IntTy , UintTy } ;
3032use syntax:: codemap:: { FileName , FilePathMapping } ;
31- use syntax:: edition:: Edition ;
33+ use syntax:: edition:: { Edition , EDITION_NAME_LIST , DEFAULT_EDITION } ;
3234use syntax:: parse:: token;
3335use syntax:: parse;
3436use 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}
0 commit comments