Skip to content

Commit f4b7115

Browse files
committed
move user_annotated_ty_for_adt into a helper trait
1 parent 423d810 commit f4b7115

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

src/librustc_mir/hair/cx/expr.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_data_structures::indexed_vec::Idx;
1313
use hair::cx::Cx;
1414
use hair::cx::block;
1515
use hair::cx::to_ref::ToRef;
16+
use hair::util::UserAnnotatedTyHelpers;
1617
use rustc::hir::def::{Def, CtorKind};
1718
use rustc::mir::interpret::GlobalId;
1819
use rustc::ty::{self, AdtKind, Ty};
@@ -475,7 +476,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
475476
adt_def: adt,
476477
variant_index: 0,
477478
substs,
478-
user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt),
479+
user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt),
479480
fields: field_refs(cx, fields),
480481
base: base.as_ref().map(|base| {
481482
FruInfo {
@@ -501,7 +502,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
501502
adt_def: adt,
502503
variant_index: index,
503504
substs,
504-
user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt),
505+
user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt),
505506
fields: field_refs(cx, fields),
506507
base: None,
507508
}
@@ -788,7 +789,7 @@ fn user_annotated_ty_for_def(
788789
Def::StructCtor(_def_id, CtorKind::Const) |
789790
Def::VariantCtor(_def_id, CtorKind::Const) =>
790791
match &cx.tables().node_id_to_type(hir_id).sty {
791-
ty::Adt(adt_def, _) => user_annotated_ty_for_adt(cx, hir_id, adt_def),
792+
ty::Adt(adt_def, _) => cx.user_annotated_ty_for_adt(hir_id, adt_def),
792793
sty => bug!("unexpected sty: {:?}", sty),
793794
},
794795

@@ -804,7 +805,7 @@ fn user_annotated_ty_for_def(
804805
}))
805806
}
806807
ty::Adt(ref adt_def, _) => {
807-
user_annotated_ty_for_adt(cx, hir_id, adt_def)
808+
cx.user_annotated_ty_for_adt(hir_id, adt_def)
808809
}
809810
_ => {
810811
bug!("unexpected sty: {:?}", sty)
@@ -816,19 +817,6 @@ fn user_annotated_ty_for_def(
816817
}
817818
}
818819

819-
fn user_annotated_ty_for_adt(
820-
cx: &mut Cx<'a, 'gcx, 'tcx>,
821-
hir_id: hir::HirId,
822-
adt_def: &'tcx AdtDef,
823-
) -> Option<CanonicalTy<'tcx>> {
824-
let user_substs = cx.tables().user_substs(hir_id)?;
825-
Some(user_substs.unchecked_map(|user_substs| {
826-
// Here, we just pair an `AdtDef` with the
827-
// `user_substs`, so no new types etc are introduced.
828-
cx.tcx().mk_adt(adt_def, user_substs)
829-
}))
830-
}
831-
832820
fn method_callee<'a, 'gcx, 'tcx>(
833821
cx: &mut Cx<'a, 'gcx, 'tcx>,
834822
expr: &hir::Expr,
@@ -943,7 +931,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
943931
adt_def,
944932
variant_index: adt_def.variant_index_with_id(def_id),
945933
substs,
946-
user_ty: user_annotated_ty_for_adt(cx, expr.hir_id, adt_def),
934+
user_ty: cx.user_annotated_ty_for_adt(expr.hir_id, adt_def),
947935
fields: vec![],
948936
base: None,
949937
}

src/librustc_mir/hair/cx/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//!
1616
1717
use hair::*;
18+
use hair::util::UserAnnotatedTyHelpers;
1819

1920
use rustc_data_structures::indexed_vec::Idx;
2021
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
@@ -272,6 +273,16 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
272273
}
273274
}
274275

276+
impl UserAnnotatedTyHelpers<'gcx, 'tcx> for Cx<'_, 'gcx, 'tcx> {
277+
fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx> {
278+
self.tcx()
279+
}
280+
281+
fn tables(&self) -> &ty::TypeckTables<'tcx> {
282+
self.tables()
283+
}
284+
}
285+
275286
fn lint_level_for_hir_id(tcx: TyCtxt, mut id: ast::NodeId) -> ast::NodeId {
276287
// Right now we insert a `with_ignore` node in the dep graph here to
277288
// ignore the fact that `lint_levels` below depends on the entire crate.

src/librustc_mir/hair/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub mod cx;
2929
pub mod pattern;
3030
pub use self::pattern::{BindingMode, Pattern, PatternKind, FieldPattern};
3131

32+
mod util;
33+
3234
#[derive(Copy, Clone, Debug)]
3335
pub enum LintLevel {
3436
Inherited,

src/librustc_mir/hair/util.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use rustc::hir;
12+
use rustc::ty::{self, AdtDef, CanonicalTy, TyCtxt};
13+
14+
crate trait UserAnnotatedTyHelpers<'gcx: 'tcx, 'tcx> {
15+
fn tcx(&self) -> TyCtxt<'_, 'gcx, 'tcx>;
16+
17+
fn tables(&self) -> &ty::TypeckTables<'tcx>;
18+
19+
fn user_annotated_ty_for_adt(
20+
&self,
21+
hir_id: hir::HirId,
22+
adt_def: &'tcx AdtDef,
23+
) -> Option<CanonicalTy<'tcx>> {
24+
let user_substs = self.tables().user_substs(hir_id)?;
25+
Some(user_substs.unchecked_map(|user_substs| {
26+
// Here, we just pair an `AdtDef` with the
27+
// `user_substs`, so no new types etc are introduced.
28+
self.tcx().mk_adt(adt_def, user_substs)
29+
}))
30+
}
31+
}

0 commit comments

Comments
 (0)