Skip to content

Commit 3ed9155

Browse files
Rexicon226mlugg
authored andcommitted
Sema: simplify comptime @intFromPtr logic
1 parent ef35c3d commit 3ed9155

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/Sema.zig

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,19 +2287,6 @@ fn resolveValueResolveLazy(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Value
22872287
return try sema.resolveLazyValue((try sema.resolveValue(inst)) orelse return null);
22882288
}
22892289

2290-
/// Like `resolveValue`, but any pointer value which does not correspond
2291-
/// to a comptime-known integer (e.g. a decl pointer) returns `null`.
2292-
/// Lazy values are recursively resolved.
2293-
fn resolveValueIntable(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Value {
2294-
const val = (try sema.resolveValue(inst)) orelse return null;
2295-
if (sema.pt.zcu.intern_pool.getBackingAddrTag(val.toIntern())) |addr| switch (addr) {
2296-
.nav, .uav, .comptime_alloc, .comptime_field => return null,
2297-
.int => {},
2298-
.eu_payload, .opt_payload, .arr_elem, .field => unreachable,
2299-
};
2300-
return try sema.resolveLazyValue(val);
2301-
}
2302-
23032290
/// Value Tag may be `undef` or `variable`.
23042291
pub fn resolveFinalDeclValue(
23052292
sema: *Sema,
@@ -10144,14 +10131,19 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
1014410131
}
1014510132
const len = if (is_vector) operand_ty.vectorLen(zcu) else undefined;
1014610133
const dest_ty: Type = if (is_vector) try pt.vectorType(.{ .child = .usize_type, .len = len }) else .usize;
10147-
if (try sema.resolveValueIntable(operand)) |operand_val| ct: {
10134+
10135+
if (try sema.resolveValue(operand)) |operand_val| ct: {
1014810136
if (!is_vector) {
1014910137
if (operand_val.isUndef(zcu)) {
1015010138
return Air.internedToRef((try pt.undefValue(Type.usize)).toIntern());
1015110139
}
10140+
const addr = try operand_val.getUnsignedIntSema(pt) orelse {
10141+
// Wasn't an integer pointer. This is a runtime operation.
10142+
break :ct;
10143+
};
1015210144
return Air.internedToRef((try pt.intValue(
1015310145
Type.usize,
10154-
(try operand_val.toUnsignedIntSema(pt)),
10146+
addr,
1015510147
)).toIntern());
1015610148
}
1015710149
const new_elems = try sema.arena.alloc(InternPool.Index, len);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
comptime {
2+
const num: usize = 4;
3+
const y: ?*const usize = #
4+
_ = @intFromPtr(y);
5+
}
6+
7+
// error
8+
// backend=stage2
9+
// target=native
10+
//
11+
// :4:9: error: unable to evaluate comptime expression
12+
// :4:21: note: operation is runtime due to this operand
13+
// :1:1: note: 'comptime' keyword forces comptime evaluation

0 commit comments

Comments
 (0)