@@ -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 , ALL_EDITIONS , 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 [ UNTRACKED ] ,
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,22 @@ 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
+
1718
1723
// We need the opts_present check because the driver will send us Matches
1719
1724
// with only stable options if no unstable options are used. Since error-format
1720
1725
// 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(
2171
2176
cli_forced_codegen_units : codegen_units,
2172
2177
cli_forced_thinlto_off : disable_thinlto,
2173
2178
remap_path_prefix,
2179
+ edition,
2174
2180
} ,
2175
2181
cfg,
2176
2182
)
@@ -2300,7 +2306,7 @@ mod dep_tracking {
2300
2306
use std:: hash:: Hash ;
2301
2307
use std:: path:: PathBuf ;
2302
2308
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 ,
2304
2310
Passes , Sanitizer } ;
2305
2311
use syntax:: feature_gate:: UnstableFeatures ;
2306
2312
use rustc_back:: { PanicStrategy , RelroLevel } ;
@@ -2363,7 +2369,6 @@ mod dep_tracking {
2363
2369
impl_dep_tracking_hash_via_hash ! ( cstore:: NativeLibraryKind ) ;
2364
2370
impl_dep_tracking_hash_via_hash ! ( Sanitizer ) ;
2365
2371
impl_dep_tracking_hash_via_hash ! ( Option <Sanitizer >) ;
2366
- impl_dep_tracking_hash_via_hash ! ( Edition ) ;
2367
2372
impl_dep_tracking_hash_via_hash ! ( TargetTriple ) ;
2368
2373
2369
2374
impl_dep_tracking_hash_for_sortable_vec_of ! ( String ) ;
@@ -2422,6 +2427,11 @@ mod dep_tracking {
2422
2427
}
2423
2428
}
2424
2429
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
+
2425
2435
#[ cfg( test) ]
2426
2436
mod tests {
2427
2437
use errors;
@@ -3081,4 +3091,17 @@ mod tests {
3081
3091
opts. debugging_opts . relro_level = Some ( RelroLevel :: Full ) ;
3082
3092
assert ! ( reference. dep_tracking_hash( ) != opts. dep_tracking_hash( ) ) ;
3083
3093
}
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
+ }
3084
3107
}
0 commit comments