1 use crate::symbol::{sym, Symbol};
5 use rustc_macros::HashStable_Generic;
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)]
11 // When adding new editions, be sure to do the following:
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
18 // Editions *must* be kept in order, oldest to newest.
29 // Must be in order from oldest to newest.
30 pub const ALL_EDITIONS: &[Edition] =
31 &[Edition::Edition2015, Edition::Edition2018, Edition::Edition2021, Edition::Edition2024];
33 pub const EDITION_NAME_LIST: &str = "2015|2018|2021|2024";
35 pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
37 pub const LATEST_STABLE_EDITION: Edition = Edition::Edition2021;
39 impl fmt::Display for Edition {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 Edition::Edition2015 => "2015",
43 Edition::Edition2018 => "2018",
44 Edition::Edition2021 => "2021",
45 Edition::Edition2024 => "2024",
52 pub fn lint_name(&self) -> &'static str {
54 Edition::Edition2015 => "rust_2015_compatibility",
55 Edition::Edition2018 => "rust_2018_compatibility",
56 Edition::Edition2021 => "rust_2021_compatibility",
57 Edition::Edition2024 => "rust_2024_compatibility",
61 pub fn feature_name(&self) -> Symbol {
63 Edition::Edition2015 => sym::rust_2015_preview,
64 Edition::Edition2018 => sym::rust_2018_preview,
65 Edition::Edition2021 => sym::rust_2021_preview,
66 Edition::Edition2024 => sym::rust_2024_preview,
70 pub fn is_stable(&self) -> bool {
72 Edition::Edition2015 => true,
73 Edition::Edition2018 => true,
74 Edition::Edition2021 => true,
75 Edition::Edition2024 => false,
79 pub fn rust_2015(&self) -> bool {
80 *self == Edition::Edition2015
83 /// Are we allowed to use features from the Rust 2018 edition?
84 pub fn rust_2018(&self) -> bool {
85 *self >= Edition::Edition2018
88 /// Are we allowed to use features from the Rust 2021 edition?
89 pub fn rust_2021(&self) -> bool {
90 *self >= Edition::Edition2021
93 /// Are we allowed to use features from the Rust 2024 edition?
94 pub fn rust_2024(&self) -> bool {
95 *self >= Edition::Edition2024
99 impl FromStr for Edition {
101 fn from_str(s: &str) -> Result<Self, ()> {
103 "2015" => Ok(Edition::Edition2015),
104 "2018" => Ok(Edition::Edition2018),
105 "2021" => Ok(Edition::Edition2021),
106 "2024" => Ok(Edition::Edition2024),