Skip to content

Commit a1110bc

Browse files
author
Ariel Ben-Yehuda
committed
Fix off-by-one error in default-type-parameter checking
Fixes #18183
1 parent 40db46c commit a1110bc

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/librustc_typeck/collect.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,29 @@ fn ty_generics<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
18601860
result
18611861
}
18621862

1863+
fn convert_default_type_parameter<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
1864+
path: &P<ast::Ty>,
1865+
space: ParamSpace,
1866+
index: u32)
1867+
-> Ty<'tcx>
1868+
{
1869+
let ty = ast_ty_to_ty(&ccx.icx(&()), &ExplicitRscope, &path);
1870+
1871+
for leaf_ty in ty.walk() {
1872+
if let ty::TyParam(p) = leaf_ty.sty {
1873+
if p.space == space && p.idx >= index {
1874+
span_err!(ccx.tcx.sess, path.span, E0128,
1875+
"type parameters with a default cannot use \
1876+
forward declared identifiers");
1877+
1878+
return ccx.tcx.types.err
1879+
}
1880+
}
1881+
}
1882+
1883+
ty
1884+
}
1885+
18631886
fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
18641887
ast_generics: &ast::Generics,
18651888
space: ParamSpace,
@@ -1874,25 +1897,9 @@ fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
18741897
None => { }
18751898
}
18761899

1877-
let default = match param.default {
1878-
None => None,
1879-
Some(ref path) => {
1880-
let ty = ast_ty_to_ty(&ccx.icx(&()), &ExplicitRscope, &**path);
1881-
let cur_idx = index;
1882-
1883-
for leaf_ty in ty.walk() {
1884-
if let ty::TyParam(p) = leaf_ty.sty {
1885-
if p.idx > cur_idx {
1886-
span_err!(tcx.sess, path.span, E0128,
1887-
"type parameters with a default cannot use \
1888-
forward declared identifiers");
1889-
}
1890-
}
1891-
}
1892-
1893-
Some(ty)
1894-
}
1895-
};
1900+
let default = param.default.as_ref().map(
1901+
|def| convert_default_type_parameter(ccx, def, space, index)
1902+
);
18961903

18971904
let object_lifetime_default =
18981905
compute_object_lifetime_default(ccx, param.id,

src/test/compile-fail/issue-18183.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub struct Foo<Bar=Bar>; //~ ERROR E0128
12+
pub struct Baz(Foo);
13+
fn main() {}

0 commit comments

Comments
 (0)