Skip to content

Commit f7a1006

Browse files
committed
Make resolve check for type-variable name-shadowing
Capturing a type argument in the enclosing scope should be an error -- this commit implements that check in resolve, avoiding a potential assertion failure in trans. Closes #648.
1 parent 0d9c08a commit f7a1006

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/comp/middle/resolve.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,10 @@ fn def_is_obj_field(&def d) -> bool {
596596
ret alt (d) { case (ast::def_obj_field(_)) { true } case (_) { false } };
597597
}
598598

599+
fn def_is_ty_arg(&def d) -> bool {
600+
ret alt(d) { case (ast::def_ty_arg(_)) { true } case (_) { false } };
601+
}
602+
599603
fn lookup_in_scope(&env e, scopes sc, &span sp, &ident name, namespace ns) ->
600604
option::t[def] {
601605
fn in_scope(&env e, &span sp, &ident name, &scope s, namespace ns) ->
@@ -666,15 +670,24 @@ fn lookup_in_scope(&env e, scopes sc, &span sp, &ident name, namespace ns) ->
666670
if (!option::is_none(fnd)) {
667671
auto df = option::get(fnd);
668672
if (left_fn && def_is_local(df) ||
669-
left_fn_level2 && def_is_obj_field(df)) {
670-
e.sess.span_fatal(sp,
671-
"attempted dynamic \
672-
environment-capture");
673+
left_fn_level2 && def_is_obj_field(df)
674+
|| (scope_is_fn(hd) && left_fn
675+
&& def_is_ty_arg(df))) {
676+
auto msg = alt (ns) {
677+
case (ns_type) {
678+
"Attempt to use a type \
679+
argument out of scope"
680+
}
681+
case (_) { "attempted dynamic \
682+
environment-capture" }
683+
};
684+
e.sess.span_fatal(sp, msg);
673685
}
674686
ret fnd;
675687
}
676688
if (left_fn) { left_fn_level2 = true; }
677-
if (ns == ns_value && !left_fn) { left_fn = scope_is_fn(hd); }
689+
if ((ns == ns_value || ns == ns_type) && !left_fn) {
690+
left_fn = scope_is_fn(hd); }
678691
sc = *tl;
679692
}
680693
}

src/test/compile-fail/nested-ty-params.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
// error-pattern:Unbound type parameter in callee
2-
/* I'm actually not sure whether this should compile.
3-
But having a nice error message seems better than
4-
a bounds check failure (which is what was happening
5-
before.) */
1+
// xfail-stage0
2+
// error-pattern:Attempt to use a type argument out of scope
63
fn hd[U](&vec[U] v) -> U {
74
fn hd1(&vec[U] w) -> U {
85
ret w.(0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// xfail-stage0
2+
// error-pattern:Attempt to use a type argument out of scope
3+
fn foo[T] (&T x) {
4+
fn bar(fn (&T) -> T f) { };
5+
}
6+
fn main() { foo(1); }

0 commit comments

Comments
 (0)