Browse Source

Improve error messages for `switch` and `for` r-values with a suggestion

gingerBill 1 year ago
parent
commit
46b9bd8c0e
3 changed files with 60 additions and 5 deletions
  1. 43 0
      src/check_stmt.cpp
  2. 16 5
      src/error.cpp
  3. 1 0
      src/tokenizer.cpp

+ 43 - 0
src/check_stmt.cpp

@@ -474,16 +474,59 @@ gb_internal Type *check_assignment_variable(CheckerContext *ctx, Operand *lhs, O
 		}
 
 		Entity *e = entity_of_node(lhs->expr);
+		Entity *original_e = e;
+
+		Ast *name = unparen_expr(lhs->expr);
+		while (name->kind == Ast_SelectorExpr) {
+			name = name->SelectorExpr.expr;
+			e = entity_of_node(name);
+		}
+		if (e == nullptr) {
+			e = original_e;
+		}
 
 		gbString str = expr_to_string(lhs->expr);
 		if (e != nullptr && e->flags & EntityFlag_Param) {
+			ERROR_BLOCK();
 			if (e->flags & EntityFlag_Using) {
 				error(lhs->expr, "Cannot assign to '%s' which is from a 'using' procedure parameter", str);
 			} else {
 				error(lhs->expr, "Cannot assign to '%s' which is a procedure parameter", str);
 			}
+			error_line("\tSuggestion: Did you mean to pass '%.*s' by pointer?\n", LIT(e->token.string));
+			show_error_on_line(e->token.pos, token_pos_end(e->token));
 		} else {
+			ERROR_BLOCK();
 			error(lhs->expr, "Cannot assign to '%s'", str);
+
+			if (e) if (e->flags & EntityFlag_ForValue) {
+				isize offset = show_error_on_line(e->token.pos, token_pos_end(e->token), "Suggestion:");
+				if (offset < 0) {
+					if (is_type_map(e->type)) {
+						error_line("\tSuggestion: Did you mean? 'for key, &%.*s in ...'\n", LIT(e->token.string));
+					} else {
+						error_line("\tSuggestion: Did you mean? 'for &%.*s in ...'\n", LIT(e->token.string));
+					}
+				} else {
+					error_line("\t");
+					for (isize i = 0; i < offset-1; i++) {
+						error_line(" ");
+					}
+					error_line("'%.*s' is immutable, declare it as '&%.*s' to make it mutable\n", LIT(e->token.string), LIT(e->token.string));
+				}
+
+			} else if (e->flags & EntityFlag_SwitchValue) {
+				isize offset = show_error_on_line(e->token.pos, token_pos_end(e->token), "Suggestion:");
+				if (offset < 0) {
+					error_line("\tSuggestion: Did you mean? 'switch &%.*s in ...'\n", LIT(e->token.string));
+				} else {
+					error_line("\t");
+					for (isize i = 0; i < offset-1; i++) {
+						error_line(" ");
+					}
+					error_line("'%.*s' is immutable, declare it as '&%.*s' to make it mutable\n", LIT(e->token.string), LIT(e->token.string));
+				}
+			}
 		}
 		gb_string_free(str);
 

+ 16 - 5
src/error.cpp

@@ -84,6 +84,7 @@ gb_internal bool set_file_path_string(i32 index, String const &path) {
 	bool ok = false;
 	GB_ASSERT(index >= 0);
 	mutex_lock(&global_error_collector.path_mutex);
+	mutex_lock(&global_files_mutex);
 
 	if (index >= global_file_path_strings.count) {
 		array_resize(&global_file_path_strings, index+1);
@@ -94,6 +95,7 @@ gb_internal bool set_file_path_string(i32 index, String const &path) {
 		ok = true;
 	}
 
+	mutex_unlock(&global_files_mutex);
 	mutex_unlock(&global_error_collector.path_mutex);
 	return ok;
 }
@@ -102,6 +104,7 @@ gb_internal bool thread_safe_set_ast_file_from_id(i32 index, AstFile *file) {
 	bool ok = false;
 	GB_ASSERT(index >= 0);
 	mutex_lock(&global_error_collector.path_mutex);
+	mutex_lock(&global_files_mutex);
 
 	if (index >= global_files.count) {
 		array_resize(&global_files, index+1);
@@ -111,7 +114,7 @@ gb_internal bool thread_safe_set_ast_file_from_id(i32 index, AstFile *file) {
 		global_files[index] = file;
 		ok = true;
 	}
-
+	mutex_unlock(&global_files_mutex);
 	mutex_unlock(&global_error_collector.path_mutex);
 	return ok;
 }
@@ -119,12 +122,14 @@ gb_internal bool thread_safe_set_ast_file_from_id(i32 index, AstFile *file) {
 gb_internal String get_file_path_string(i32 index) {
 	GB_ASSERT(index >= 0);
 	mutex_lock(&global_error_collector.path_mutex);
+	mutex_lock(&global_files_mutex);
 
 	String path = {};
 	if (index < global_file_path_strings.count) {
 		path = global_file_path_strings[index];
 	}
 
+	mutex_unlock(&global_files_mutex);
 	mutex_unlock(&global_error_collector.path_mutex);
 	return path;
 }
@@ -132,12 +137,14 @@ gb_internal String get_file_path_string(i32 index) {
 gb_internal AstFile *thread_safe_get_ast_file_from_id(i32 index) {
 	GB_ASSERT(index >= 0);
 	mutex_lock(&global_error_collector.path_mutex);
+	mutex_lock(&global_files_mutex);
 
 	AstFile *file = nullptr;
 	if (index < global_files.count) {
 		file = global_files[index];
 	}
 
+	mutex_unlock(&global_files_mutex);
 	mutex_unlock(&global_error_collector.path_mutex);
 	return file;
 }
@@ -247,10 +254,10 @@ gb_internal void terminal_reset_colours(void) {
 }
 
 
-gb_internal bool show_error_on_line(TokenPos const &pos, TokenPos end) {
+gb_internal isize show_error_on_line(TokenPos const &pos, TokenPos end, char const *prefix=nullptr) {
 	get_error_value()->end = end;
 	if (!show_error_line()) {
-		return false;
+		return -1;
 	}
 
 	i32 offset = 0;
@@ -270,6 +277,10 @@ gb_internal bool show_error_on_line(TokenPos const &pos, TokenPos end) {
 			MAX_LINE_LENGTH_PADDED = MAX_LINE_LENGTH-MAX_TAB_WIDTH-ELLIPSIS_PADDING,
 		};
 
+		if (prefix) {
+			error_out("\t%s\n\n", prefix);
+		}
+
 		error_out("\t");
 
 		terminal_set_colours(TerminalStyle_Bold, TerminalColour_White);
@@ -328,9 +339,9 @@ gb_internal bool show_error_on_line(TokenPos const &pos, TokenPos end) {
 		terminal_reset_colours();
 
 		error_out("\n");
-		return true;
+		return offset;
 	}
-	return false;
+	return -1;
 }
 
 gb_internal void error_out_empty(void) {

+ 1 - 0
src/tokenizer.cpp

@@ -193,6 +193,7 @@ gb_internal void init_keyword_hash_table(void) {
 
 gb_global Array<String>           global_file_path_strings; // index is file id
 gb_global Array<struct AstFile *> global_files; // index is file id
+gb_global BlockingMutex           global_files_mutex;
 
 gb_internal String   get_file_path_string(i32 index);
 gb_internal struct AstFile *thread_safe_get_ast_file_from_id(i32 index);