Skip to content

Commit

Permalink
Report error from imported top level statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Jun 7, 2024
1 parent 922ca26 commit 467ae30
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
9 changes: 2 additions & 7 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2049,8 +2049,7 @@ print random()
### Main module.
Only the main module can have top-level statements that aren't static declarations.
If an imported module contains top-level statements, they are ignored.
In the future, it may result in a compile error instead:
An imported module containing top-level statements returns an error:
```cy
-- main.cy
use a 'foo.cy'
Expand All @@ -2059,11 +2058,7 @@ print a.foo
-- foo.cy
use 'bar.cy'
var .foo = 123
print foo -- Statement is ignored.

-- bar.cy
var .bar = 321
print bar -- Statement is ignored.
print foo -- Error: Top-level statement not allowed.
```
### Circular imports.
Expand Down
25 changes: 25 additions & 0 deletions src/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,31 @@ fn reserveSyms(self: *Compiler, core_sym: *cy.sym.Chunk) !void{
self.sema.list_tmpl = core.getSym("List").?.cast(.template);
self.sema.table_type = core.getSym("Table").?.cast(.object_t);
}
if (chunk != self.main_chunk) {
// Check for illegal top level statements.
if (chunk.parser.staticDecls.items.len != chunk.ast.root.?.stmts.len) {
for (chunk.ast.root.?.stmts) |stmt| {
switch (stmt.type()) {
.context_decl,
.custom_decl,
.distinct_decl,
.enumDecl,
.funcDecl,
.import_stmt,
.objectDecl,
.specialization,
.staticDecl,
.structDecl,
.table_decl,
.typeAliasDecl,
.template => {},
else => {
return chunk.reportError("Top level statement is not allowed from imported module.", @ptrCast(stmt));
}
}
}
}
}
}

// Check for import tasks.
Expand Down
1 change: 1 addition & 0 deletions test/behavior_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ if (!aot) {
run.case2(Config.initFileModules("./test/modules/import_missing_sym_error.cy").withSilent(), "modules/import_missing_sym_error.cy");
run.case2(Config.initFileModules("./test/modules/import_rel_path.cy"), "modules/import_rel_path.cy");
run.case2(Config.initFileModules("./test/modules/import_implied_rel_path.cy"), "modules/import_implied_rel_path.cy");
run.case2(Config.initFileModules("./test/modules/import_stmt_error.cy"), "modules/import_stmt_error.cy");
run.case2(Config.initFileModules("./test/modules/import_unresolved_rel_path.cy"), "modules/import_unresolved_rel_path.cy");

// Import when running main script in the cwd.
Expand Down
9 changes: 9 additions & 0 deletions test/modules/import_stmt_error.cy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use a './test_mods/stmt_error.cy'

--cytest: error
--CompileError: Top level statement is not allowed from imported module.
--
--@AbsPath(test/modules/test_mods/stmt_error.cy):1:1:
--print 123
--^
--
3 changes: 0 additions & 3 deletions test/modules/test_mods/a.cy
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ func fn2(a int, b int):
func barNoExport():
return 234

-- Test that there is no main block execution for imported modules.
panic(.ExecutedModuleMain)

func toInt(val any) int:
return int(val)

Expand Down
1 change: 1 addition & 0 deletions test/modules/test_mods/stmt_error.cy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print 123

0 comments on commit 467ae30

Please sign in to comment.