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