]> git.lizzy.rs Git - rust.git/blob - src/librustc_span/edition.rs
Rollup merge of #75485 - RalfJung:pin, r=nagisa
[rust.git] / src / librustc_span / 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 (RFC 2052)
8 #[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Debug, Encodable, Decodable, Eq)]
9 #[derive(HashStable_Generic)]
10 pub enum Edition {
11     // editions must be kept in order, oldest to newest
12     /// The 2015 edition
13     Edition2015,
14     /// The 2018 edition
15     Edition2018,
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 lint_name(&self) -> &'static str {
43         match *self {
44             Edition::Edition2015 => "rust_2015_compatibility",
45             Edition::Edition2018 => "rust_2018_compatibility",
46         }
47     }
48
49     pub fn feature_name(&self) -> Symbol {
50         match *self {
51             Edition::Edition2015 => sym::rust_2015_preview,
52             Edition::Edition2018 => sym::rust_2018_preview,
53         }
54     }
55
56     pub fn is_stable(&self) -> bool {
57         match *self {
58             Edition::Edition2015 => true,
59             Edition::Edition2018 => true,
60         }
61     }
62 }
63
64 impl FromStr for Edition {
65     type Err = ();
66     fn from_str(s: &str) -> Result<Self, ()> {
67         match s {
68             "2015" => Ok(Edition::Edition2015),
69             "2018" => Ok(Edition::Edition2018),
70             _ => Err(()),
71         }
72     }
73 }