Catch cyclic imports harder. Add 2 tests to confirm.

This commit is contained in:
Graydon Hoare 2010-07-09 11:59:00 -07:00
parent fdaa723c4a
commit ab3921f27e
3 changed files with 34 additions and 11 deletions

View File

@ -1545,10 +1545,18 @@ let rec project_ident_from_items
(cx:ctxt)
(lchk:loop_check)
(scopes:scope list)
(scope_id:node_id)
((view:Ast.mod_view),(items:Ast.mod_items))
(ident:Ast.ident)
(inside:bool)
: resolved =
let lchk =
if List.mem (scope_id, ident) lchk
then err (Some scope_id) "cyclic import for ident %s" ident
else (scope_id, ident)::lchk
in
if not (inside || (exports_permit view ident))
then None
else
@ -1558,7 +1566,8 @@ let rec project_ident_from_items
| None ->
match htab_search view.Ast.view_imports ident with
None -> None
| Some name -> lookup_by_name cx lchk scopes name
| Some name ->
lookup_by_name cx lchk scopes name
and found cx scopes id =
Hashtbl.replace cx.ctxt_node_referenced id ();
@ -1578,7 +1587,7 @@ and project_name_comp_from_resolved
let ident = get_name_comp_ident ext in
let md = get_mod_item cx id in
Hashtbl.replace cx.ctxt_node_referenced id ();
project_ident_from_items cx lchk scopes md ident false
project_ident_from_items cx lchk scopes id md ident false
and lookup_by_name
(cx:ctxt)
@ -1602,12 +1611,6 @@ and lookup_by_ident
(ident:Ast.ident)
: resolved =
let passing id =
if List.mem (id, ident) lchk
then err (Some id) "cyclic import for ident %s" ident
else (id, ident)::lchk
in
let check_slots scopes islots =
arr_search islots
(fun _ (sloti,ident') ->
@ -1651,7 +1654,7 @@ and lookup_by_ident
| SCOPE_crate crate ->
project_ident_from_items
cx (passing crate.id) scopes crate.node.Ast.crate_items ident true
cx lchk scopes crate.id crate.node.Ast.crate_items ident true
| SCOPE_obj_fn fn ->
would_capture (check_slots scopes fn.node.Ast.fn_input_slots)
@ -1671,8 +1674,8 @@ and lookup_by_ident
end
| Ast.MOD_ITEM_mod md ->
project_ident_from_items cx (passing item.id)
scopes md ident true
project_ident_from_items cx lchk
scopes item.id md ident true
| _ -> None
in

View File

@ -0,0 +1,13 @@
// error-pattern:cyclic import
mod a {
import b.x;
}
mod b {
import a.x;
fn main() {
auto y = x;
}
}

View File

@ -0,0 +1,7 @@
// error-pattern:cyclic import
import x;
fn main() {
auto y = x;
}