rust/src/libsyntax_pos/edition.rs

83 lines
2.4 KiB
Rust
Raw Normal View History

2018-03-06 14:05:03 -08: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 13:16:36 -08:00
use std::fmt;
2018-03-06 14:05:03 -08:00
use std::str::FromStr;
2018-03-14 20:30:06 -07:00
/// The edition of the compiler (RFC 2052)
#[derive(Clone, Copy, Hash, PartialEq, PartialOrd, Debug, RustcEncodable, RustcDecodable)]
2018-03-06 14:05:03 -08:00
#[non_exhaustive]
2018-03-14 20:30:06 -07:00
pub enum Edition {
2018-06-14 10:46:50 -07:00
// editions must be kept in order, oldest to newest
2018-03-06 14:05:03 -08:00
2018-03-14 20:30:06 -07:00
/// The 2015 edition
Edition2015,
/// The 2018 edition
Edition2018,
2018-03-06 14:05:03 -08:00
2018-03-14 20:30:06 -07:00
// when adding new editions, be sure to update:
2018-03-06 14:05:03 -08:00
//
// - Update the `ALL_EDITIONS` const
// - Update the EDITION_NAME_LIST const
2018-03-06 14:05:03 -08:00
// - add a `rust_####()` function to the session
// - update the enum in Cargo's sources as well
}
// must be in order from oldest to newest
pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
2018-03-06 14:05:03 -08:00
pub const EDITION_NAME_LIST: &'static str = "2015|2018";
2018-04-19 13:56:26 -07:00
pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
2018-03-14 20:30:06 -07:00
impl fmt::Display for Edition {
2018-03-08 13:16:36 -08:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match *self {
2018-03-14 20:30:06 -07:00
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
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 {
2018-03-06 14:05:03 -08:00
pub fn lint_name(&self) -> &'static str {
match *self {
Edition::Edition2015 => "rust_2015_compatibility",
Edition::Edition2018 => "rust_2018_compatibility",
2018-03-06 14:05:03 -08:00
}
}
2018-03-21 15:48:56 -07:00
pub fn feature_name(&self) -> &'static str {
match *self {
Edition::Edition2015 => "rust_2015_preview",
Edition::Edition2018 => "rust_2018_preview",
}
}
pub fn is_stable(&self) -> bool {
match *self {
Edition::Edition2015 => true,
2018-09-06 10:20:01 -06:00
Edition::Edition2018 => true,
}
}
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),
2018-03-06 14:05:03 -08:00
_ => Err(())
}
}
}