Skip to content

Commit 430553b

Browse files
nikomatsakisAlexander Regueiro
and
Alexander Regueiro
committed
introduce trait_def_id method
Co-Authored-By: Alexander Regueiro <alexreg@me.com>
1 parent 6ecad33 commit 430553b

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};
@@ -2053,6 +2054,20 @@ pub struct TraitRef {
20532054
pub hir_ref_id: HirId,
20542055
}
20552056

2057+
impl TraitRef {
2058+
/// Get the `DefId` of the referenced trait. It _must_ actually be a trait or trait alias.
2059+
pub fn trait_def_id(&self) -> DefId {
2060+
match self.path.def {
2061+
Def::Trait(did) => did,
2062+
Def::TraitAlias(did) => did,
2063+
Def::Err => {
2064+
FatalError.raise();
2065+
}
2066+
_ => unreachable!(),
2067+
}
2068+
}
2069+
}
2070+
20562071
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
20572072
pub struct PolyTraitRef {
20582073
/// 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;
@@ -689,35 +689,21 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
689689
{
690690
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1);
691691

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

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

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

0 commit comments

Comments
 (0)