]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/edition.rs
New upstream version 1.27.2+dfsg1
[rustc.git] / src / libsyntax / edition.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::fmt;
12 use std::str::FromStr;
13
14 /// The edition of the compiler (RFC 2052)
15 #[derive(Clone, Copy, Hash, PartialOrd, Ord, Eq, PartialEq, Debug)]
16 #[non_exhaustive]
17 pub enum Edition {
18 // editions must be kept in order, newest to oldest
19
20 /// The 2015 edition
21 Edition2015,
22 /// The 2018 edition
23 Edition2018,
24
25 // when adding new editions, be sure to update:
26 //
27 // - Update the `ALL_EDITIONS` const
28 // - Update the EDITION_NAME_LIST const
29 // - add a `rust_####()` function to the session
30 // - update the enum in Cargo's sources as well
31 }
32
33 // must be in order from oldest to newest
34 pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
35
36 pub const EDITION_NAME_LIST: &'static str = "2015|2018";
37
38 pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
39
40 impl fmt::Display for Edition {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 let s = match *self {
43 Edition::Edition2015 => "2015",
44 Edition::Edition2018 => "2018",
45 };
46 write!(f, "{}", s)
47 }
48 }
49
50 impl Edition {
51 pub fn lint_name(&self) -> &'static str {
52 match *self {
53 Edition::Edition2015 => "rust_2015_breakage",
54 Edition::Edition2018 => "rust_2018_breakage",
55 }
56 }
57
58 pub fn feature_name(&self) -> &'static str {
59 match *self {
60 Edition::Edition2015 => "rust_2015_preview",
61 Edition::Edition2018 => "rust_2018_preview",
62 }
63 }
64
65 pub fn is_stable(&self) -> bool {
66 match *self {
67 Edition::Edition2015 => true,
68 Edition::Edition2018 => false,
69 }
70 }
71 }
72
73 impl FromStr for Edition {
74 type Err = ();
75 fn from_str(s: &str) -> Result<Self, ()> {
76 match s {
77 "2015" => Ok(Edition::Edition2015),
78 "2018" => Ok(Edition::Edition2018),
79 _ => Err(())
80 }
81 }
82 }