Browse Source

Fix `using` issue #62

Ginger Bill 8 years ago
parent
commit
615fa82d1f
2 changed files with 11 additions and 7 deletions
  1. 10 7
      src/check_stmt.c
  2. 1 0
      src/ir.c

+ 10 - 7
src/check_stmt.c

@@ -439,6 +439,8 @@ bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bo
 		return true;
 	}
 
+	add_entity_use(c, expr, e);
+
 	switch (e->kind) {
 	case Entity_Alias: {
 		if (e->Alias.original != NULL) {
@@ -506,17 +508,18 @@ bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bo
 
 	case Entity_Variable: {
 		Type *t = base_type(type_deref(e->type));
-		if (is_type_struct(t) || is_type_raw_union(t)) {
+		if (is_type_struct(t) || is_type_raw_union(t) || is_type_union(t)) {
 			// TODO(bill): Make it work for unions too
-			Scope **found = map_scope_get(&c->info.scopes, hash_pointer(t->Record.node));
-			GB_ASSERT(found != NULL);
-			for_array(i, (*found)->elements.entries) {
-				Entity *f = (*found)->elements.entries.e[i].value;
+			Scope **found_ = map_scope_get(&c->info.scopes, hash_pointer(t->Record.node));
+			GB_ASSERT(found_ != NULL);
+			Scope *found = *found_;
+			for_array(i, found->elements.entries) {
+				Entity *f = found->elements.entries.e[i].value;
 				if (f->kind == Entity_Variable) {
 					Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
-					if (is_selector) {
+					// if (is_selector) {
 						uvar->using_expr = expr;
-					}
+					// }
 					Entity *prev = scope_insert_entity(c->context.scope, uvar);
 					if (prev != NULL) {
 						gbString expr_str = expr_to_string(expr);

+ 1 - 0
src/ir.c

@@ -4696,6 +4696,7 @@ irValue *ir_get_using_variable(irProcedure *proc, Entity *e) {
 	if (pv != NULL) {
 		v = *pv;
 	} else {
+		GB_ASSERT_MSG(e->using_expr != NULL, "%.*s", LIT(name));
 		v = ir_build_addr(proc, e->using_expr).addr;
 	}
 	GB_ASSERT(v != NULL);