]> git.lizzy.rs Git - rust.git/commitdiff
Catch forward declarations in default type params at AST conversion.
authorKevin Butler <haqkrs@gmail.com>
Thu, 17 Apr 2014 00:17:23 +0000 (01:17 +0100)
committerKevin Butler <haqkrs@gmail.com>
Thu, 17 Apr 2014 17:24:52 +0000 (18:24 +0100)
src/librustc/middle/typeck/collect.rs
src/test/compile-fail/generic-type-params-forward-mention.rs

index 7e53445147f50dc97b4ea2fc978d96a30da0d117..6df8da3edaa69e39f3c107b6056d5659614af820 100644 (file)
@@ -945,7 +945,24 @@ pub fn ty_generics(ccx: &CrateCtxt,
                 let param_ty = ty::param_ty {idx: base_index + offset,
                                              def_id: local_def(param.id)};
                 let bounds = @compute_bounds(ccx, param_ty, &param.bounds);
-                let default = param.default.map(|x| ast_ty_to_ty(ccx, &ExplicitRscope, x));
+                let default = param.default.map(|path| {
+                    let ty = ast_ty_to_ty(ccx, &ExplicitRscope, path);
+                    let cur_idx = param_ty.idx;
+
+                    ty::walk_ty(ty, |t| {
+                        match ty::get(t).sty {
+                            ty::ty_param(p) => if p.idx > cur_idx {
+                                ccx.tcx.sess.span_err(path.span,
+                                                        "type parameters with a default cannot use \
+                                                        forward declared identifiers")
+                            },
+                            _ => {}
+                        }
+                    });
+
+                    ty
+                });
+
                 let def = ty::TypeParameterDef {
                     ident: param.ident,
                     def_id: local_def(param.id),
index 424a92d74eec8b2bc86c2eb4bfc67a5ac1c1645c..ace53fb51a4056515222f395e204acf4e5954e4f 100644 (file)
@@ -12,8 +12,7 @@
 
 // Ensure that we get an error and not an ICE for this problematic case.
 struct Foo<T = Option<U>, U = bool>;
-
+//~^ ERROR type parameters with a default cannot use forward declared identifiers
 fn main() {
     let x: Foo;
-    //~^ ERROR missing type param `U` in the substitution of `std::option::Option<U>`
 }