Files
rust/compiler/rustc_span/src/edition.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
3.0 KiB
Rust
Raw Normal View History

2018-03-08 13:16:36 -08:00
use std::fmt;
2018-03-06 14:05:03 -08:00
use std::str::FromStr;
2019-11-09 22:25:30 +01:00
use rustc_macros::HashStable_Generic;
/// The edition of the compiler. (See [RFC 2052](https://github.com/rust-lang/rfcs/blob/master/text/2052-epochs.md).)
#[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Debug, Encodable, Decodable, Eq)]
#[derive(HashStable_Generic)]
2018-03-14 20:30:06 -07:00
pub enum Edition {
// When adding new editions, be sure to do the following:
//
// - update the `ALL_EDITIONS` const
// - update the `EDITION_NAME_LIST` const
// - add a `rust_####()` function to the session
// - update the enum in Cargo's sources as well
//
// Editions *must* be kept in order, oldest to newest.
2018-03-14 20:30:06 -07:00
/// The 2015 edition
Edition2015,
/// The 2018 edition
Edition2018,
2021-03-10 09:09:37 +08:00
/// The 2021 edition
2020-12-30 14:33:46 +01:00
Edition2021,
2022-02-28 18:13:24 -05:00
/// The 2024 edition
Edition2024,
2018-03-06 14:05:03 -08:00
}
// Must be in order from oldest to newest.
2020-12-30 14:33:46 +01:00
pub const ALL_EDITIONS: &[Edition] =
2022-02-28 18:13:24 -05:00
&[Edition::Edition2015, Edition::Edition2018, Edition::Edition2021, Edition::Edition2024];
2018-03-06 14:05:03 -08:00
2022-02-28 18:13:24 -05:00
pub const EDITION_NAME_LIST: &str = "2015|2018|2021|2024";
2018-04-19 13:56:26 -07:00
pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
2021-08-30 15:33:09 +02:00
pub const LATEST_STABLE_EDITION: Edition = Edition::Edition2021;
2020-12-30 14:33:46 +01:00
2018-03-14 20:30:06 -07:00
impl fmt::Display for Edition {
2019-02-04 03:42:27 +09:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2018-03-08 13:16:36 -08:00
let s = match *self {
2018-03-14 20:30:06 -07:00
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
2020-12-30 14:33:46 +01:00
Edition::Edition2021 => "2021",
2022-02-28 18:13:24 -05:00
Edition::Edition2024 => "2024",
2018-03-08 13:16:36 -08:00
};
write!(f, "{s}")
2018-03-06 14:05:03 -08:00
}
}
2018-03-14 20:30:06 -07:00
impl Edition {
2023-01-31 10:07:29 +00:00
pub fn lint_name(self) -> &'static str {
match self {
Edition::Edition2015 => "rust_2015_compatibility",
Edition::Edition2018 => "rust_2018_compatibility",
2020-12-30 14:33:46 +01:00
Edition::Edition2021 => "rust_2021_compatibility",
2022-02-28 18:13:24 -05:00
Edition::Edition2024 => "rust_2024_compatibility",
2018-03-06 14:05:03 -08:00
}
}
2018-03-21 15:48:56 -07:00
2023-01-31 10:07:29 +00:00
pub fn is_stable(self) -> bool {
match self {
Edition::Edition2015 => true,
2018-09-06 10:20:01 -06:00
Edition::Edition2018 => true,
2021-08-17 09:42:23 +08:00
Edition::Edition2021 => true,
2022-02-28 18:13:24 -05:00
Edition::Edition2024 => false,
}
}
2022-02-28 18:13:24 -05:00
2023-01-31 09:52:54 +00:00
/// Is this edition 2015?
2023-02-01 10:42:20 +00:00
pub fn is_rust_2015(self) -> bool {
2023-01-31 10:07:29 +00:00
self == Edition::Edition2015
2022-02-28 18:13:24 -05:00
}
/// Are we allowed to use features from the Rust 2018 edition?
pub fn at_least_rust_2018(self) -> bool {
2023-01-31 10:07:29 +00:00
self >= Edition::Edition2018
2022-02-28 18:13:24 -05:00
}
/// Are we allowed to use features from the Rust 2021 edition?
pub fn at_least_rust_2021(self) -> bool {
2023-01-31 10:07:29 +00:00
self >= Edition::Edition2021
2022-02-28 18:13:24 -05:00
}
/// Are we allowed to use features from the Rust 2024 edition?
pub fn at_least_rust_2024(self) -> bool {
2023-01-31 10:07:29 +00:00
self >= Edition::Edition2024
2022-02-28 18:13:24 -05:00
}
2018-03-06 14:05:03 -08:00
}
2018-03-14 20:30:06 -07:00
impl FromStr for Edition {
2018-03-06 14:05:03 -08:00
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
match s {
2018-03-14 20:30:06 -07:00
"2015" => Ok(Edition::Edition2015),
"2018" => Ok(Edition::Edition2018),
2020-12-30 14:33:46 +01:00
"2021" => Ok(Edition::Edition2021),
2022-02-28 18:13:24 -05:00
"2024" => Ok(Edition::Edition2024),
2018-03-06 14:05:03 -08:00
_ => Err(()),
}
}
}