@@ -312,16 +312,18 @@ let rec unify_types t1 t2 =
312312 | Some unified -> Some (Array (unified, s1))
313313 | None -> None )
314314
315- (* Pointer types - any pointer can be null *)
315+ (* Null type unification - null can unify with any pointer or function type *)
316+ | Null , Pointer t -> Some (Pointer t) (* null unifies with any pointer *)
317+ | Pointer t , Null -> Some (Pointer t) (* any pointer unifies with null *)
318+ | Null , Function (params , ret ) -> Some (Function (params, ret)) (* null unifies with functions *)
319+ | Function (params , ret ), Null -> Some (Function (params, ret)) (* functions unify with null *)
320+
321+ (* Pointer types *)
316322 | Pointer t1 , Pointer t2 ->
317323 (match unify_types t1 t2 with
318324 | Some unified -> Some (Pointer unified)
319325 | None -> None )
320326
321- (* Special case: null pointer (Pointer U32) can unify with any function type *)
322- | Pointer U32 , Function (params , ret ) -> Some (Function (params, ret))
323- | Function (params , ret ), Pointer U32 -> Some (Function (params, ret))
324-
325327 (* Result types *)
326328 | Result (ok1 , err1 ), Result (ok2 , err2 ) ->
327329 (match unify_types ok1 ok2, unify_types err1 err2 with
@@ -430,7 +432,7 @@ let type_check_literal lit pos =
430432 Str (max 1 len) (* At least size 1 to handle empty strings *)
431433 | CharLit _ -> Char
432434 | BoolLit _ -> Bool
433- | NullLit -> Pointer U32 (* null literal as nullable pointer, can be unified with any pointer type *)
435+ | NullLit -> Null (* null literal - can unify with any pointer or function type *)
434436 | NoneLit -> NoneType (* none literal represents missing/absent values *)
435437 | ArrayLit init_style ->
436438 (* Handle enhanced array literal type checking *)
@@ -1080,7 +1082,8 @@ and type_check_binary_op ctx left op right pos =
10801082 (match resolved_left_type, resolved_right_type with
10811083 | Str _ , Str _ -> Bool (* Allow string comparison regardless of size *)
10821084 (* Null comparisons - any type can be compared with null *)
1083- | _ , Pointer _ | Pointer _ , _ -> Bool
1085+ | Null , _ | _ , Null -> Bool (* Direct null comparisons *)
1086+ | _ , Pointer _ | Pointer _ , _ -> Bool (* Pointer comparisons (legacy) *)
10841087 (* None comparisons - allow with map access expressions or variables that could contain map results *)
10851088 | NoneType , _ | _ , NoneType ->
10861089 (* Check if at least one operand is a map access or could reasonably be a map result *)
0 commit comments