| author | |
| committer | |
| log | ad168db727a1baec8b8efe0bb4418d0df7e84769 |
| tree | 4e1eea7d3dd70e18201d4111fa3bffed61a7356a |
| parent | 5c8912d7a445cbafba92913bdd364f9f02d18d87 |
| signature |
3 files changed, 22 insertions(+), 16 deletions(-)
src/Sema.zig+11| ... | ... | @@ -9891,6 +9891,17 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 9891 | 9891 | if (!ptr_ty.isPtrAtRuntime(mod)) { |
| 9892 | 9892 | return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(mod)}); |
| 9893 | 9893 | } |
| 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 | } | |
| 9894 | 9905 | if (try sema.resolveMaybeUndefValIntable(operand)) |operand_val| ct: { |
| 9895 | 9906 | if (!is_vector) { |
| 9896 | 9907 | return Air.internedToRef((try mod.intValue( |
test/behavior/pointers.zig-16| ... | ... | @@ -499,22 +499,6 @@ test "ptrCast comptime known slice to C pointer" { |
| 499 | 499 | try std.testing.expectEqualStrings(s, std.mem.sliceTo(p, 0)); |
| 500 | 500 | } |
| 501 | 501 | |
| 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 | ||
| 518 | 502 | test "pointer alignment and element type include call expression" { |
| 519 | 503 | const S = struct { |
| 520 | 504 | fn T() type { |
test/cases/compile_errors/@intFromPtr_with_bad_type.zig created+11| ... | ... | @@ -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 |