about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorSean Bowe <ewillbefull@gmail.com>2015-09-14 02:33:29 -0600
committerSean Bowe <ewillbefull@gmail.com>2015-09-14 02:33:29 -0600
commit4fec679399ddaf67d15adddad2677731f94dfa36 (patch)
tree5dd5bb4bba11884554a79136d6519e20bd2ed85f /src
parent3d0774f5989fc9425c5dc0ab5118fdcabba33555 (diff)
downloadrust-4fec679399ddaf67d15adddad2677731f94dfa36.tar.gz
rust-4fec679399ddaf67d15adddad2677731f94dfa36.zip
Cleaner abstraction for type_substs
Diffstat (limited to 'src')
-rw-r--r--src/librustc_typeck/astconv.rs71
1 files changed, 43 insertions, 28 deletions
diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs
index 13476ddebc1..0537b176aef 100644
--- a/src/librustc_typeck/astconv.rs
+++ b/src/librustc_typeck/astconv.rs
@@ -408,34 +408,13 @@ fn create_substs_for_ast_path<'tcx>(
                                                .take_while(|x| x.default.is_none())
                                                .count();
 
-    // Fill with `ty_infer` if no params were specified, as long as
-    // they were optional (e.g. paths inside expressions).
-    let mut type_substs = if param_mode == PathParamMode::Optional &&
-                             types_provided.is_empty() {
-        fn default_type_parameter<'tcx>(p: &ty::TypeParameterDef<'tcx>, self_ty: Option<Ty<'tcx>>)
-                                        -> Option<ty::TypeParameterDef<'tcx>>
-        {
-            if let Some(ref default) = p.default {
-                if self_ty.is_none() && default.has_self_ty() {
-                    // There is no suitable inference default for a type parameter
-                    // that references self with no self-type provided.
-                    return None;
-                }
-            }
-
-            Some(p.clone())
-        }
-
-        let mut substs = region_substs.clone();
-
-        ty_param_defs
-            .iter()
-            .map(|p| this.ty_infer(default_type_parameter(p, self_ty), Some(&mut substs),
-                                   Some(TypeSpace), span))
-            .collect()
-    } else {
-        types_provided
-    };
+    let mut type_substs = get_type_substs_for_defs(this,
+                                                   span,
+                                                   types_provided,
+                                                   param_mode,
+                                                   ty_param_defs,
+                                                   region_substs.clone(),
+                                                   self_ty);
 
     let supplied_ty_param_count = type_substs.len();
     check_type_argument_count(this.tcx(), span, supplied_ty_param_count,
@@ -499,6 +478,42 @@ fn create_substs_for_ast_path<'tcx>(
     substs
 }
 
+/// Returns types_provided if it is not empty, otherwise populating the
+/// type parameters with inference variables as appropriate.
+fn get_type_substs_for_defs<'tcx>(this: &AstConv<'tcx>,
+                                  span: Span,
+                                  types_provided: Vec<Ty<'tcx>>,
+                                  param_mode: PathParamMode,
+                                  ty_param_defs: &[ty::TypeParameterDef<'tcx>],
+                                  mut substs: Substs<'tcx>,
+                                  self_ty: Option<Ty<'tcx>>)
+                                  -> Vec<Ty<'tcx>>
+{
+    fn default_type_parameter<'tcx>(p: &ty::TypeParameterDef<'tcx>, self_ty: Option<Ty<'tcx>>)
+                                    -> Option<ty::TypeParameterDef<'tcx>>
+    {
+        if let Some(ref default) = p.default {
+            if self_ty.is_none() && default.has_self_ty() {
+                // There is no suitable inference default for a type parameter
+                // that references self with no self-type provided.
+                return None;
+            }
+        }
+
+        Some(p.clone())
+    }
+
+    if param_mode == PathParamMode::Optional && types_provided.is_empty() {
+        ty_param_defs
+            .iter()
+            .map(|p| this.ty_infer(default_type_parameter(p, self_ty), Some(&mut substs),
+                                   Some(TypeSpace), span))
+            .collect()
+    } else {
+        types_provided
+    }
+}
+
 struct ConvertedBinding<'tcx> {
     item_name: ast::Name,
     ty: Ty<'tcx>,