]> git.proxmox.com Git - rustc.git/blob - src/librustc_span/edition.rs
New upstream version 1.42.0+dfsg0+pve1
[rustc.git] / src / librustc_span / edition.rs
1 use crate::symbol::{sym, Symbol};
2 use std::fmt;
3 use std::str::FromStr;
4
5 use rustc_macros::HashStable_Generic;
6
7 /// The edition of the compiler (RFC 2052)
8 #[derive(
9 Clone,
10 Copy,
11 Hash,
12 PartialEq,
13 PartialOrd,
14 Debug,
15 RustcEncodable,
16 RustcDecodable,
17 Eq,
18 HashStable_Generic
19 )]
20 pub enum Edition {
21 // editions must be kept in order, oldest to newest
22 /// The 2015 edition
23 Edition2015,
24 /// The 2018 edition
25 Edition2018,
26 // when adding new editions, be sure to update:
27 //
28 // - Update the `ALL_EDITIONS` const
29 // - Update the EDITION_NAME_LIST const
30 // - add a `rust_####()` function to the session
31 // - update the enum in Cargo's sources as well
32 }
33
34 // must be in order from oldest to newest
35 pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
36
37 pub const EDITION_NAME_LIST: &str = "2015|2018";
38
39 pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
40
41 impl fmt::Display for Edition {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 let s = match *self {
44 Edition::Edition2015 => "2015",
45 Edition::Edition2018 => "2018",
46 };
47 write!(f, "{}", s)
48 }
49 }
50
51 impl Edition {
52 pub fn lint_name(&self) -> &'static str {
53 match *self {
54 Edition::Edition2015 => "rust_2015_compatibility",
55 Edition::Edition2018 => "rust_2018_compatibility",
56 }
57 }
58
59 pub fn feature_name(&self) -> Symbol {
60 match *self {
61 Edition::Edition2015 => sym::rust_2015_preview,
62 Edition::Edition2018 => sym::rust_2018_preview,
63 }
64 }
65
66 pub fn is_stable(&self) -> bool {
67 match *self {
68 Edition::Edition2015 => true,
69 Edition::Edition2018 => true,
70 }
71 }
72 }
73
74 impl FromStr for Edition {
75 type Err = ();
76 fn from_str(s: &str) -> Result<Self, ()> {
77 match s {
78 "2015" => Ok(Edition::Edition2015),
79 "2018" => Ok(Edition::Edition2018),
80 _ => Err(()),
81 }
82 }
83 }