diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index c3ecab455..771701428 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -326,12 +326,19 @@ bool parse_module(Context *context) bool is_private = try_consume(context, TOKEN_PRIVATE); + if (TOKEN_IS(TOKEN_STRING)) + { + SEMA_TOKEN_ERROR(context->tok, "'module' should be followed by a plain identifier, not a string. Did you accidentally put the module name between \"\"?"); + return false; + } + if (!TOKEN_IS(TOKEN_IDENT)) { SEMA_TOKEN_ERROR(context->tok, "Module statement should be followed by the name of the module."); return false; } + Path *path = parse_module_path(context); // Expect the module name @@ -1949,6 +1956,11 @@ static inline bool parse_import(Context *context) if (!TOKEN_IS(TOKEN_IDENT)) { + if (TOKEN_IS(TOKEN_STRING)) + { + SEMA_TOKEN_ERROR(context->tok, "An import should be followed by a plain identifier, not a string. Did you accidentally put the module name between \"\"?"); + return false; + } SEMA_TOKEN_ERROR(context->tok, "Import statement should be followed by the name of the module to import."); return false; } @@ -2238,6 +2250,9 @@ Decl *parse_top_level_statement(Context *context) SEMA_TOKEN_ERROR(context->tok, "Compile time constant unexpectedly found."); } return poisoned_decl; + case TOKEN_IMPORT: + SEMA_TOKEN_ERROR(context->tok, "Imports are only allowed directly after the module declaration."); + return poisoned_decl; default: // We could have included all fundamental types above, but do it here instead. if (!token_is_type(context->tok.type)) diff --git a/test/test_suite/import/import_error_out_of_order.c3 b/test/test_suite/import/import_error_out_of_order.c3 new file mode 100644 index 000000000..39dd94b1b --- /dev/null +++ b/test/test_suite/import/import_error_out_of_order.c3 @@ -0,0 +1,5 @@ +module foo; + +func void hello() {} + +import bar; // #error: Imports are only allowed directly after the module declaration \ No newline at end of file diff --git a/test/test_suite/import/import_error_string.c3 b/test/test_suite/import/import_error_string.c3 new file mode 100644 index 000000000..1810dcfe3 --- /dev/null +++ b/test/test_suite/import/import_error_string.c3 @@ -0,0 +1 @@ +import "Hello my friend"; // #error: An import should be followed by a plain identifier, not a string. Did you accidentally put the module name between ""? \ No newline at end of file diff --git a/test/test_suite/module/module_error_string.c3 b/test/test_suite/module/module_error_string.c3 new file mode 100644 index 000000000..ceb869c56 --- /dev/null +++ b/test/test_suite/module/module_error_string.c3 @@ -0,0 +1 @@ +module "Hello my friend"; // #error: 'module' should be followed by a plain identifier, not a string. Did you accidentally put the module name between ""? \ No newline at end of file