diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2021-04-26 10:50:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-26 10:50:06 +0100 |
| commit | 8d0428a8b399da6e576738d1acbc2c6e172c9fa6 (patch) | |
| tree | 075b0c95dc321649b0cc4c4466894bd78c661ad1 | |
| parent | ff620422fafdbda5684033b8118d6f6611eb62ac (diff) | |
| parent | 86c1aed20d3900b2a5df905a821bf13a074dc102 (diff) | |
Merge pull request #915 from wilsonk/issue-820
Fix for issue 720 (import name is not an identifier)
| -rw-r--r-- | src/checker.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/checker.cpp b/src/checker.cpp index e74c6fc10..3f77210c8 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -3748,6 +3748,19 @@ Array<ImportPathItem> find_import_path(Checker *c, AstPackage *start, AstPackage return empty_path; } #endif + +String get_invalid_import_name(String input) { + isize slash = 0; + for (isize i = input.len-1; i >= 0; i--) { + if (input[i] == '/' || input[i] == '\\') { + break; + } + slash = i; + } + input = substring(input, slash, input.len); + return input; +} + void check_add_import_decl(CheckerContext *ctx, Ast *decl) { if (decl->state_flags & StateFlag_BeenHandled) return; decl->state_flags |= StateFlag_BeenHandled; @@ -3805,7 +3818,14 @@ void check_add_import_decl(CheckerContext *ctx, Ast *decl) { if (id->is_using) { // TODO(bill): Should this be a warning? } else { - error(token, "Import name, %.*s, cannot be use as an import name as it is not a valid identifier", LIT(id->import_name.string)); + if (id->import_name.string == "") { + String invalid_name = id->fullpath; + invalid_name = get_invalid_import_name(invalid_name); + + error(id->token, "Import name %.*s, is not a valid identifier. Perhaps you want to reference the package by a different name like this: import <new_name> \"%.*s\" ", LIT(invalid_name), LIT(invalid_name)); + } else { + error(token, "Import name, %.*s, cannot be use as an import name as it is not a valid identifier", LIT(id->import_name.string)); + } } } else { GB_ASSERT(id->import_name.pos.line != 0); |