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    /// The future edition - this variant will always exist and features associated with this
27    /// edition can be moved to the next 20XX edition when it is established and it is confirmed
28    /// that those features will be part of that edition.
29    ///
30    /// This variant allows edition changes to be implemented before being assigned to a concrete
31    /// edition - primarily when there are two different unstable behaviours that need tested across
32    /// an edition boundary.
33    ///
34    /// This edition will be permanently unstable and any features associated with this edition
35    /// must also be behind a feature gate.
36    EditionFuture,
37}
38
39// Must be in order from oldest to newest.
40pub const ALL_EDITIONS: &[Edition] = &[
41    Edition::Edition2015,
42    Edition::Edition2018,
43    Edition::Edition2021,
44    Edition::Edition2024,
45    Edition::EditionFuture,
46];
47
48pub const EDITION_NAME_LIST: &str = "2015|2018|2021|2024";
49
50pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
51
52pub const LATEST_STABLE_EDITION: Edition = Edition::Edition2024;
53
54impl fmt::Display for Edition {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        let s = match *self {
57            Edition::Edition2015 => "2015",
58            Edition::Edition2018 => "2018",
59            Edition::Edition2021 => "2021",
60            Edition::Edition2024 => "2024",
61            Edition::EditionFuture => "future",
62        };
63        write!(f, "{s}")
64    }
65}
66
67impl Edition {
68    pub fn lint_name(self) -> &'static str {
69        match self {
70            Edition::Edition2015 => "rust_2015_compatibility",
71            Edition::Edition2018 => "rust_2018_compatibility",
72            Edition::Edition2021 => "rust_2021_compatibility",
73            Edition::Edition2024 => "rust_2024_compatibility",
74            Edition::EditionFuture => "edition_future_compatibility",
75        }
76    }
77
78    pub fn is_stable(self) -> bool {
79        match self {
80            Edition::Edition2015 => true,
81            Edition::Edition2018 => true,
82            Edition::Edition2021 => true,
83            Edition::Edition2024 => true,
84            Edition::EditionFuture => false,
85        }
86    }
87
88    /// Is this edition 2015?
89    pub fn is_rust_2015(self) -> bool {
90        self == Edition::Edition2015
91    }
92
93    /// Are we allowed to use features from the Rust 2018 edition?
94    pub fn at_least_rust_2018(self) -> bool {
95        self >= Edition::Edition2018
96    }
97
98    /// Are we allowed to use features from the Rust 2021 edition?
99    pub fn at_least_rust_2021(self) -> bool {
100        self >= Edition::Edition2021
101    }
102
103    /// Are we allowed to use features from the Rust 2024 edition?
104    pub fn at_least_rust_2024(self) -> bool {
105        self >= Edition::Edition2024
106    }
107
108    /// Are we allowed to use features from the future edition?
109    pub fn at_least_edition_future(self) -> bool {
110        self >= Edition::EditionFuture
111    }
112}
113
114impl FromStr for Edition {
115    type Err = ();
116    fn from_str(s: &str) -> Result<Self, ()> {
117        match s {
118            "2015" => Ok(Edition::Edition2015),
119            "2018" => Ok(Edition::Edition2018),
120            "2021" => Ok(Edition::Edition2021),
121            "2024" => Ok(Edition::Edition2024),
122            "future" => Ok(Edition::EditionFuture),
123            _ => Err(()),
124        }
125    }
126}