Skip to content

Commit

Permalink
handle import same file twice (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Dec 3, 2023
1 parent 36f2183 commit 5132347
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
13 changes: 12 additions & 1 deletion self_hosted/main.jou
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ class Compiler:

for idest = 0; idest < self->nfiles; idest++:
dest = &self->files[idest]
seen_before: FileState** = malloc(sizeof(seen_before[0]) * dest->ast.nimports)

for i = 0; i < dest->ast.nimports; i++:
imp = &dest->ast.imports[i]

Expand All @@ -235,10 +237,17 @@ class Compiler:
if strcmp(self->files[isrc].ast.path, imp->resolved_path) == 0:
src = &self->files[isrc]
break
assert src != NULL

if src == dest:
fail(imp->location, "the file itself cannot be imported")

assert src != NULL
for k = 0; k < i; k++:
if seen_before[k] == src:
message: byte[500]
snprintf(message, sizeof(message), "file \"%s\" is imported twice", imp->specified_path)
fail(imp->location, message)
seen_before[i] = src

for exp = src->pending_exports; exp->name[0] != '\0'; exp++:
if self->verbosity >= 1:
Expand All @@ -249,6 +258,8 @@ class Compiler:
check_ast_and_import_conflicts(&dest->ast, exp)
dest->typectx.add_imported_symbol(exp)

free(seen_before)

for i = 0; i < self->nfiles; i++:
free(self->files[i].pending_exports)
self->files[i].pending_exports = NULL
Expand Down
11 changes: 11 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,22 @@ static void add_imported_symbol(struct FileState *fs, const ExportSymbol *es, As
static void add_imported_symbols(struct CompileState *compst)
{
for (struct FileState *to = compst->files.ptr; to < End(compst->files); to++) {
List(struct FileState *) seen_before = {0};

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 (int i = 0; i < seen_before.len; i++) {
if (seen_before.ptr[i] == from) {
fail_with_error(imp->location, "file \"%s\" is imported twice", imp->specified_path);
}
}
Append(&seen_before, from);

for (struct ExportSymbol *es = from->pending_exports; es->name[0]; es++) {
if (command_line_args.verbosity >= 2) {
const char *kindstr;
Expand All @@ -391,6 +400,8 @@ static void add_imported_symbols(struct CompileState *compst)
add_imported_symbol(to, es, imp);
}
}

free(seen_before.ptr);
}

// Mark all exports as no longer pending.
Expand Down
2 changes: 2 additions & 0 deletions tests/other_errors/double_import.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "stdlib/io.jou"
import "stdlib/io.jou" # Error: file "stdlib/io.jou" is imported twice

0 comments on commit 5132347

Please sign in to comment.