Skip to content

Commit 5bd1e7f

Browse files
authored
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
2 parents 29181b3 + 0172e46 commit 5bd1e7f

File tree

4 files changed

+79
-11
lines changed

4 files changed

+79
-11
lines changed

src/librustc_metadata/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
356356
fn load_macro(&self, id: DefId, sess: &Session) -> LoadedMacro {
357357
let data = self.get_crate_data(id.krate);
358358
if let Some(ref proc_macros) = data.proc_macros {
359-
return LoadedMacro::ProcMacro(proc_macros[id.index.as_usize()].1.clone());
359+
return LoadedMacro::ProcMacro(proc_macros[id.index.as_usize() - 1].1.clone());
360360
}
361361

362362
let (name, def) = data.get_macro(id.index);

src/librustc_metadata/decoder.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc::hir::intravisit::IdRange;
2323

2424
use rustc::middle::cstore::{DepKind, InlinedItem, LinkagePreference};
2525
use rustc::hir::def::{self, Def, CtorKind};
26-
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
26+
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
2727
use rustc::middle::lang_items;
2828
use rustc::ty::{self, Ty, TyCtxt};
2929
use rustc::ty::subst::Substs;
@@ -513,7 +513,14 @@ impl<'a, 'tcx> CrateMetadata {
513513
}
514514

515515
pub fn get_def(&self, index: DefIndex) -> Option<Def> {
516-
self.entry(index).kind.to_def(self.local_def_id(index))
516+
if self.proc_macros.is_some() {
517+
Some(match index {
518+
CRATE_DEF_INDEX => Def::Mod(self.local_def_id(index)),
519+
_ => Def::Macro(self.local_def_id(index)),
520+
})
521+
} else {
522+
self.entry(index).kind.to_def(self.local_def_id(index))
523+
}
517524
}
518525

519526
pub fn get_trait_def(&self,
@@ -643,15 +650,24 @@ impl<'a, 'tcx> CrateMetadata {
643650
}
644651

645652
pub fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
646-
self.entry(id).stability.map(|stab| stab.decode(self))
653+
match self.proc_macros {
654+
Some(_) if id != CRATE_DEF_INDEX => None,
655+
_ => self.entry(id).stability.map(|stab| stab.decode(self)),
656+
}
647657
}
648658

649659
pub fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
650-
self.entry(id).deprecation.map(|depr| depr.decode(self))
660+
match self.proc_macros {
661+
Some(_) if id != CRATE_DEF_INDEX => None,
662+
_ => self.entry(id).deprecation.map(|depr| depr.decode(self)),
663+
}
651664
}
652665

653666
pub fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
654-
self.entry(id).visibility
667+
match self.proc_macros {
668+
Some(_) => ty::Visibility::Public,
669+
_ => self.entry(id).visibility,
670+
}
655671
}
656672

657673
fn get_impl_data(&self, id: DefIndex) -> ImplData<'tcx> {
@@ -692,11 +708,11 @@ impl<'a, 'tcx> CrateMetadata {
692708
where F: FnMut(def::Export)
693709
{
694710
if let Some(ref proc_macros) = self.proc_macros {
695-
for (id, &(name, _)) in proc_macros.iter().enumerate() {
696-
callback(def::Export {
697-
name: name,
698-
def: Def::Macro(DefId { krate: self.cnum, index: DefIndex::new(id), }),
699-
})
711+
if id == CRATE_DEF_INDEX {
712+
for (id, &(name, _)) in proc_macros.iter().enumerate() {
713+
let def = Def::Macro(DefId { krate: self.cnum, index: DefIndex::new(id + 1) });
714+
callback(def::Export { name: name, def: def });
715+
}
700716
}
701717
return
702718
}
@@ -894,6 +910,9 @@ impl<'a, 'tcx> CrateMetadata {
894910
}
895911

896912
pub fn get_item_attrs(&self, node_id: DefIndex) -> Vec<ast::Attribute> {
913+
if self.proc_macros.is_some() && node_id != CRATE_DEF_INDEX {
914+
return Vec::new();
915+
}
897916
// The attributes for a tuple struct are attached to the definition, not the ctor;
898917
// we assume that someone passing in a tuple struct ctor is actually wanting to
899918
// look at the definition
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// force-host
12+
// no-prefer-dynamic
13+
14+
#![feature(proc_macro, proc_macro_lib)]
15+
#![crate_type = "proc-macro"]
16+
17+
extern crate proc_macro;
18+
use proc_macro::TokenStream;
19+
20+
#[proc_macro_derive(A)]
21+
pub fn derive_a(_: TokenStream) -> TokenStream {
22+
"".parse().unwrap()
23+
}
24+
25+
#[proc_macro_derive(B)]
26+
pub fn derive_b(_: TokenStream) -> TokenStream {
27+
"".parse().unwrap()
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:derive-a-b.rs
12+
13+
#![feature(proc_macro)]
14+
15+
#[macro_use]
16+
extern crate derive_a_b;
17+
18+
fn main() {
19+
// Test that constructing the `visible_parent_map` (in `cstore_impl.rs`) does not ICE.
20+
std::cell::Cell::new(0) //~ ERROR mismatched types
21+
}

0 commit comments

Comments
 (0)