Auto merge of #37793 - jseyfried:fix_proc_macro_def_ids, r=nrc
Fix proc macro def ids Update some `CStore` methods to also work correctly with proc macro def ids. Fixes #37788. r? @nrc
This commit is contained in:
commit
5bd1e7f59f
4 changed files with 79 additions and 11 deletions
|
@ -356,7 +356,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
|
||||||
fn load_macro(&self, id: DefId, sess: &Session) -> LoadedMacro {
|
fn load_macro(&self, id: DefId, sess: &Session) -> LoadedMacro {
|
||||||
let data = self.get_crate_data(id.krate);
|
let data = self.get_crate_data(id.krate);
|
||||||
if let Some(ref proc_macros) = data.proc_macros {
|
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);
|
let (name, def) = data.get_macro(id.index);
|
||||||
|
|
|
@ -23,7 +23,7 @@ use rustc::hir::intravisit::IdRange;
|
||||||
|
|
||||||
use rustc::middle::cstore::{DepKind, InlinedItem, LinkagePreference};
|
use rustc::middle::cstore::{DepKind, InlinedItem, LinkagePreference};
|
||||||
use rustc::hir::def::{self, Def, CtorKind};
|
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::middle::lang_items;
|
||||||
use rustc::ty::{self, Ty, TyCtxt};
|
use rustc::ty::{self, Ty, TyCtxt};
|
||||||
use rustc::ty::subst::Substs;
|
use rustc::ty::subst::Substs;
|
||||||
|
@ -513,7 +513,14 @@ impl<'a, 'tcx> CrateMetadata {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_def(&self, index: DefIndex) -> Option<Def> {
|
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,
|
pub fn get_trait_def(&self,
|
||||||
|
@ -643,15 +650,24 @@ impl<'a, 'tcx> CrateMetadata {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
|
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> {
|
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 {
|
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> {
|
fn get_impl_data(&self, id: DefIndex) -> ImplData<'tcx> {
|
||||||
|
@ -692,11 +708,11 @@ impl<'a, 'tcx> CrateMetadata {
|
||||||
where F: FnMut(def::Export)
|
where F: FnMut(def::Export)
|
||||||
{
|
{
|
||||||
if let Some(ref proc_macros) = self.proc_macros {
|
if let Some(ref proc_macros) = self.proc_macros {
|
||||||
for (id, &(name, _)) in proc_macros.iter().enumerate() {
|
if id == CRATE_DEF_INDEX {
|
||||||
callback(def::Export {
|
for (id, &(name, _)) in proc_macros.iter().enumerate() {
|
||||||
name: name,
|
let def = Def::Macro(DefId { krate: self.cnum, index: DefIndex::new(id + 1) });
|
||||||
def: Def::Macro(DefId { krate: self.cnum, index: DefIndex::new(id), }),
|
callback(def::Export { name: name, def: def });
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -894,6 +910,9 @@ impl<'a, 'tcx> CrateMetadata {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_item_attrs(&self, node_id: DefIndex) -> Vec<ast::Attribute> {
|
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;
|
// 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
|
// we assume that someone passing in a tuple struct ctor is actually wanting to
|
||||||
// look at the definition
|
// look at the definition
|
||||||
|
|
|
@ -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
src/test/compile-fail-fulldeps/proc-macro/issue-37788.rs
Normal file
21
src/test/compile-fail-fulldeps/proc-macro/issue-37788.rs
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue