Skip to content

Commit f829d20

Browse files
committed
Catch forward declarations in default type params at AST conversion.
1 parent 52a53e8 commit f829d20

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/librustc/middle/typeck/collect.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,24 @@ pub fn ty_generics(ccx: &CrateCtxt,
945945
let param_ty = ty::param_ty {idx: base_index + offset,
946946
def_id: local_def(param.id)};
947947
let bounds = @compute_bounds(ccx, param_ty, &param.bounds);
948-
let default = param.default.map(|x| ast_ty_to_ty(ccx, &ExplicitRscope, x));
948+
let default = param.default.map(|path| {
949+
let ty = ast_ty_to_ty(ccx, &ExplicitRscope, path);
950+
let cur_idx = param_ty.idx;
951+
952+
ty::walk_ty(ty, |t| {
953+
match ty::get(t).sty {
954+
ty::ty_param(p) => if p.idx > cur_idx {
955+
ccx.tcx.sess.span_err(path.span,
956+
"type parameters with a default cannot use \
957+
forward declared identifiers")
958+
},
959+
_ => {}
960+
}
961+
});
962+
963+
ty
964+
});
965+
949966
let def = ty::TypeParameterDef {
950967
ident: param.ident,
951968
def_id: local_def(param.id),

src/test/compile-fail/generic-type-params-forward-mention.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
// Ensure that we get an error and not an ICE for this problematic case.
1414
struct Foo<T = Option<U>, U = bool>;
15-
15+
//~^ ERROR type parameters with a default cannot use forward declared identifiers
1616
fn main() {
1717
let x: Foo;
18-
//~^ ERROR missing type param `U` in the substitution of `std::option::Option<U>`
1918
}

0 commit comments

Comments
 (0)