Make calling def_id on a DefSelfTy an error; the previous defids that

were returned, either the trait or the *self type itself*, were not
particularly representative of what the Def is (a type parameter).
Rewrite paths to handle this case specially, just as they handle the
primitive case specifically. This entire `def_id` codepath is kind of a
mess.
This commit is contained in:
Niko Matsakis 2015-09-08 10:08:30 -04:00
parent 95ce1ebe7c
commit 3b1399df2d
7 changed files with 17 additions and 9 deletions

View File

@ -99,6 +99,7 @@ fn lookup_and_handle_definition(&mut self, id: &ast::NodeId) {
}
_ if self.ignore_non_const_paths => (),
def::DefPrimTy(_) => (),
def::DefSelfTy(..) => (),
def::DefVariant(enum_id, variant_id, _) => {
self.check_def_id(enum_id);
if !self.ignore_variant_stack.contains(&variant_id) {

View File

@ -135,14 +135,12 @@ pub fn def_id(&self) -> DefId {
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
DefMethod(id) | DefConst(id) | DefAssociatedConst(id) |
DefSelfTy(Some(id), None)=> {
DefMethod(id) | DefConst(id) | DefAssociatedConst(id) => {
id
}
DefLocal(id) |
DefUpvar(id, _, _) |
DefSelfTy(_, Some((_, id))) => {
DefUpvar(id, _, _) => {
DefId::xxx_local(id) // TODO, clearly
}

View File

@ -474,6 +474,7 @@ pub fn check_path(tcx: &ty::ctxt, path: &hir::Path, id: ast::NodeId,
cb: &mut FnMut(DefId, Span, &Option<&Stability>)) {
match tcx.def_map.borrow().get(&id).map(|d| d.full_def()) {
Some(def::DefPrimTy(..)) => {}
Some(def::DefSelfTy(..)) => {}
Some(def) => {
maybe_do_stability_check(tcx, def.def_id(), path.span, cb);
}

View File

@ -263,6 +263,7 @@ fn visit_item(&mut self, item: &hir::Item) {
hir::TyPath(..) => {
match self.tcx.def_map.borrow().get(&ty.id).unwrap().full_def() {
def::DefPrimTy(..) => true,
def::DefSelfTy(..) => true,
def => {
let did = def.def_id();
if let Some(node_id) = self.tcx.map.as_local_node_id(did) {
@ -337,7 +338,7 @@ fn visit_item(&mut self, item: &hir::Item) {
hir::ItemTy(ref ty, _) if public_first => {
if let hir::TyPath(..) = ty.node {
match self.tcx.def_map.borrow().get(&ty.id).unwrap().full_def() {
def::DefPrimTy(..) | def::DefTyParam(..) => {},
def::DefPrimTy(..) | def::DefSelfTy(..) | def::DefTyParam(..) => {},
def => {
let did = def.def_id();
if let Some(node_id) = self.tcx.map.as_local_node_id(did) {
@ -1148,7 +1149,7 @@ impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
fn path_is_private_type(&self, path_id: ast::NodeId) -> bool {
let did = match self.tcx.def_map.borrow().get(&path_id).map(|d| d.full_def()) {
// `int` etc. (None doesn't seem to occur.)
None | Some(def::DefPrimTy(..)) => return false,
None | Some(def::DefPrimTy(..)) | Some(def::DefSelfTy(..)) => return false,
Some(def) => def.def_id(),
};

View File

@ -235,7 +235,8 @@ fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
}
let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def();
match def {
def::DefPrimTy(_) => None,
def::DefPrimTy(..) => None,
def::DefSelfTy(..) => None,
_ => Some(def.def_id()),
}
}

View File

@ -637,7 +637,7 @@ fn lookup_ref_id(&self, ref_id: NodeId) -> Option<DefId> {
}
let def = self.tcx.def_map.borrow().get(&ref_id).unwrap().full_def();
match def {
def::DefPrimTy(_) => None,
def::DefPrimTy(_) | def::DefSelfTy(..) => None,
_ => Some(def.def_id()),
}
}

View File

@ -2570,17 +2570,19 @@ fn name_from_pat(p: &hir::Pat) -> String {
fn resolve_type(cx: &DocContext,
path: Path,
id: ast::NodeId) -> Type {
debug!("resolve_type({:?},{:?})", path, id);
let tcx = match cx.tcx_opt() {
Some(tcx) => tcx,
// If we're extracting tests, this return value doesn't matter.
None => return Primitive(Bool),
};
debug!("searching for {} in defmap", id);
let def = match tcx.def_map.borrow().get(&id) {
Some(k) => k.full_def(),
None => panic!("unresolved id not in defmap")
};
debug!("resolve_type: def={:?}", def);
let is_generic = match def {
def::DefPrimTy(p) => match p {
hir::TyStr => return Primitive(Str),
@ -2610,6 +2612,8 @@ fn resolve_type(cx: &DocContext,
}
fn register_def(cx: &DocContext, def: def::Def) -> DefId {
debug!("register_def({:?})", def);
let (did, kind) = match def {
def::DefFn(i, _) => (i, TypeFunction),
def::DefTy(i, false) => (i, TypeTypedef),
@ -2619,6 +2623,8 @@ fn register_def(cx: &DocContext, def: def::Def) -> DefId {
def::DefMod(i) => (i, TypeModule),
def::DefStatic(i, _) => (i, TypeStatic),
def::DefVariant(i, _, _) => (i, TypeEnum),
def::DefSelfTy(Some(def_id), _) => (def_id, TypeTrait),
def::DefSelfTy(_, Some((impl_id, _))) => return cx.map.local_def_id(impl_id),
_ => return def.def_id()
};
if did.is_local() { return did }