rustc_span/
edition.rs

1use std::fmt;
2use std::str::FromStr;
3
4use rustc_macros::{Decodable, Encodable, HashStable_Generic};
5
6/// The edition of the compiler. (See [RFC 2052](https://github.com/rust-lang/rfcs/blob/master/text/2052-epochs.md).)
7#[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Debug, Encodable, Decodable, Eq)]
8#[derive(HashStable_Generic)]
9pub enum Edition {
10    // When adding new editions, be sure to do the following:
11    //
12    // - update the `ALL_EDITIONS` const
13    // - update the `EDITION_NAME_LIST` const
14    // - add a `rust_####()` function to the session
15    // - update the enum in Cargo's sources as well
16    //
17    // Editions *must* be kept in order, oldest to newest.
18    /// The 2015 edition
19    Edition2015,
20    /// The 2018 edition
21    Edition2018,
22    /// The 2021 edition
23    Edition2021,
24    /// The 2024 edition
25    Edition2024,
26}
27
28// Must be in order from oldest to newest.
29pub const ALL_EDITIONS: &[Edition] =
30    &[Edition::Edition2015, Edition::Edition2018, Edition::Edition2021, Edition::Edition2024];
31
32pub const EDITION_NAME_LIST: &str = "2015|2018|2021|2024";
33
34pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
35
36pub const LATEST_STABLE_EDITION: Edition = Edition::Edition2024;
37
38impl fmt::Display for Edition {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        let s = match *self {
41            Edition::Edition2015 => "2015",
42            Edition::Edition2018 => "2018",
43            Edition::Edition2021 => "2021",
44            Edition::Edition2024 => "2024",
45        };
46        write!(f, "{s}")
47    }
48}
49
50impl Edition {
51    pub fn lint_name(self) -> &'static str {
52        match self {
53            Edition::Edition2015 => "rust_2015_compatibility",
54            Edition::Edition2018 => "rust_2018_compatibility",
55            Edition::Edition2021 => "rust_2021_compatibility",
56            Edition::Edition2024 => "rust_2024_compatibility",
57        }
58    }
59
60    pub fn is_stable(self) -> bool {
61        match self {
62            Edition::Edition2015 => true,
63            Edition::Edition2018 => true,
64            Edition::Edition2021 => true,
65            Edition::Edition2024 => true,
66        }
67    }
68
69    /// Is this edition 2015?
70    pub fn is_rust_2015(self) -> bool {
71        self == Edition::Edition2015
72    }
73
74    /// Are we allowed to use features from the Rust 2018 edition?
75    pub fn at_least_rust_2018(self) -> bool {
76        self >= Edition::Edition2018
77    }
78
79    /// Are we allowed to use features from the Rust 2021 edition?
80    pub fn at_least_rust_2021(self) -> bool {
81        self >= Edition::Edition2021
82    }
83
84    /// Are we allowed to use features from the Rust 2024 edition?
85    pub fn at_least_rust_2024(self) -> bool {
86        self >= Edition::Edition2024
87    }
88}
89
90impl FromStr for Edition {
91    type Err = ();
92    fn from_str(s: &str) -> Result<Self, ()> {
93        match s {
94            "2015" => Ok(Edition::Edition2015),
95            "2018" => Ok(Edition::Edition2018),
96            "2021" => Ok(Edition::Edition2021),
97            "2024" => Ok(Edition::Edition2024),
98            _ => Err(()),
99        }
100    }
101}