Auto merge of #45605 - Nashenas88:derive-newtype, r=nikomatsakis

Add derive and doc comment capabilities to newtype_index macro

This moves `RustcDecodable` and `RustcEncodable` out of the macro definition and into the macro uses. They were conflicting with `CrateNum`'s impls of `serialize::UseSpecializedEncodable` and `serialize::UseSpecializedDecodable`, and now it's not :). `CrateNum` is now defined with the `newtype_index` macro. I also added support for doc comments on constant definitions and allowed a type to remove the pub specification on the tuple param (otherwise a LOT of code would refuse to compile for `CrateNum`). I was getting dozens of errors like this if `CrateNum` was defined as `pub struct CrateNum(pub u32)`:
```
error[E0530]: match bindings cannot shadow tuple structs
   --> src/librustc/dep_graph/dep_node.rs:624:25
    |
63  | use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
    |                   -------- a tuple struct `CrateNum` is imported here
...
624 |     [] MissingLangItems(CrateNum),
    |                         ^^^^^^^^ cannot be named the same as a tuple struct
```

I also cleaned up the formatting of the macro bodies as they were getting impossibly long. Should I go back and fix the matching rules to this style too?

I also want to see what the test results look like because `CrateNum` used to just derive `Debug`, but the `newtype_index` macro has a custom implementation. This might require further pushes.

Feel free to bikeshed on the macro language, I have no preference here.
This commit is contained in:
bors 2017-11-04 10:24:20 +00:00
commit a6885cb853
4 changed files with 252 additions and 53 deletions

View file

@ -16,30 +16,23 @@ use serialize::{self, Encoder, Decoder};
use std::fmt;
use std::u32;
#[derive(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Debug)]
pub struct CrateNum(u32);
newtype_index!(CrateNum
{
derive[Debug]
ENCODABLE = custom
impl Idx for CrateNum {
fn new(value: usize) -> Self {
assert!(value < (u32::MAX) as usize);
CrateNum(value as u32)
}
/// Item definitions in the currently-compiled crate would have the CrateNum
/// LOCAL_CRATE in their DefId.
const LOCAL_CRATE = 0,
fn index(self) -> usize {
self.0 as usize
}
}
/// Virtual crate for builtin macros
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
// `CrateNum`s.
const BUILTIN_MACROS_CRATE = u32::MAX,
/// Item definitions in the currently-compiled crate would have the CrateNum
/// LOCAL_CRATE in their DefId.
pub const LOCAL_CRATE: CrateNum = CrateNum(0);
/// Virtual crate for builtin macros
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s.
pub const BUILTIN_MACROS_CRATE: CrateNum = CrateNum(u32::MAX);
/// A CrateNum value that indicates that something is wrong.
pub const INVALID_CRATE: CrateNum = CrateNum(u32::MAX - 1);
/// A CrateNum value that indicates that something is wrong.
const INVALID_CRATE = u32::MAX - 1,
});
impl CrateNum {
pub fn new(x: usize) -> CrateNum {

View file

@ -158,8 +158,8 @@ pub struct BlockRemainder {
newtype_index!(FirstStatementIndex
{
DEBUG_FORMAT = "{}",
MAX = SCOPE_DATA_REMAINDER_MAX,
pub idx
MAX = SCOPE_DATA_REMAINDER_MAX
});
impl From<ScopeData> for Scope {

View file

@ -1552,6 +1552,7 @@ pub struct Constant<'tcx> {
newtype_index!(Promoted { DEBUG_FORMAT = "promoted[{}]" });
#[derive(Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub enum Literal<'tcx> {
Value {