Skip to content

Commit ab3921f

Browse files
committed
Catch cyclic imports harder. Add 2 tests to confirm.
1 parent fdaa723 commit ab3921f

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/boot/me/semant.ml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,10 +1545,18 @@ let rec project_ident_from_items
15451545
(cx:ctxt)
15461546
(lchk:loop_check)
15471547
(scopes:scope list)
1548+
(scope_id:node_id)
15481549
((view:Ast.mod_view),(items:Ast.mod_items))
15491550
(ident:Ast.ident)
15501551
(inside:bool)
15511552
: resolved =
1553+
1554+
let lchk =
1555+
if List.mem (scope_id, ident) lchk
1556+
then err (Some scope_id) "cyclic import for ident %s" ident
1557+
else (scope_id, ident)::lchk
1558+
in
1559+
15521560
if not (inside || (exports_permit view ident))
15531561
then None
15541562
else
@@ -1558,7 +1566,8 @@ let rec project_ident_from_items
15581566
| None ->
15591567
match htab_search view.Ast.view_imports ident with
15601568
None -> None
1561-
| Some name -> lookup_by_name cx lchk scopes name
1569+
| Some name ->
1570+
lookup_by_name cx lchk scopes name
15621571

15631572
and found cx scopes id =
15641573
Hashtbl.replace cx.ctxt_node_referenced id ();
@@ -1578,7 +1587,7 @@ and project_name_comp_from_resolved
15781587
let ident = get_name_comp_ident ext in
15791588
let md = get_mod_item cx id in
15801589
Hashtbl.replace cx.ctxt_node_referenced id ();
1581-
project_ident_from_items cx lchk scopes md ident false
1590+
project_ident_from_items cx lchk scopes id md ident false
15821591

15831592
and lookup_by_name
15841593
(cx:ctxt)
@@ -1602,12 +1611,6 @@ and lookup_by_ident
16021611
(ident:Ast.ident)
16031612
: resolved =
16041613

1605-
let passing id =
1606-
if List.mem (id, ident) lchk
1607-
then err (Some id) "cyclic import for ident %s" ident
1608-
else (id, ident)::lchk
1609-
in
1610-
16111614
let check_slots scopes islots =
16121615
arr_search islots
16131616
(fun _ (sloti,ident') ->
@@ -1651,7 +1654,7 @@ and lookup_by_ident
16511654

16521655
| SCOPE_crate crate ->
16531656
project_ident_from_items
1654-
cx (passing crate.id) scopes crate.node.Ast.crate_items ident true
1657+
cx lchk scopes crate.id crate.node.Ast.crate_items ident true
16551658

16561659
| SCOPE_obj_fn fn ->
16571660
would_capture (check_slots scopes fn.node.Ast.fn_input_slots)
@@ -1671,8 +1674,8 @@ and lookup_by_ident
16711674
end
16721675

16731676
| Ast.MOD_ITEM_mod md ->
1674-
project_ident_from_items cx (passing item.id)
1675-
scopes md ident true
1677+
project_ident_from_items cx lchk
1678+
scopes item.id md ident true
16761679

16771680
| _ -> None
16781681
in
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// error-pattern:cyclic import
2+
3+
mod a {
4+
import b.x;
5+
}
6+
7+
mod b {
8+
import a.x;
9+
10+
fn main() {
11+
auto y = x;
12+
}
13+
}

src/test/compile-fail/import-loop.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// error-pattern:cyclic import
2+
3+
import x;
4+
5+
fn main() {
6+
auto y = x;
7+
}

0 commit comments

Comments
 (0)