Skip to main content

rustc_span/
edition.rs

1use std::fmt;
2use std::str::FromStr;
3
4use rustc_macros::{BlobDecodable, 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(#[automatically_derived]
impl ::core::clone::Clone for Edition {
    #[inline]
    fn clone(&self) -> Edition { *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) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash, #[automatically_derived]
impl ::core::cmp::PartialEq for Edition {
    #[inline]
    fn eq(&self, other: &Edition) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::PartialOrd for Edition {
    #[inline]
    fn partial_cmp(&self, other: &Edition)
        -> ::core::option::Option<::core::cmp::Ordering> {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        ::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
    }
}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);
                match *self {
                    Edition::Edition2015 => {}
                    Edition::Edition2018 => {}
                    Edition::Edition2021 => {}
                    Edition::Edition2024 => {}
                    Edition::EditionFuture => {}
                }
            }
        }
    };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 {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {}
}Eq)]
8#[derive(const _: () =
    {
        impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
            for Edition where __CTX: crate::HashStableContext {
            #[inline]
            fn hash_stable(&self, __hcx: &mut __CTX,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    Edition::Edition2015 => {}
                    Edition::Edition2018 => {}
                    Edition::Edition2021 => {}
                    Edition::Edition2024 => {}
                    Edition::EditionFuture => {}
                }
            }
        }
    };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|future>";
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        f.write_fmt(format_args!("{0}", s))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}