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