aboutsummaryrefslogtreecommitdiff
path: root/src/Type.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2025-01-20 13:34:47 +0000
committerMatthew Lugg <mlugg@mlugg.co.uk>2025-01-21 00:33:32 +0000
commit8bcb578507908e17e2081bb03f8c51eb508d51db (patch)
tree1e9caba23ae65432f8aa5abaa55f070e2b5220eb /src/Type.zig
parentb9198b708f8accb41fdb3f11bf635d63fbff461d (diff)
downloadzig-8bcb578507908e17e2081bb03f8c51eb508d51db.tar.gz
zig-8bcb578507908e17e2081bb03f8c51eb508d51db.zip
Sema: fix `is_non_null_ptr` handling for runtime-known pointers
We can still often determine a comptime result based on the type, even if the pointer is runtime-known. Also, we previously used load -> is non null instead of AIR `is_non_null_ptr` if the pointer is comptime-known, but that's a bad heuristic. Instead, we should check for the pointer to be comptime-known, *and* for the load to be comptime-known, and only in that case should we call `Sema.analyzeIsNonNull`. Resolves: #22556
Diffstat (limited to 'src/Type.zig')
-rw-r--r--src/Type.zig10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/Type.zig b/src/Type.zig
index c6c4334270..b628e0c60e 100644
--- a/src/Type.zig
+++ b/src/Type.zig
@@ -4114,6 +4114,16 @@ pub fn containerTypeName(ty: Type, ip: *const InternPool) InternPool.NullTermina
};
}
+/// Returns `true` if a value of this type is always `null`.
+/// Returns `false` if a value of this type is neve `null`.
+/// Returns `null` otherwise.
+pub fn isNullFromType(ty: Type, zcu: *const Zcu) ?bool {
+ if (ty.zigTypeTag(zcu) != .optional and !ty.isCPtr(zcu)) return false;
+ const child = ty.optionalChild(zcu);
+ if (child.zigTypeTag(zcu) == .noreturn) return true; // `?noreturn` is always null
+ return null;
+}
+
pub const @"u1": Type = .{ .ip_index = .u1_type };
pub const @"u8": Type = .{ .ip_index = .u8_type };
pub const @"u16": Type = .{ .ip_index = .u16_type };