Skip to content

Commit ad168db

Browse files
authored
Sema: disallow @intFromPtr for comptime-only types
1 parent 5c8912d commit ad168db

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/Sema.zig

+11
Original file line numberDiff line numberDiff line change
@@ -9891,6 +9891,17 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
98919891
if (!ptr_ty.isPtrAtRuntime(mod)) {
98929892
return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(mod)});
98939893
}
9894+
const pointee_ty = ptr_ty.childType(mod);
9895+
if (try sema.typeRequiresComptime(ptr_ty)) {
9896+
const msg = msg: {
9897+
const msg = try sema.errMsg(block, ptr_src, "comptime-only type '{}' has no pointer address", .{pointee_ty.fmt(mod)});
9898+
errdefer msg.destroy(sema.gpa);
9899+
const src_decl = mod.declPtr(block.src_decl);
9900+
try sema.explainWhyTypeIsComptime(msg, ptr_src.toSrcLoc(src_decl, mod), pointee_ty);
9901+
break :msg msg;
9902+
};
9903+
return sema.failWithOwnedErrorMsg(block, msg);
9904+
}
98949905
if (try sema.resolveMaybeUndefValIntable(operand)) |operand_val| ct: {
98959906
if (!is_vector) {
98969907
return Air.internedToRef((try mod.intValue(

test/behavior/pointers.zig

-16
Original file line numberDiff line numberDiff line change
@@ -499,22 +499,6 @@ test "ptrCast comptime known slice to C pointer" {
499499
try std.testing.expectEqualStrings(s, std.mem.sliceTo(p, 0));
500500
}
501501

502-
test "intFromPtr on a generic function" {
503-
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
504-
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
505-
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
506-
507-
const S = struct {
508-
fn generic(i: anytype) @TypeOf(i) {
509-
return i;
510-
}
511-
fn doTheTest(a: anytype) !void {
512-
try expect(@intFromPtr(a) != 0);
513-
}
514-
};
515-
try S.doTheTest(&S.generic);
516-
}
517-
518502
test "pointer alignment and element type include call expression" {
519503
const S = struct {
520504
fn T() type {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const x = 42;
2+
const y = @intFromPtr(&x);
3+
pub export fn entry() void {
4+
_ = y;
5+
}
6+
7+
// error
8+
// backend=stage2
9+
// target=native
10+
//
11+
// :2:23: error: comptime-only type 'comptime_int' has no pointer address

0 commit comments

Comments
 (0)