From 5efc86a838b6dc5c41185957464b5a5b342aba19 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 3 May 2017 11:28:22 -0400 Subject: [PATCH] expose a method for converting `hir::Ty` to `Ty<'tcx>` Also, remove a lot of `pub` things from `librustc_typeck`. --- src/librustc_typeck/check/coercion.rs | 4 ---- src/librustc_typeck/collect.rs | 6 +++--- src/librustc_typeck/lib.rs | 22 +++++++++++++++++----- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 61d04876bfe..b0b57aee5b2 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -965,10 +965,6 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E> } } - pub fn is_empty(&self) -> bool { - self.pushed == 0 - } - /// Return the "expected type" with which this coercion was /// constructed. This represents the "downward propagated" type /// that was given to us at the start of typing whatever construct diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 0d75a1ecf3d..7d1a6894a82 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -116,7 +116,7 @@ pub fn provide(providers: &mut Providers) { /// `ItemCtxt` is parameterized by a `DefId` that it uses to satisfy /// `get_type_parameter_bounds` requests, drawing the information from /// the AST (`hir::Generics`), recursively. -struct ItemCtxt<'a,'tcx:'a> { +pub struct ItemCtxt<'a,'tcx:'a> { tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId, } @@ -180,7 +180,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'a, 'tcx> { // Utility types and common code for the above passes. impl<'a, 'tcx> ItemCtxt<'a, 'tcx> { - fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId) + pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId) -> ItemCtxt<'a,'tcx> { ItemCtxt { tcx: tcx, @@ -190,7 +190,7 @@ impl<'a, 'tcx> ItemCtxt<'a, 'tcx> { } impl<'a,'tcx> ItemCtxt<'a,'tcx> { - fn to_ty(&self, ast_ty: &hir::Ty) -> Ty<'tcx> { + pub fn to_ty(&self, ast_ty: &hir::Ty) -> Ty<'tcx> { AstConv::ast_ty_to_ty(self, ast_ty) } } diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 94b4bfade94..8bfa38f765e 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -122,14 +122,14 @@ use std::iter; // registered before they are used. pub mod diagnostics; -pub mod check; -pub mod check_unused; +mod check; +mod check_unused; mod astconv; -pub mod collect; +mod collect; mod constrained_type_params; mod impl_wf_check; -pub mod coherence; -pub mod variance; +mod coherence; +mod variance; pub struct TypeAndSubsts<'tcx> { pub substs: &'tcx Substs<'tcx>, @@ -337,4 +337,16 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) } } +/// A quasi-deprecated helper used in rustdoc and save-analysis to get +/// the type from a HIR node. +pub fn hir_ty_to_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, hir_ty: &hir::Ty) -> Ty<'tcx> { + // In case there are any projections etc, find the "environment" + // def-id that will be used to determine the traits/predicates in + // scope. This is derived from the enclosing item-like thing. + let env_node_id = tcx.hir.get_parent(hir_ty.id); + let env_def_id = tcx.hir.local_def_id(env_node_id); + let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id); + item_cx.to_ty(hir_ty) +} + __build_diagnostic_array! { librustc_typeck, DIAGNOSTICS }