Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler panics when using unary - on enum #4706

Open
prescientmoon opened this issue Jan 17, 2025 · 3 comments
Open

Compiler panics when using unary - on enum #4706

prescientmoon opened this issue Jan 17, 2025 · 3 comments

Comments

@prescientmoon
Copy link

Context

Useful information to add to a bug report:

	Odin:    dev-2025-01
	OS:      NixOS 24.11 (Vicuna), Linux 6.12.1
	CPU:     Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
	RAM:     7823 MiB
	Backend: LLVM 13.0.1

Expected Behavior

The following code should compile

package repro

Chunk_Transform :: struct { side: Side }
Side :: enum {}

main :: proc() {
	first: Side
	side := (Chunk_Transform{-first}).side
}

Current Behavior

Running odin run repro yields the following error

src/llvm_backend_expr.cpp(274): Panic: Unhandled type Side
fish: Job 2, 'odin build repro' terminated by signal SIGILL (Illegal instruction)
@prescientmoon
Copy link
Author

prescientmoon commented Jan 17, 2025

Here's a smaller (and slightly better motivated, perhaps) example that fails with a similar error:

Side :: enum {
	Top    = 1,
	Bottom = -1,
}

main :: proc() {
	side: Side
	side = -side
}

@prescientmoon
Copy link
Author

Hmm, I'm not familiar with the compiler internals, but it looks like enum negation is allowed here, yet this is not handled here?

@prescientmoon
Copy link
Author

prescientmoon commented Jan 17, 2025

I'm not familiar with the codebase, but if anyone is in my situation and wants a temporary (probably incorrect) patch to get around the issue (for now), here it is

diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp
index df9dca801..0d9c77a26 100644
--- a/src/llvm_backend_expr.cpp
+++ b/src/llvm_backend_expr.cpp
@@ -231,11 +231,12 @@ gb_internal lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x,
 		res.type = x.type;
 		return res;
 	case Token_Sub: // Number negation
-		if (is_type_integer(x.type)) {
+		auto ty = core_type(x.type);
+		if (is_type_integer(ty)) {
 			res.value = LLVMBuildNeg(p->builder, x.value, "");
-		} else if (is_type_float(x.type)) {
+		} else if (is_type_float(ty)) {
 			res.value = LLVMBuildFNeg(p->builder, x.value, "");
-		} else if (is_type_complex(x.type)) {
+		} else if (is_type_complex(ty)) {
 			LLVMValueRef v0 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 0, ""), "");
 			LLVMValueRef v1 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 1, ""), "");
 
@@ -245,7 +246,7 @@ gb_internal lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x,
 			LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 1, ""));
 			return lb_addr_load(p, addr);
 
-		} else if (is_type_quaternion(x.type)) {
+		} else if (is_type_quaternion(ty)) {
 			LLVMValueRef v0 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 0, ""), "");
 			LLVMValueRef v1 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 1, ""), "");
 			LLVMValueRef v2 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 2, ""), "");
@@ -265,7 +266,7 @@ gb_internal lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x,
 			} else {
 				res.value = LLVMBuildNeg(p->builder, x.value, "");
 			}
-		} else if (is_type_matrix(x.type)) {
+		} else if (is_type_matrix(ty)) {
 			lbValue zero = {};
 			zero.value = LLVMConstNull(lb_type(p->module, type));
 			zero.type = type;

@prescientmoon prescientmoon changed the title Panic: unhandled type ... Compiler panics when using unary - on enum Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant