diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2018-10-02 11:00:57 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2018-10-08 19:20:37 -0400 |
| commit | f4b711516d0ef53957c1f65590051ed9e43cb4a9 (patch) | |
| tree | ff651d9eaf2e3ac7371bf56572ade2b01114064d | |
| parent | 423d8109868c1f926f2cfcc3bff980c3daa515fd (diff) | |
| download | rust-f4b711516d0ef53957c1f65590051ed9e43cb4a9.tar.gz rust-f4b711516d0ef53957c1f65590051ed9e43cb4a9.zip | |
move `user_annotated_ty_for_adt` into a helper trait
| -rw-r--r-- | src/librustc_mir/hair/cx/expr.rs | 24 | ||||
| -rw-r--r-- | src/librustc_mir/hair/cx/mod.rs | 11 | ||||
| -rw-r--r-- | src/librustc_mir/hair/mod.rs | 2 | ||||
| -rw-r--r-- | src/librustc_mir/hair/util.rs | 31 |
4 files changed, 50 insertions, 18 deletions
diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs index 8d50fbbdc6b..ef7be184ab0 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -13,6 +13,7 @@ use rustc_data_structures::indexed_vec::Idx; use hair::cx::Cx; use hair::cx::block; use hair::cx::to_ref::ToRef; +use hair::util::UserAnnotatedTyHelpers; use rustc::hir::def::{Def, CtorKind}; use rustc::mir::interpret::GlobalId; use rustc::ty::{self, AdtKind, Ty}; @@ -475,7 +476,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, adt_def: adt, variant_index: 0, substs, - user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt), + user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt), fields: field_refs(cx, fields), base: base.as_ref().map(|base| { FruInfo { @@ -501,7 +502,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, adt_def: adt, variant_index: index, substs, - user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt), + user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt), fields: field_refs(cx, fields), base: None, } @@ -788,7 +789,7 @@ fn user_annotated_ty_for_def( Def::StructCtor(_def_id, CtorKind::Const) | Def::VariantCtor(_def_id, CtorKind::Const) => match &cx.tables().node_id_to_type(hir_id).sty { - ty::Adt(adt_def, _) => user_annotated_ty_for_adt(cx, hir_id, adt_def), + ty::Adt(adt_def, _) => cx.user_annotated_ty_for_adt(hir_id, adt_def), sty => bug!("unexpected sty: {:?}", sty), }, @@ -804,7 +805,7 @@ fn user_annotated_ty_for_def( })) } ty::Adt(ref adt_def, _) => { - user_annotated_ty_for_adt(cx, hir_id, adt_def) + cx.user_annotated_ty_for_adt(hir_id, adt_def) } _ => { bug!("unexpected sty: {:?}", sty) @@ -816,19 +817,6 @@ fn user_annotated_ty_for_def( } } -fn user_annotated_ty_for_adt( - cx: &mut Cx<'a, 'gcx, 'tcx>, - hir_id: hir::HirId, - adt_def: &'tcx AdtDef, -) -> Option<CanonicalTy<'tcx>> { - let user_substs = cx.tables().user_substs(hir_id)?; - Some(user_substs.unchecked_map(|user_substs| { - // Here, we just pair an `AdtDef` with the - // `user_substs`, so no new types etc are introduced. - cx.tcx().mk_adt(adt_def, user_substs) - })) -} - fn method_callee<'a, 'gcx, 'tcx>( cx: &mut Cx<'a, 'gcx, 'tcx>, expr: &hir::Expr, @@ -943,7 +931,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, adt_def, variant_index: adt_def.variant_index_with_id(def_id), substs, - user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt_def), + user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt_def), fields: vec![], base: None, } diff --git a/src/librustc_mir/hair/cx/mod.rs b/src/librustc_mir/hair/cx/mod.rs index 4d4a89fca8b..5f798135966 100644 --- a/src/librustc_mir/hair/cx/mod.rs +++ b/src/librustc_mir/hair/cx/mod.rs @@ -15,6 +15,7 @@ //! use hair::*; +use hair::util::UserAnnotatedTyHelpers; use rustc_data_structures::indexed_vec::Idx; use rustc::hir::def_id::{DefId, LOCAL_CRATE}; @@ -272,6 +273,16 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> { } } +impl UserAnnotatedTyHelpers<'gcx, 'tcx> for Cx<'_, 'gcx, 'tcx> { + fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> { + self.tcx() + } + + fn tables(&self) -> &ty::TypeckTables<'tcx> { + self.tables() + } +} + fn lint_level_for_hir_id(tcx: TyCtxt, mut id: ast::NodeId) -> ast::NodeId { // Right now we insert a `with_ignore` node in the dep graph here to // ignore the fact that `lint_levels` below depends on the entire crate. diff --git a/src/librustc_mir/hair/mod.rs b/src/librustc_mir/hair/mod.rs index eb73a202148..e4f88e4fcc3 100644 --- a/src/librustc_mir/hair/mod.rs +++ b/src/librustc_mir/hair/mod.rs @@ -29,6 +29,8 @@ pub mod cx; pub mod pattern; pub use self::pattern::{BindingMode, Pattern, PatternKind, FieldPattern}; +mod util; + #[derive(Copy, Clone, Debug)] pub enum LintLevel { Inherited, diff --git a/src/librustc_mir/hair/util.rs b/src/librustc_mir/hair/util.rs new file mode 100644 index 00000000000..803ce615747 --- /dev/null +++ b/src/librustc_mir/hair/util.rs @@ -0,0 +1,31 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use rustc::hir; +use rustc::ty::{self, AdtDef, CanonicalTy, TyCtxt}; + +crate trait UserAnnotatedTyHelpers<'gcx: 'tcx, 'tcx> { + fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx>; + + fn tables(&self) -> &ty::TypeckTables<'tcx>; + + fn user_annotated_ty_for_adt( + &self, + hir_id: hir::HirId, + adt_def: &'tcx AdtDef, + ) -> Option<CanonicalTy<'tcx>> { + let user_substs = self.tables().user_substs(hir_id)?; + Some(user_substs.unchecked_map(|user_substs| { + // Here, we just pair an `AdtDef` with the + // `user_substs`, so no new types etc are introduced. + self.tcx().mk_adt(adt_def, user_substs) + })) + } +} |
