rust/src/libsyntax/edition.rs

70 lines
2.1 KiB
Rust
Raw Normal View History

2018-03-06 22:05:03 +00:00
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-03-08 21:16:36 +00:00
use std::fmt;
2018-03-06 22:05:03 +00:00
use std::str::FromStr;
2018-03-15 03:30:06 +00:00
/// The edition of the compiler (RFC 2052)
2018-03-06 22:05:03 +00:00
#[derive(Clone, Copy, Hash, PartialOrd, Ord, Eq, PartialEq, Debug)]
#[non_exhaustive]
2018-03-15 03:30:06 +00:00
pub enum Edition {
// editions must be kept in order, newest to oldest
2018-03-06 22:05:03 +00:00
2018-03-15 03:30:06 +00:00
/// The 2015 edition
Edition2015,
/// The 2018 edition
Edition2018,
2018-03-06 22:05:03 +00:00
2018-03-15 03:30:06 +00:00
// when adding new editions, be sure to update:
2018-03-06 22:05:03 +00:00
//
2018-03-15 03:30:06 +00:00
// - the list in the `parse_edition` static in librustc::session::config
2018-03-06 22:05:03 +00:00
// - add a `rust_####()` function to the session
// - update the enum in Cargo's sources as well
//
2018-03-15 03:30:06 +00:00
// When -Zedition becomes --edition, there will
// also be a check for the edition being nightly-only
2018-03-06 22:05:03 +00:00
// somewhere. That will need to be updated
2018-03-15 03:30:06 +00:00
// whenever we're stabilizing/introducing a new edition
2018-03-06 22:05:03 +00:00
// as well as changing the default Cargo template.
}
// must be in order from oldest to newest
2018-03-15 03:30:06 +00:00
pub const ALL_EPOCHS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
2018-03-06 22:05:03 +00:00
2018-03-15 03:30:06 +00:00
impl fmt::Display for Edition {
2018-03-08 21:16:36 +00:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match *self {
2018-03-15 03:30:06 +00:00
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
2018-03-08 21:16:36 +00:00
};
write!(f, "{}", s)
2018-03-06 22:05:03 +00:00
}
}
2018-03-15 03:30:06 +00:00
impl Edition {
2018-03-06 22:05:03 +00:00
pub fn lint_name(&self) -> &'static str {
match *self {
2018-03-15 03:30:06 +00:00
Edition::Edition2015 => "edition_2015",
Edition::Edition2018 => "edition_2018",
2018-03-06 22:05:03 +00:00
}
}
}
2018-03-15 03:30:06 +00:00
impl FromStr for Edition {
2018-03-06 22:05:03 +00:00
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
match s {
2018-03-15 03:30:06 +00:00
"2015" => Ok(Edition::Edition2015),
"2018" => Ok(Edition::Edition2018),
2018-03-06 22:05:03 +00:00
_ => Err(())
}
}
}