Skip to content

Fix proc macro def ids #37793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
fn load_macro(&self, id: DefId, sess: &Session) -> LoadedMacro {
let data = self.get_crate_data(id.krate);
if let Some(ref proc_macros) = data.proc_macros {
return LoadedMacro::ProcMacro(proc_macros[id.index.as_usize()].1.clone());
return LoadedMacro::ProcMacro(proc_macros[id.index.as_usize() - 1].1.clone());
}

let (name, def) = data.get_macro(id.index);
Expand Down
39 changes: 29 additions & 10 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use rustc::hir::intravisit::IdRange;

use rustc::middle::cstore::{DepKind, InlinedItem, LinkagePreference};
use rustc::hir::def::{self, Def, CtorKind};
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc::middle::lang_items;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
Expand Down Expand Up @@ -513,7 +513,14 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_def(&self, index: DefIndex) -> Option<Def> {
self.entry(index).kind.to_def(self.local_def_id(index))
if self.proc_macros.is_some() {
Some(match index {
CRATE_DEF_INDEX => Def::Mod(self.local_def_id(index)),
_ => Def::Macro(self.local_def_id(index)),
})
} else {
self.entry(index).kind.to_def(self.local_def_id(index))
}
}

pub fn get_trait_def(&self,
Expand Down Expand Up @@ -643,15 +650,24 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
self.entry(id).stability.map(|stab| stab.decode(self))
match self.proc_macros {
Some(_) if id != CRATE_DEF_INDEX => None,
_ => self.entry(id).stability.map(|stab| stab.decode(self)),
}
}

pub fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
self.entry(id).deprecation.map(|depr| depr.decode(self))
match self.proc_macros {
Some(_) if id != CRATE_DEF_INDEX => None,
_ => self.entry(id).deprecation.map(|depr| depr.decode(self)),
}
}

pub fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
self.entry(id).visibility
match self.proc_macros {
Some(_) => ty::Visibility::Public,
_ => self.entry(id).visibility,
}
}

fn get_impl_data(&self, id: DefIndex) -> ImplData<'tcx> {
Expand Down Expand Up @@ -692,11 +708,11 @@ impl<'a, 'tcx> CrateMetadata {
where F: FnMut(def::Export)
{
if let Some(ref proc_macros) = self.proc_macros {
for (id, &(name, _)) in proc_macros.iter().enumerate() {
callback(def::Export {
name: name,
def: Def::Macro(DefId { krate: self.cnum, index: DefIndex::new(id), }),
})
if id == CRATE_DEF_INDEX {
for (id, &(name, _)) in proc_macros.iter().enumerate() {
let def = Def::Macro(DefId { krate: self.cnum, index: DefIndex::new(id + 1) });
callback(def::Export { name: name, def: def });
}
}
return
}
Expand Down Expand Up @@ -894,6 +910,9 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_item_attrs(&self, node_id: DefIndex) -> Vec<ast::Attribute> {
if self.proc_macros.is_some() && node_id != CRATE_DEF_INDEX {
return Vec::new();
}
// The attributes for a tuple struct are attached to the definition, not the ctor;
// we assume that someone passing in a tuple struct ctor is actually wanting to
// look at the definition
Expand Down
28 changes: 28 additions & 0 deletions src/test/compile-fail-fulldeps/proc-macro/auxiliary/derive-a-b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2016 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.

// force-host
// no-prefer-dynamic

#![feature(proc_macro, proc_macro_lib)]
#![crate_type = "proc-macro"]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_derive(A)]
pub fn derive_a(_: TokenStream) -> TokenStream {
"".parse().unwrap()
}

#[proc_macro_derive(B)]
pub fn derive_b(_: TokenStream) -> TokenStream {
"".parse().unwrap()
}
21 changes: 21 additions & 0 deletions src/test/compile-fail-fulldeps/proc-macro/issue-37788.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2016 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.

// aux-build:derive-a-b.rs

#![feature(proc_macro)]

#[macro_use]
extern crate derive_a_b;

fn main() {
// Test that constructing the `visible_parent_map` (in `cstore_impl.rs`) does not ICE.
std::cell::Cell::new(0) //~ ERROR mismatched types
}