Skip to content

Commit 86e4029

Browse files
committed
rustc_typeck: simplify AstConv requests as implemented by collect.
1 parent e8d01ea commit 86e4029

File tree

6 files changed

+350
-542
lines changed

6 files changed

+350
-542
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@ pub trait AstConv<'gcx, 'tcx> {
5252
fn ast_ty_to_ty_cache(&self) -> &RefCell<NodeMap<Ty<'tcx>>>;
5353

5454
/// Returns the generic type and lifetime parameters for an item.
55-
fn get_generics(&self, span: Span, id: DefId)
56-
-> Result<&'tcx ty::Generics, ErrorReported>;
55+
fn get_generics(&self, id: DefId) -> &'tcx ty::Generics;
5756

5857
/// Identify the type for an item, like a type alias, fn, or struct.
59-
fn get_item_type(&self, span: Span, id: DefId) -> Result<Ty<'tcx>, ErrorReported>;
58+
fn get_item_type(&self, span: Span, id: DefId) -> Ty<'tcx>;
6059

6160
/// Returns the `TraitDef` for a given trait. This allows you to
6261
/// figure out the set of type parameters defined on the trait.
63-
fn get_trait_def(&self, span: Span, id: DefId)
64-
-> Result<&'tcx ty::TraitDef, ErrorReported>;
62+
fn get_trait_def(&self, id: DefId) -> &'tcx ty::TraitDef;
6563

6664
/// Ensure that the super-predicates for the trait with the given
6765
/// id are available and also for the transitive set of
@@ -251,14 +249,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
251249
// If the type is parameterized by this region, then replace this
252250
// region with the current anon region binding (in other words,
253251
// whatever & would get replaced with).
254-
let decl_generics = match self.get_generics(span, def_id) {
255-
Ok(generics) => generics,
256-
Err(ErrorReported) => {
257-
// No convenient way to recover from a cycle here. Just bail. Sorry!
258-
self.tcx().sess.abort_if_errors();
259-
bug!("ErrorReported returned, but no errors reports?")
260-
}
261-
};
252+
let decl_generics = self.get_generics(def_id);
262253
let expected_num_region_params = decl_generics.regions.len();
263254
let supplied_num_region_params = lifetimes.len();
264255
if expected_num_region_params != supplied_num_region_params {
@@ -279,8 +270,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
279270
let is_object = self_ty.map_or(false, |ty| ty.sty == TRAIT_OBJECT_DUMMY_SELF);
280271
let default_needs_object_self = |p: &ty::TypeParameterDef| {
281272
if is_object && p.has_default {
282-
let default = self.get_item_type(span, p.def_id).ok();
283-
if default.has_self_ty() {
273+
if self.get_item_type(span, p.def_id).has_self_ty() {
284274
// There is no suitable inference default for a type parameter
285275
// that references self, in an object type.
286276
return true;
@@ -347,10 +337,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
347337
tcx.types.err
348338
} else {
349339
// This is a default type parameter.
350-
match self.get_item_type(span, def.def_id) {
351-
Ok(ty) => ty.subst_spanned(tcx, substs, Some(span)),
352-
Err(ErrorReported) => tcx.types.err
353-
}
340+
self.get_item_type(span, def.def_id).subst_spanned(tcx, substs, Some(span))
354341
}
355342
} else {
356343
// We've already errored above about the mismatch.
@@ -499,14 +486,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
499486
debug!("create_substs_for_ast_trait_ref(trait_segment={:?})",
500487
trait_segment);
501488

502-
let trait_def = match self.get_trait_def(span, trait_def_id) {
503-
Ok(trait_def) => trait_def,
504-
Err(ErrorReported) => {
505-
// No convenient way to recover from a cycle here. Just bail. Sorry!
506-
self.tcx().sess.abort_if_errors();
507-
bug!("ErrorReported returned, but no errors reports?")
508-
}
509-
};
489+
let trait_def = self.get_trait_def(trait_def_id);
510490

511491
match trait_segment.parameters {
512492
hir::AngleBracketedParameters(_) => {
@@ -647,16 +627,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
647627
item_segment: &hir::PathSegment)
648628
-> Ty<'tcx>
649629
{
650-
let tcx = self.tcx();
651-
let decl_ty = match self.get_item_type(span, did) {
652-
Ok(ty) => ty,
653-
Err(ErrorReported) => {
654-
return tcx.types.err;
655-
}
656-
};
657-
658630
let substs = self.ast_path_substs_for_ty(span, did, item_segment);
659-
decl_ty.subst(self.tcx(), substs)
631+
self.get_item_type(span, did).subst(self.tcx(), substs)
660632
}
661633

662634
/// Transform a PolyTraitRef into a PolyExistentialTraitRef by
@@ -1058,37 +1030,23 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
10581030
tcx.prohibit_type_params(&path.segments);
10591031

10601032
let node_id = tcx.hir.as_local_node_id(did).unwrap();
1061-
let item_def_id = tcx.hir.local_def_id(::ty_param_owner(tcx, node_id));
1062-
let index = match self.get_generics(span, item_def_id) {
1063-
Ok(generics) => {
1064-
generics.type_param_to_index[&tcx.hir.local_def_id(node_id).index]
1065-
}
1066-
Err(ErrorReported) => return tcx.types.err
1067-
};
1068-
tcx.mk_param(index, ::ty_param_name(tcx, node_id))
1033+
let item_id = tcx.hir.get_parent_node(node_id);
1034+
let item_def_id = tcx.hir.local_def_id(item_id);
1035+
let generics = self.get_generics(item_def_id);
1036+
let index = generics.type_param_to_index[&tcx.hir.local_def_id(node_id).index];
1037+
tcx.mk_param(index, tcx.hir.name(node_id))
10691038
}
10701039
Def::SelfTy(_, Some(def_id)) => {
10711040
// Self in impl (we know the concrete type).
10721041

10731042
assert_eq!(opt_self_ty, None);
10741043
tcx.prohibit_type_params(&path.segments);
10751044

1076-
// FIXME: Self type is not always computed when we are here because type parameter
1077-
// bounds may affect Self type and have to be converted before it.
1078-
let ty = if def_id.is_local() {
1079-
tcx.item_types.borrow().get(&def_id).cloned()
1080-
} else {
1081-
Some(tcx.item_type(def_id))
1082-
};
1083-
if let Some(ty) = ty {
1084-
if let Some(free_substs) = self.get_free_substs() {
1085-
ty.subst(tcx, free_substs)
1086-
} else {
1087-
ty
1088-
}
1045+
let ty = self.get_item_type(span, def_id);
1046+
if let Some(free_substs) = self.get_free_substs() {
1047+
ty.subst(tcx, free_substs)
10891048
} else {
1090-
tcx.sess.span_err(span, "`Self` type is used before it's determined");
1091-
tcx.types.err
1049+
ty
10921050
}
10931051
}
10941052
Def::SelfTy(Some(_), None) => {
@@ -1241,9 +1199,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
12411199
// Create the anonymized type.
12421200
if allow {
12431201
let def_id = tcx.hir.local_def_id(ast_ty.id);
1244-
if let Err(ErrorReported) = self.get_generics(ast_ty.span, def_id) {
1245-
return tcx.types.err;
1246-
}
1202+
self.get_generics(def_id);
12471203
let substs = Substs::identity_for_item(tcx, def_id);
12481204
let ty = tcx.mk_anon(tcx.hir.local_def_id(ast_ty.id), substs);
12491205

src/librustc_typeck/check/mod.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ use rustc::ty::subst::{Kind, Subst, Substs};
9191
use rustc::traits::{self, ObligationCause, ObligationCauseCode, Reveal};
9292
use rustc::ty::{ParamTy, ParameterEnvironment};
9393
use rustc::ty::{LvaluePreference, NoPreference, PreferMutLvalue};
94-
use rustc::ty::{self, ToPolyTraitRef, Ty, TyCtxt, Visibility};
94+
use rustc::ty::{self, Ty, TyCtxt, Visibility, ToPolyTraitRef};
9595
use rustc::ty::{MethodCall, MethodCallee};
9696
use rustc::ty::adjustment;
9797
use rustc::ty::fold::{BottomUpFolder, TypeFoldable};
@@ -1359,21 +1359,16 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
13591359
&self.ast_ty_to_ty_cache
13601360
}
13611361

1362-
fn get_generics(&self, _: Span, id: DefId)
1363-
-> Result<&'tcx ty::Generics, ErrorReported>
1364-
{
1365-
Ok(self.tcx().item_generics(id))
1362+
fn get_generics(&self, id: DefId) -> &'tcx ty::Generics {
1363+
self.tcx().item_generics(id)
13661364
}
13671365

1368-
fn get_item_type(&self, _: Span, id: DefId) -> Result<Ty<'tcx>, ErrorReported>
1369-
{
1370-
Ok(self.tcx().item_type(id))
1366+
fn get_item_type(&self, _: Span, id: DefId) -> Ty<'tcx> {
1367+
self.tcx().item_type(id)
13711368
}
13721369

1373-
fn get_trait_def(&self, _: Span, id: DefId)
1374-
-> Result<&'tcx ty::TraitDef, ErrorReported>
1375-
{
1376-
Ok(self.tcx().lookup_trait_def(id))
1370+
fn get_trait_def(&self, id: DefId) -> &'tcx ty::TraitDef {
1371+
self.tcx().lookup_trait_def(id)
13771372
}
13781373

13791374
fn ensure_super_predicates(&self, _: Span, _: DefId) -> Result<(), ErrorReported> {
@@ -1391,7 +1386,8 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
13911386
-> Result<Vec<ty::PolyTraitRef<'tcx>>, ErrorReported>
13921387
{
13931388
let tcx = self.tcx;
1394-
let item_def_id = tcx.hir.local_def_id(::ty_param_owner(tcx, node_id));
1389+
let item_id = ::ty_param_owner(tcx, node_id);
1390+
let item_def_id = tcx.hir.local_def_id(item_id);
13951391
let generics = tcx.item_generics(item_def_id);
13961392
let index = generics.type_param_to_index[&tcx.hir.local_def_id(node_id).index];
13971393
let r = self.parameter_environment

0 commit comments

Comments
 (0)