introduce trait_def_id method

Co-Authored-By: Alexander Regueiro <alexreg@me.com>
This commit is contained in:
Niko Matsakis 2019-01-08 10:50:24 -05:00
parent 6ecad33838
commit 430553bc77
2 changed files with 19 additions and 18 deletions

View file

@ -10,6 +10,7 @@ pub use self::PrimTy::*;
pub use self::UnOp::*;
pub use self::UnsafeSource::*;
use errors::FatalError;
use hir::def::Def;
use hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
use util::nodemap::{NodeMap, FxHashSet};
@ -2053,6 +2054,20 @@ pub struct TraitRef {
pub hir_ref_id: HirId,
}
impl TraitRef {
/// Get the `DefId` of the referenced trait. It _must_ actually be a trait or trait alias.
pub fn trait_def_id(&self) -> DefId {
match self.path.def {
Def::Trait(did) => did,
Def::TraitAlias(did) => did,
Def::Err => {
FatalError.raise();
}
_ => unreachable!(),
}
}
}
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct PolyTraitRef {
/// The `'a` in `<'a> Foo<&'a T>`

View file

@ -2,7 +2,7 @@
//! The main routine here is `ast_ty_to_ty()`; each use is parameterized by an
//! instance of `AstConv`.
use errors::{Applicability, FatalError, DiagnosticId};
use errors::{Applicability, DiagnosticId};
use hir::{self, GenericArg, GenericArgs};
use hir::def::Def;
use hir::def_id::DefId;
@ -689,27 +689,13 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
{
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1);
let trait_def_id = self.trait_def_id(trait_ref);
self.ast_path_to_mono_trait_ref(trait_ref.path.span,
trait_def_id,
trait_ref.trait_def_id(),
self_ty,
trait_ref.path.segments.last().unwrap())
}
/// Get the `DefId` of the given trait ref. It _must_ actually be a trait.
fn trait_def_id(&self, trait_ref: &hir::TraitRef) -> DefId {
let path = &trait_ref.path;
match path.def {
Def::Trait(trait_def_id) => trait_def_id,
Def::TraitAlias(alias_def_id) => alias_def_id,
Def::Err => {
FatalError.raise();
}
_ => unreachable!(),
}
}
/// The given trait ref must actually be a trait.
/// The given trait-ref must actually be a trait.
pub(super) fn instantiate_poly_trait_ref_inner(&self,
trait_ref: &hir::TraitRef,
self_ty: Ty<'tcx>,
@ -717,7 +703,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
speculative: bool)
-> (ty::PolyTraitRef<'tcx>, Option<Vec<Span>>)
{
let trait_def_id = self.trait_def_id(trait_ref);
let trait_def_id = trait_ref.trait_def_id();
debug!("instantiate_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id);