Skip to main content

rustc_span/
edition.rs

1use std::fmt;
2use std::str::FromStr;
3
4use rustc_macros::{BlobDecodable, Encodable, StableHash};
5
6/// The edition of the compiler. (See [RFC 2052](https://rust-lang.github.io/rfcs/2052-epochs.html).)
7#[derive(#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for Edition { }
#[automatically_derived]
impl ::core::clone::Clone for Edition {
    #[inline]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Edition { }Copy, #[automatically_derived]
impl ::core::hash::Hash for Edition {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
            state)
    }
}Hash, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for Edition { }
#[automatically_derived]
impl ::core::cmp::PartialEq for Edition {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
            ::core::intrinsics::discriminant_value(other)
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::PartialOrd for Edition {
    #[inline]
    fn partial_cmp(&self, other: &Self)
        -> ::core::option::Option<::core::cmp::Ordering> {
        ::core::cmp::PartialOrd::partial_cmp(&::core::intrinsics::discriminant_value(self),
            &::core::intrinsics::discriminant_value(other))
    }
}PartialOrd, #[automatically_derived]
impl ::core::fmt::Debug for Edition {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                Edition::Edition2015 => "Edition2015",
                Edition::Edition2018 => "Edition2018",
                Edition::Edition2021 => "Edition2021",
                Edition::Edition2024 => "Edition2024",
                Edition::EditionFuture => "EditionFuture",
            })
    }
}Debug, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for Edition {
            fn encode(&self, __encoder: &mut __E) {
                let disc =
                    match *self {
                        Edition::Edition2015 => { 0usize }
                        Edition::Edition2018 => { 1usize }
                        Edition::Edition2021 => { 2usize }
                        Edition::Edition2024 => { 3usize }
                        Edition::EditionFuture => { 4usize }
                    };
                ::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::BlobDecoder> ::rustc_serialize::Decodable<__D>
            for Edition {
            fn decode(__decoder: &mut __D) -> Self {
                match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
                    {
                    0usize => { Edition::Edition2015 }
                    1usize => { Edition::Edition2018 }
                    2usize => { Edition::Edition2021 }
                    3usize => { Edition::Edition2024 }
                    4usize => { Edition::EditionFuture }
                    n => {
                        ::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Edition`, expected 0..5, actual {0}",
                                n));
                    }
                }
            }
        }
    };BlobDecodable, #[automatically_derived]
impl ::core::cmp::Eq for Edition { }Eq)]
8#[derive(const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for Edition {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    Edition::Edition2015 => {}
                    Edition::Edition2018 => {}
                    Edition::Edition2021 => {}
                    Edition::Edition2024 => {}
                    Edition::EditionFuture => {}
                }
            }
        }
    };StableHash)]
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
48/// All the valid editions that `--edition` will accept on nightly.
49pub const EDITION_NAME_LIST: &str = "<2015|2018|2021|2024|future>";
50/// All the valid editions on stable, which doesn't include `future`.
51pub const EDITION_NAME_LIST_STABLE: &str = "<2015|2018|2021|2024>";
52
53pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
54
55pub const LATEST_STABLE_EDITION: Edition = Edition::Edition2024;
56
57impl fmt::Display for Edition {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        let s = self.as_str();
60        f.write_fmt(format_args!("{0}", s))write!(f, "{s}")
61    }
62}
63
64impl Edition {
65    pub fn lint_name(self) -> &'static str {
66        match self {
67            Edition::Edition2015 => "rust_2015_compatibility",
68            Edition::Edition2018 => "rust_2018_compatibility",
69            Edition::Edition2021 => "rust_2021_compatibility",
70            Edition::Edition2024 => "rust_2024_compatibility",
71            Edition::EditionFuture => "edition_future_compatibility",
72        }
73    }
74
75    pub fn as_str(&self) -> &'static str {
76        match *self {
77            Edition::Edition2015 => "2015",
78            Edition::Edition2018 => "2018",
79            Edition::Edition2021 => "2021",
80            Edition::Edition2024 => "2024",
81            Edition::EditionFuture => "future",
82        }
83    }
84
85    pub fn is_stable(self) -> bool {
86        match self {
87            Edition::Edition2015 => true,
88            Edition::Edition2018 => true,
89            Edition::Edition2021 => true,
90            Edition::Edition2024 => true,
91            Edition::EditionFuture => false,
92        }
93    }
94
95    /// Is this edition 2015?
96    pub fn is_rust_2015(self) -> bool {
97        self == Edition::Edition2015
98    }
99
100    /// Are we allowed to use features from the Rust 2018 edition?
101    pub fn at_least_rust_2018(self) -> bool {
102        self >= Edition::Edition2018
103    }
104
105    /// Are we allowed to use features from the Rust 2021 edition?
106    pub fn at_least_rust_2021(self) -> bool {
107        self >= Edition::Edition2021
108    }
109
110    /// Are we allowed to use features from the Rust 2024 edition?
111    pub fn at_least_rust_2024(self) -> bool {
112        self >= Edition::Edition2024
113    }
114
115    /// Are we allowed to use features from the future edition?
116    pub fn at_least_edition_future(self) -> bool {
117        self >= Edition::EditionFuture
118    }
119}
120
121impl FromStr for Edition {
122    type Err = ();
123    fn from_str(s: &str) -> Result<Self, ()> {
124        match s {
125            "2015" => Ok(Edition::Edition2015),
126            "2018" => Ok(Edition::Edition2018),
127            "2021" => Ok(Edition::Edition2021),
128            "2024" => Ok(Edition::Edition2024),
129            "future" => Ok(Edition::EditionFuture),
130            _ => Err(()),
131        }
132    }
133}