]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_pos/edition.rs
Rollup merge of #61420 - felixrabe:patch-2, r=dtolnay
[rust.git] / src / libsyntax_pos / edition.rs
1 use crate::symbol::{Symbol, sym};
2 use std::fmt;
3 use std::str::FromStr;
4 use crate::GLOBALS;
5
6 /// The edition of the compiler (RFC 2052)
7 #[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Debug, RustcEncodable, RustcDecodable, Eq)]
8 pub enum Edition {
9     // editions must be kept in order, oldest to newest
10
11     /// The 2015 edition
12     Edition2015,
13     /// The 2018 edition
14     Edition2018,
15
16     // when adding new editions, be sure to update:
17     //
18     // - Update the `ALL_EDITIONS` const
19     // - Update the EDITION_NAME_LIST const
20     // - add a `rust_####()` function to the session
21     // - update the enum in Cargo's sources as well
22 }
23
24 // must be in order from oldest to newest
25 pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
26
27 pub const EDITION_NAME_LIST: &str = "2015|2018";
28
29 pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
30
31 impl fmt::Display for Edition {
32     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33         let s = match *self {
34             Edition::Edition2015 => "2015",
35             Edition::Edition2018 => "2018",
36         };
37         write!(f, "{}", s)
38     }
39 }
40
41 impl Edition {
42     pub fn from_session() -> Edition {
43         GLOBALS.with(|globals| globals.edition)
44     }
45
46     pub fn lint_name(&self) -> &'static str {
47         match *self {
48             Edition::Edition2015 => "rust_2015_compatibility",
49             Edition::Edition2018 => "rust_2018_compatibility",
50         }
51     }
52
53     pub fn feature_name(&self) -> Symbol {
54         match *self {
55             Edition::Edition2015 => sym::rust_2015_preview,
56             Edition::Edition2018 => sym::rust_2018_preview,
57         }
58     }
59
60     pub fn is_stable(&self) -> bool {
61         match *self {
62             Edition::Edition2015 => true,
63             Edition::Edition2018 => true,
64         }
65     }
66 }
67
68 impl FromStr for Edition {
69     type Err = ();
70     fn from_str(s: &str) -> Result<Self, ()> {
71         match s {
72             "2015" => Ok(Edition::Edition2015),
73             "2018" => Ok(Edition::Edition2018),
74             _ => Err(())
75         }
76     }
77 }