]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use crate::symbol::{sym, Symbol}; |
0531ce1d XL |
2 | use std::fmt; |
3 | use std::str::FromStr; | |
4 | ||
60c5eb7d XL |
5 | use rustc_macros::HashStable_Generic; |
6 | ||
5869c6ff | 7 | /// The edition of the compiler. (See [RFC 2052](https://github.com/rust-lang/rfcs/blob/master/text/2052-epochs.md).) |
3dfed10e | 8 | #[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Debug, Encodable, Decodable, Eq)] |
ba9703b0 | 9 | #[derive(HashStable_Generic)] |
0531ce1d | 10 | pub enum Edition { |
5869c6ff XL |
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. | |
0531ce1d XL |
19 | /// The 2015 edition |
20 | Edition2015, | |
21 | /// The 2018 edition | |
22 | Edition2018, | |
6a06907d | 23 | /// The 2021 edition |
5869c6ff | 24 | Edition2021, |
04454e1e FG |
25 | /// The 2024 edition |
26 | Edition2024, | |
0531ce1d XL |
27 | } |
28 | ||
5869c6ff XL |
29 | // Must be in order from oldest to newest. |
30 | pub const ALL_EDITIONS: &[Edition] = | |
04454e1e | 31 | &[Edition::Edition2015, Edition::Edition2018, Edition::Edition2021, Edition::Edition2024]; |
0531ce1d | 32 | |
04454e1e | 33 | pub const EDITION_NAME_LIST: &str = "2015|2018|2021|2024"; |
83c7162d XL |
34 | |
35 | pub const DEFAULT_EDITION: Edition = Edition::Edition2015; | |
36 | ||
94222f64 | 37 | pub const LATEST_STABLE_EDITION: Edition = Edition::Edition2021; |
5869c6ff | 38 | |
0531ce1d | 39 | impl fmt::Display for Edition { |
9fa01778 | 40 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
0531ce1d XL |
41 | let s = match *self { |
42 | Edition::Edition2015 => "2015", | |
43 | Edition::Edition2018 => "2018", | |
5869c6ff | 44 | Edition::Edition2021 => "2021", |
04454e1e | 45 | Edition::Edition2024 => "2024", |
0531ce1d | 46 | }; |
9c376795 | 47 | write!(f, "{s}") |
0531ce1d XL |
48 | } |
49 | } | |
50 | ||
51 | impl Edition { | |
9ffffee4 FG |
52 | pub fn lint_name(self) -> &'static str { |
53 | match self { | |
94b46f34 XL |
54 | Edition::Edition2015 => "rust_2015_compatibility", |
55 | Edition::Edition2018 => "rust_2018_compatibility", | |
5869c6ff | 56 | Edition::Edition2021 => "rust_2021_compatibility", |
04454e1e | 57 | Edition::Edition2024 => "rust_2024_compatibility", |
83c7162d XL |
58 | } |
59 | } | |
60 | ||
9ffffee4 FG |
61 | pub fn feature_name(self) -> Symbol { |
62 | match self { | |
48663c56 XL |
63 | Edition::Edition2015 => sym::rust_2015_preview, |
64 | Edition::Edition2018 => sym::rust_2018_preview, | |
5869c6ff | 65 | Edition::Edition2021 => sym::rust_2021_preview, |
04454e1e | 66 | Edition::Edition2024 => sym::rust_2024_preview, |
83c7162d XL |
67 | } |
68 | } | |
69 | ||
9ffffee4 FG |
70 | pub fn is_stable(self) -> bool { |
71 | match self { | |
83c7162d | 72 | Edition::Edition2015 => true, |
0bf4aa26 | 73 | Edition::Edition2018 => true, |
94222f64 | 74 | Edition::Edition2021 => true, |
04454e1e | 75 | Edition::Edition2024 => false, |
0531ce1d XL |
76 | } |
77 | } | |
04454e1e | 78 | |
9ffffee4 FG |
79 | /// Is this edition 2015? |
80 | pub fn is_rust_2015(self) -> bool { | |
81 | self == Edition::Edition2015 | |
04454e1e FG |
82 | } |
83 | ||
84 | /// Are we allowed to use features from the Rust 2018 edition? | |
9ffffee4 FG |
85 | pub fn rust_2018(self) -> bool { |
86 | self >= Edition::Edition2018 | |
04454e1e FG |
87 | } |
88 | ||
89 | /// Are we allowed to use features from the Rust 2021 edition? | |
9ffffee4 FG |
90 | pub fn rust_2021(self) -> bool { |
91 | self >= Edition::Edition2021 | |
04454e1e FG |
92 | } |
93 | ||
94 | /// Are we allowed to use features from the Rust 2024 edition? | |
9ffffee4 FG |
95 | pub fn rust_2024(self) -> bool { |
96 | self >= Edition::Edition2024 | |
04454e1e | 97 | } |
0531ce1d XL |
98 | } |
99 | ||
100 | impl FromStr for Edition { | |
101 | type Err = (); | |
102 | fn from_str(s: &str) -> Result<Self, ()> { | |
103 | match s { | |
104 | "2015" => Ok(Edition::Edition2015), | |
105 | "2018" => Ok(Edition::Edition2018), | |
5869c6ff | 106 | "2021" => Ok(Edition::Edition2021), |
04454e1e | 107 | "2024" => Ok(Edition::Edition2024), |
dfeec247 | 108 | _ => Err(()), |
0531ce1d XL |
109 | } |
110 | } | |
111 | } |