@@ -16,6 +16,8 @@ pub use self::CrateType::*;
16
16
pub use self :: Passes :: * ;
17
17
pub use self :: DebugInfoLevel :: * ;
18
18
19
+ use std:: str:: FromStr ;
20
+
19
21
use session:: { early_error, early_warn, Session } ;
20
22
use session:: search_paths:: SearchPaths ;
21
23
@@ -28,7 +30,7 @@ use middle::cstore;
28
30
29
31
use syntax:: ast:: { self , IntTy , UintTy } ;
30
32
use syntax:: codemap:: { FileName , FilePathMapping } ;
31
- use syntax:: edition:: Edition ;
33
+ use syntax:: edition:: { Edition , EDITION_NAME_LIST , DEFAULT_EDITION } ;
32
34
use syntax:: parse:: token;
33
35
use syntax:: parse;
34
36
use syntax:: symbol:: Symbol ;
@@ -410,6 +412,7 @@ top_level_options!(
410
412
411
413
// Remap source path prefixes in all output (messages, object files, debug, etc)
412
414
remap_path_prefix: Vec <( PathBuf , PathBuf ) > [ UNTRACKED ] ,
415
+ edition: Edition [ TRACKED ] ,
413
416
}
414
417
) ;
415
418
@@ -589,6 +592,7 @@ pub fn basic_options() -> Options {
589
592
cli_forced_codegen_units : None ,
590
593
cli_forced_thinlto_off : false ,
591
594
remap_path_prefix : Vec :: new ( ) ,
595
+ edition : DEFAULT_EDITION ,
592
596
}
593
597
}
594
598
@@ -773,16 +777,13 @@ macro_rules! options {
773
777
Some ( "`string` or `string=string`" ) ;
774
778
pub const parse_lto: Option <& ' static str > =
775
779
Some ( "one of `thin`, `fat`, or omitted" ) ;
776
- pub const parse_edition: Option <& ' static str > =
777
- Some ( "one of: `2015`, `2018`" ) ;
778
780
}
779
781
780
782
#[ allow( dead_code) ]
781
783
mod $mod_set {
782
784
use super :: { $struct_name, Passes , SomePasses , AllPasses , Sanitizer , Lto } ;
783
785
use rustc_back:: { LinkerFlavor , PanicStrategy , RelroLevel } ;
784
786
use std:: path:: PathBuf ;
785
- use syntax:: edition:: Edition ;
786
787
787
788
$(
788
789
pub fn $opt( cg: & mut $struct_name, v: Option <& str >) -> bool {
@@ -985,20 +986,6 @@ macro_rules! options {
985
986
true
986
987
}
987
988
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
- }
1002
989
}
1003
990
) }
1004
991
@@ -1292,10 +1279,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
1292
1279
`everybody_loops` (all function bodies replaced with `loop {}`),
1293
1280
`hir` (the HIR), `hir,identified`, or
1294
1281
`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." ) ,
1299
1282
run_dsymutil: Option <bool > = ( None , parse_opt_bool, [ TRACKED ] ,
1300
1283
"run `dsymutil` and delete intermediate object files" ) ,
1301
1284
ui_testing: bool = ( false , parse_bool, [ UNTRACKED ] ,
@@ -1656,6 +1639,12 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
1656
1639
`expanded,identified` (fully parenthesized, AST nodes with IDs)." ,
1657
1640
"TYPE" ,
1658
1641
) ,
1642
+ opt:: opt_s(
1643
+ "" ,
1644
+ "edition" ,
1645
+ "Specify which edition of the compiler to use when compiling code." ,
1646
+ EDITION_NAME_LIST ,
1647
+ ) ,
1659
1648
opt:: multi_s(
1660
1649
"" ,
1661
1650
"remap-path-prefix" ,
@@ -1715,6 +1704,34 @@ pub fn build_session_options_and_crate_config(
1715
1704
) ,
1716
1705
} ;
1717
1706
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
+
1718
1735
// We need the opts_present check because the driver will send us Matches
1719
1736
// with only stable options if no unstable options are used. Since error-format
1720
1737
// 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(
2171
2188
cli_forced_codegen_units : codegen_units,
2172
2189
cli_forced_thinlto_off : disable_thinlto,
2173
2190
remap_path_prefix,
2191
+ edition,
2174
2192
} ,
2175
2193
cfg,
2176
2194
)
@@ -2300,11 +2318,12 @@ mod dep_tracking {
2300
2318
use std:: hash:: Hash ;
2301
2319
use std:: path:: PathBuf ;
2302
2320
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 ,
2304
2322
Passes , Sanitizer } ;
2305
2323
use syntax:: feature_gate:: UnstableFeatures ;
2306
2324
use rustc_back:: { PanicStrategy , RelroLevel } ;
2307
2325
use rustc_back:: target:: TargetTriple ;
2326
+ use syntax:: edition:: Edition ;
2308
2327
2309
2328
pub trait DepTrackingHash {
2310
2329
fn hash ( & self , hasher : & mut DefaultHasher , error_format : ErrorOutputType ) ;
@@ -2363,8 +2382,8 @@ mod dep_tracking {
2363
2382
impl_dep_tracking_hash_via_hash ! ( cstore:: NativeLibraryKind ) ;
2364
2383
impl_dep_tracking_hash_via_hash ! ( Sanitizer ) ;
2365
2384
impl_dep_tracking_hash_via_hash ! ( Option <Sanitizer >) ;
2366
- impl_dep_tracking_hash_via_hash ! ( Edition ) ;
2367
2385
impl_dep_tracking_hash_via_hash ! ( TargetTriple ) ;
2386
+ impl_dep_tracking_hash_via_hash ! ( Edition ) ;
2368
2387
2369
2388
impl_dep_tracking_hash_for_sortable_vec_of ! ( String ) ;
2370
2389
impl_dep_tracking_hash_for_sortable_vec_of ! ( PathBuf ) ;
@@ -2437,6 +2456,7 @@ mod tests {
2437
2456
use super :: { Externs , OutputType , OutputTypes } ;
2438
2457
use rustc_back:: { PanicStrategy , RelroLevel } ;
2439
2458
use syntax:: symbol:: Symbol ;
2459
+ use syntax:: edition:: { Edition , DEFAULT_EDITION } ;
2440
2460
use syntax;
2441
2461
2442
2462
fn optgroups ( ) -> getopts:: Options {
@@ -3081,4 +3101,17 @@ mod tests {
3081
3101
opts. debugging_opts . relro_level = Some ( RelroLevel :: Full ) ;
3082
3102
assert ! ( reference. dep_tracking_hash( ) != opts. dep_tracking_hash( ) ) ;
3083
3103
}
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
+ }
3084
3117
}
0 commit comments