Skip to content

Commit e50f2eb

Browse files
nikomatsakisalexreg
authored andcommitted
introduce trait_def_id method
Co-Authored-By: Alexander Regueiro <alexreg@me.com>
1 parent 80c653c commit e50f2eb

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/librustc/hir/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub use self::PrimTy::*;
1010
pub use self::UnOp::*;
1111
pub use self::UnsafeSource::*;
1212

13+
use errors::FatalError;
1314
use hir::def::Def;
1415
use hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
1516
use util::nodemap::{NodeMap, FxHashSet};
@@ -2055,6 +2056,20 @@ pub struct TraitRef {
20552056
pub hir_ref_id: HirId,
20562057
}
20572058

2059+
impl TraitRef {
2060+
/// Get the `DefId` of the referenced trait. It _must_ actually be a trait or trait alias.
2061+
pub fn trait_def_id(&self) -> DefId {
2062+
match self.path.def {
2063+
Def::Trait(did) => did,
2064+
Def::TraitAlias(did) => did,
2065+
Def::Err => {
2066+
FatalError.raise();
2067+
}
2068+
_ => unreachable!(),
2069+
}
2070+
}
2071+
}
2072+
20582073
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
20592074
pub struct PolyTraitRef {
20602075
/// The `'a` in `<'a> Foo<&'a T>`

src/librustc_typeck/astconv.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! The main routine here is `ast_ty_to_ty()`; each use is parameterized by an
33
//! instance of `AstConv`.
44
5-
use errors::{Applicability, FatalError, DiagnosticId};
5+
use errors::{Applicability, DiagnosticId};
66
use hir::{self, GenericArg, GenericArgs};
77
use hir::def::Def;
88
use hir::def_id::DefId;
@@ -690,35 +690,21 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
690690
{
691691
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1);
692692

693-
let trait_def_id = self.trait_def_id(trait_ref);
694693
self.ast_path_to_mono_trait_ref(trait_ref.path.span,
695-
trait_def_id,
694+
trait_ref.trait_def_id(),
696695
self_ty,
697696
trait_ref.path.segments.last().unwrap())
698697
}
699698

700-
/// Get the `DefId` of the given trait ref. It _must_ actually be a trait.
701-
fn trait_def_id(&self, trait_ref: &hir::TraitRef) -> DefId {
702-
let path = &trait_ref.path;
703-
match path.def {
704-
Def::Trait(trait_def_id) => trait_def_id,
705-
Def::TraitAlias(alias_def_id) => alias_def_id,
706-
Def::Err => {
707-
FatalError.raise();
708-
}
709-
_ => unreachable!(),
710-
}
711-
}
712-
713-
/// The given trait ref must actually be a trait.
699+
/// The given trait-ref must actually be a trait.
714700
pub(super) fn instantiate_poly_trait_ref_inner(&self,
715701
trait_ref: &hir::TraitRef,
716702
self_ty: Ty<'tcx>,
717703
poly_projections: &mut Vec<(ty::PolyProjectionPredicate<'tcx>, Span)>,
718704
speculative: bool)
719705
-> (ty::PolyTraitRef<'tcx>, Option<Vec<Span>>)
720706
{
721-
let trait_def_id = self.trait_def_id(trait_ref);
707+
let trait_def_id = trait_ref.trait_def_id();
722708

723709
debug!("instantiate_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id);
724710

0 commit comments

Comments
 (0)