Skip to main content

compiletest/
edition.rs

1use std::str::FromStr;
2
3use crate::fatal;
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
6pub(crate) enum Edition {
7    // Note that the ordering here is load-bearing, as we want the future edition to be greater than
8    // any year-based edition.
9    Year(u32),
10    Future,
11}
12
13impl Edition {
14    pub(crate) const RUSTC_DEFAULT: Self = Self::Year(2015);
15    pub(crate) const LATEST_STABLE: Self = Self::Year(2024);
16
17    pub(crate) fn to_flag(self) -> String {
18        format!("--edition={self}")
19    }
20}
21
22impl std::fmt::Display for Edition {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            Edition::Year(year) => write!(f, "{year}"),
26            Edition::Future => f.write_str("future"),
27        }
28    }
29}
30
31impl From<u32> for Edition {
32    fn from(value: u32) -> Self {
33        Edition::Year(value)
34    }
35}
36
37impl FromStr for Edition {
38    type Err = String;
39
40    fn from_str(input: &str) -> Result<Self, Self::Err> {
41        let input = input.trim();
42        if input == "future" {
43            Ok(Edition::Future)
44        } else {
45            let year: u32 =
46                input.parse().map_err(|v| format!("{input} is not a valid edition year: {v}"))?;
47            Ok(Edition::Year(year))
48        }
49    }
50}
51
52pub(crate) fn parse_edition(input: &str) -> Edition {
53    input.parse().unwrap_or_else(|_| fatal!("`{input}` doesn't look like an edition"))
54}