Sema: disallow @intFromPtr for comptime-only types

This commit is contained in:
Bogdan Romanyuk 2023-10-17 23:05:55 +03:00 committed by GitHub
parent 5c8912d7a4
commit ad168db727
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 16 deletions

View File

@ -9891,6 +9891,17 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
if (!ptr_ty.isPtrAtRuntime(mod)) {
return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(mod)});
}
const pointee_ty = ptr_ty.childType(mod);
if (try sema.typeRequiresComptime(ptr_ty)) {
const msg = msg: {
const msg = try sema.errMsg(block, ptr_src, "comptime-only type '{}' has no pointer address", .{pointee_ty.fmt(mod)});
errdefer msg.destroy(sema.gpa);
const src_decl = mod.declPtr(block.src_decl);
try sema.explainWhyTypeIsComptime(msg, ptr_src.toSrcLoc(src_decl, mod), pointee_ty);
break :msg msg;
};
return sema.failWithOwnedErrorMsg(block, msg);
}
if (try sema.resolveMaybeUndefValIntable(operand)) |operand_val| ct: {
if (!is_vector) {
return Air.internedToRef((try mod.intValue(

View File

@ -499,22 +499,6 @@ test "ptrCast comptime known slice to C pointer" {
try std.testing.expectEqualStrings(s, std.mem.sliceTo(p, 0));
}
test "intFromPtr on a generic function" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
const S = struct {
fn generic(i: anytype) @TypeOf(i) {
return i;
}
fn doTheTest(a: anytype) !void {
try expect(@intFromPtr(a) != 0);
}
};
try S.doTheTest(&S.generic);
}
test "pointer alignment and element type include call expression" {
const S = struct {
fn T() type {

View File

@ -0,0 +1,11 @@
const x = 42;
const y = @intFromPtr(&x);
pub export fn entry() void {
_ = y;
}
// error
// backend=stage2
// target=native
//
// :2:23: error: comptime-only type 'comptime_int' has no pointer address