From ab3921f27e7cc0114568cac3def629a867ae4a54 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Fri, 9 Jul 2010 11:59:00 -0700 Subject: [PATCH] Catch cyclic imports harder. Add 2 tests to confirm. --- src/boot/me/semant.ml | 25 ++++++++++++++----------- src/test/compile-fail/import-loop-2.rs | 13 +++++++++++++ src/test/compile-fail/import-loop.rs | 7 +++++++ 3 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 src/test/compile-fail/import-loop-2.rs create mode 100644 src/test/compile-fail/import-loop.rs diff --git a/src/boot/me/semant.ml b/src/boot/me/semant.ml index 61eb148527b..64f2c939926 100644 --- a/src/boot/me/semant.ml +++ b/src/boot/me/semant.ml @@ -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 diff --git a/src/test/compile-fail/import-loop-2.rs b/src/test/compile-fail/import-loop-2.rs new file mode 100644 index 00000000000..474634b520c --- /dev/null +++ b/src/test/compile-fail/import-loop-2.rs @@ -0,0 +1,13 @@ +// error-pattern:cyclic import + +mod a { + import b.x; +} + +mod b { + import a.x; + + fn main() { + auto y = x; + } +} diff --git a/src/test/compile-fail/import-loop.rs b/src/test/compile-fail/import-loop.rs new file mode 100644 index 00000000000..649e2d5d3bc --- /dev/null +++ b/src/test/compile-fail/import-loop.rs @@ -0,0 +1,7 @@ +// error-pattern:cyclic import + +import x; + +fn main() { + auto y = x; +}