Quellcode durchsuchen

Fix `using import` to work correctly

Ginger Bill vor 7 Jahren
Ursprung
Commit
bbb0e14633
2 geänderte Dateien mit 33 neuen und 25 gelöschten Zeilen
  1. 23 18
      src/check_expr.cpp
  2. 10 7
      src/checker.cpp

+ 23 - 18
src/check_expr.cpp

@@ -2540,18 +2540,10 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_h
 			bool is_not_exported = !is_entity_exported(entity);
 			if (entity->kind == Entity_ImportName) {
 				is_not_exported = true;
-			} else if (!implicit_is_found) {
-				is_not_exported = false;
+			} else if (implicit_is_found) {
+				is_not_exported = !is_overloaded;
 			}
 
-			if (is_not_exported) {
-				gbString sel_str = expr_to_string(selector);
-				error(op_expr, "`%s` is not exported by `%.*s`", sel_str, LIT(import_name));
-				gb_string_free(sel_str);
-				operand->mode = Addressing_Invalid;
-				operand->expr = node;
-				return nullptr;
-			}
 
 			if (is_overloaded) {
 				HashKey key = hash_string(entity_name);
@@ -2567,7 +2559,7 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_h
 					}
 
 					// NOTE(bill): Check to see if it's imported
-					if (map_get(&import_scope->implicit, hash_entity(procs[i]))) {
+					if (is_entity_implicitly_imported(e, procs[i])) {
 						gb_swap(Entity *, procs[i], procs[overload_count-1]);
 						overload_count--;
 						i--; // NOTE(bill): Counteract the post event
@@ -2586,15 +2578,28 @@ Entity *check_selector(Checker *c, Operand *operand, AstNode *node, Type *type_h
 					}
 				}
 
-				if (overload_count > 0 && !skip) {
-					operand->mode              = Addressing_Overload;
-					operand->type              = t_invalid;
-					operand->expr              = node;
-					operand->overload_count    = overload_count;
-					operand->overload_entities = procs;
-					return procs[0];
+				if (!skip) {
+					if (overload_count > 0) {
+						operand->mode              = Addressing_Overload;
+						operand->type              = t_invalid;
+						operand->expr              = node;
+						operand->overload_count    = overload_count;
+						operand->overload_entities = procs;
+						return procs[0];
+					} else {
+						is_not_exported = true;
+					}
 				}
 			}
+
+			if (is_not_exported) {
+				gbString sel_str = expr_to_string(selector);
+				error(op_expr, "`%s` is not exported by `%.*s`", sel_str, LIT(import_name));
+				gb_string_free(sel_str);
+				operand->mode = Addressing_Invalid;
+				operand->expr = node;
+				return nullptr;
+			}
 		}
 	}
 

+ 10 - 7
src/checker.cpp

@@ -787,8 +787,6 @@ Entity *scope_insert_entity(Scope *s, Entity *entity) {
 	}
 
 	if (prev != nullptr && entity->kind == Entity_Procedure) {
-		// if (s->is_global) return prev;
-
 		multi_map_insert(&s->elements, key, entity);
 	} else {
 		map_set(&s->elements, key, entity);
@@ -2520,10 +2518,16 @@ void check_add_import_decl(Checker *c, AstNodeImportDecl *id) {
 		// NOTE(bill): Add imported entities to this file's scope
 		for_array(elem_index, scope->elements.entries) {
 			Entity *e = scope->elements.entries[elem_index].value;
-			if (e->scope == parent_scope) return;
+			if (e->scope == parent_scope) continue;
+
+			if (e->token.string == "get_proc_address") {
+				// gb_printf_err("%.*s %.*s get_proc_address\n", LIT(scope->file->fullpath), LIT(parent_scope->file->fullpath));
+			}
 
-			if (is_entity_exported(e)) {
-				// TODO(bill): Should these entities be imported but cause an error when used?
+			bool implicit_is_found = map_get(&scope->implicit, hash_entity(e)) != nullptr;
+			if (is_entity_exported(e) && !implicit_is_found) {
+				Entity *prev = scope_lookup_entity(parent_scope, e->token.string);
+				// if (prev) gb_printf_err("%.*s\n", LIT(prev->token.string));
 				bool ok = add_entity(c, parent_scope, e->identifier, e);
 				if (ok) map_set(&parent_scope->implicit, hash_entity(e), true);
 			}
@@ -2558,7 +2562,6 @@ void check_add_export_decl(Checker *c, AstNodeExportDecl *ed) {
 		return;
 	}
 
-
 	if (parent_scope->is_global) {
 		error(ed->token, "`export` cannot be used on #shared_global_scope");
 		return;
@@ -2573,7 +2576,7 @@ void check_add_export_decl(Checker *c, AstNodeExportDecl *ed) {
 	// NOTE(bill): Add imported entities to this file's scope
 	for_array(elem_index, scope->elements.entries) {
 		Entity *e = scope->elements.entries[elem_index].value;
-		if (e->scope == parent_scope) return;
+		if (e->scope == parent_scope) continue;
 
 		if (is_entity_kind_exported(e->kind)) {
 			add_entity(c, parent_scope, e->identifier, e);