mirror of
https://github.com/ziglang/zig.git
synced 2025-12-27 00:23:22 +00:00
Sema: disallow @intFromPtr for comptime-only types
This commit is contained in:
parent
5c8912d7a4
commit
ad168db727
11
src/Sema.zig
11
src/Sema.zig
@ -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(
|
||||
|
||||
@ -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 {
|
||||
|
||||
11
test/cases/compile_errors/@intFromPtr_with_bad_type.zig
Normal file
11
test/cases/compile_errors/@intFromPtr_with_bad_type.zig
Normal 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
|
||||
Loading…
x
Reference in New Issue
Block a user