Skip to content

Commit 4fec679

Browse files
committed
Cleaner abstraction for type_substs
1 parent 3d0774f commit 4fec679

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -408,34 +408,13 @@ fn create_substs_for_ast_path<'tcx>(
408408
.take_while(|x| x.default.is_none())
409409
.count();
410410

411-
// Fill with `ty_infer` if no params were specified, as long as
412-
// they were optional (e.g. paths inside expressions).
413-
let mut type_substs = if param_mode == PathParamMode::Optional &&
414-
types_provided.is_empty() {
415-
fn default_type_parameter<'tcx>(p: &ty::TypeParameterDef<'tcx>, self_ty: Option<Ty<'tcx>>)
416-
-> Option<ty::TypeParameterDef<'tcx>>
417-
{
418-
if let Some(ref default) = p.default {
419-
if self_ty.is_none() && default.has_self_ty() {
420-
// There is no suitable inference default for a type parameter
421-
// that references self with no self-type provided.
422-
return None;
423-
}
424-
}
425-
426-
Some(p.clone())
427-
}
428-
429-
let mut substs = region_substs.clone();
430-
431-
ty_param_defs
432-
.iter()
433-
.map(|p| this.ty_infer(default_type_parameter(p, self_ty), Some(&mut substs),
434-
Some(TypeSpace), span))
435-
.collect()
436-
} else {
437-
types_provided
438-
};
411+
let mut type_substs = get_type_substs_for_defs(this,
412+
span,
413+
types_provided,
414+
param_mode,
415+
ty_param_defs,
416+
region_substs.clone(),
417+
self_ty);
439418

440419
let supplied_ty_param_count = type_substs.len();
441420
check_type_argument_count(this.tcx(), span, supplied_ty_param_count,
@@ -499,6 +478,42 @@ fn create_substs_for_ast_path<'tcx>(
499478
substs
500479
}
501480

481+
/// Returns types_provided if it is not empty, otherwise populating the
482+
/// type parameters with inference variables as appropriate.
483+
fn get_type_substs_for_defs<'tcx>(this: &AstConv<'tcx>,
484+
span: Span,
485+
types_provided: Vec<Ty<'tcx>>,
486+
param_mode: PathParamMode,
487+
ty_param_defs: &[ty::TypeParameterDef<'tcx>],
488+
mut substs: Substs<'tcx>,
489+
self_ty: Option<Ty<'tcx>>)
490+
-> Vec<Ty<'tcx>>
491+
{
492+
fn default_type_parameter<'tcx>(p: &ty::TypeParameterDef<'tcx>, self_ty: Option<Ty<'tcx>>)
493+
-> Option<ty::TypeParameterDef<'tcx>>
494+
{
495+
if let Some(ref default) = p.default {
496+
if self_ty.is_none() && default.has_self_ty() {
497+
// There is no suitable inference default for a type parameter
498+
// that references self with no self-type provided.
499+
return None;
500+
}
501+
}
502+
503+
Some(p.clone())
504+
}
505+
506+
if param_mode == PathParamMode::Optional && types_provided.is_empty() {
507+
ty_param_defs
508+
.iter()
509+
.map(|p| this.ty_infer(default_type_parameter(p, self_ty), Some(&mut substs),
510+
Some(TypeSpace), span))
511+
.collect()
512+
} else {
513+
types_provided
514+
}
515+
}
516+
502517
struct ConvertedBinding<'tcx> {
503518
item_name: ast::Name,
504519
ty: Ty<'tcx>,

0 commit comments

Comments
 (0)