Mettle

M0120Pointer cast to an integer and back to a pointer

Reported by the compiler as an error or warning.

mettle explain M0120

A pointer is cast to an integer and straight back to a pointer. The integer in the middle holds the same address the pointer already held, so nothing about the value changes. What changes is what the compiler can say about it: an integer has no provenance, so the borrow checker, the alias analysis and --verify all have to give up on where that address came from.

Example

var p: float32* = (float32*)((int64)(&(buf[0])));  // M0120
var q: float32* = &(buf[0]);                       // the same value

Lowering sees through the round trip, so the analyses are not blinded while the spelling is cleaned up. The value is not wrong; the detour is.

Neither half is reported on its own, because each is sometimes the only way to say a thing. An integer that really is an address -- a handle from the operating system, a device pointer -- becomes a pointer by cast. A pointer becomes an integer to be printed, hashed or aligned. Only the round trip carries no information.

Related