Repository: mun-lang/mun (language: rust)

Issue:
Fix cryptic Option::unwrap() panic message during build
These should (probably) both compile:

```rust
// #1
extern fn extern_fn(a: float, b: float, c: float): float;

pub fn magic(): float {
    extern_fn(22.0, 23.0, 24.0)
}

// #2
extern fn extern_fn(a: float, b: float, c: float): float;

fn magic(): float {
    extern_fn(22.0, 23.0, 24.0)
}
```

But Mun panics [here](https://github.com/mun-lang/mun/blob/master/crates/mun_codegen/src/code_gen/symbols.rs#L146) when building the second, saying:

```
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', crates/mun_codegen/src/code_gen/symbols.rs:146:9
```




Discussion / hints:

Fix patch:
diff --git a/crates/mun_codegen/src/ir/type_table.rs b/crates/mun_codegen/src/ir/type_table.rs
index 6e95a7ba9..0b187cbb1 100644
--- a/crates/mun_codegen/src/ir/type_table.rs
+++ b/crates/mun_codegen/src/ir/type_table.rs
@@ -136,7 +136,7 @@ impl<'a, D: IrDatabase> TypeTableBuilder<'a, D> {
     /// Collects unique `TypeInfo` from the specified function signature and body.
     pub fn collect_fn(&mut self, hir_fn: hir::Function) {
         // Collect type info for exposed function
-        if !hir_fn.data(self.db).visibility().is_private() {
+        if !hir_fn.data(self.db).visibility().is_private() || hir_fn.is_extern(self.db) {
             let fn_sig = hir_fn.ty(self.db).callable_sig(self.db).unwrap();
 
             // Collect argument types


Test patch:
diff --git a/crates/mun_codegen/src/snapshots/test__extern_fn_file_ir.snap b/crates/mun_codegen/src/snapshots/test__extern_fn_file_ir.snap
index 624b71fc1..f5abbe3f1 100644
--- a/crates/mun_codegen/src/snapshots/test__extern_fn_file_ir.snap
+++ b/crates/mun_codegen/src/snapshots/test__extern_fn_file_ir.snap
@@ -6,8 +6,10 @@ expression: "extern fn add(a:int, b:int): int;\nfn main() {\n    add(3,4);\n}"
 source_filename = "main.mun"
 
 %DispatchTable = type { i64 (i64, i64)* }
+%struct.MunTypeInfo = type { [16 x i8], i8 addrspace(4)*, i32, i8, i8 }
 
 @dispatchTable = external global %DispatchTable
+@global_type_table = external global [1 x %struct.MunTypeInfo addrspace(4)*]
 
 define void @main() {
 body:
diff --git a/crates/mun_codegen/src/snapshots/test__extern_fn_group_ir.snap b/crates/mun_codegen/src/snapshots/test__extern_fn_group_ir.snap
index 38abd3eef..cb3729265 100644
--- a/crates/mun_codegen/src/snapshots/test__extern_fn_group_ir.snap
+++ b/crates/mun_codegen/src/snapshots/test__extern_fn_group_ir.snap
@@ -5,7 +5,11 @@ expression: "extern fn add(a:int, b:int): int;\nfn main() {\n    add(3,4);\n}"
 ; ModuleID = 'group_name'
 source_filename = "group_name"
 
+%struct.MunTypeInfo = type { [16 x i8], i8 addrspace(4)*, i32, i8, i8 }
 %DispatchTable = type { i64 (i64, i64)* }
 
+@"type_info::<core::i64>::name" = private unnamed_addr constant [10 x i8] c"core::i64\00"
+@"type_info::<core::i64>" = private unnamed_addr constant %struct.MunTypeInfo { [16 x i8] c"G\13;t\97j8\18\D7M\83`\1D\C8\19%", [10 x i8]* @"type_info::<core::i64>::name", i32 64, i8 8, i8 0 }
+@global_type_table = global [1 x %struct.MunTypeInfo addrspace(4)*] [%struct.MunTypeInfo addrspace(4)* @"type_info::<core::i64>"]
 @dispatchTable = global %DispatchTable zeroinitializer