Skip to content

Commit

Permalink
Fix TODO: the file itself cannot be imported (#393)
Browse files Browse the repository at this point in the history
Co-authored-by: Akuli <[email protected]>
  • Loading branch information
littlewhitecloud and Akuli authored Oct 23, 2023
1 parent e0d9568 commit 3337f75
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions self_hosted/main.jou
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ class Compiler:
if strcmp(self->files[isrc].ast.path, imp->resolved_path) == 0:
src = &self->files[isrc]
break
if src == dest:
fail(imp->location, "the file itself cannot be imported")

assert src != NULL

for exp = src->pending_exports; exp->name[0] != '\0'; exp++:
Expand Down
5 changes: 3 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,13 @@ static void add_imported_symbol(struct FileState *fs, const ExportSymbol *es, As

static void add_imported_symbols(struct CompileState *compst)
{
// TODO: should it be possible for a file to import from itself?
// Should fail with error?
for (struct FileState *to = compst->files.ptr; to < End(compst->files); to++) {
for (AstImport *imp = to->ast.imports.ptr; imp < End(to->ast.imports); imp++) {
struct FileState *from = find_file(compst, imp->resolved_path);
assert(from);
if (from == to) {
fail_with_error(imp->location, "the file itself cannot be imported");
}

for (struct ExportSymbol *es = from->pending_exports; es->name[0]; es++) {
if (command_line_args.verbosity >= 2) {
Expand Down
4 changes: 4 additions & 0 deletions tests/other_errors/import_it_self.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "./import_it_self.jou" # Error: the file itself cannot be imported

def main() -> int:
return 0

0 comments on commit 3337f75

Please sign in to comment.