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