fix(0117): pointer-to-array indexing auto-derefs
A '*[N]T' receiver in an index expression reached LLVM emission with an unresolved element type and tripped the panic sentinel — no read or write spelling worked. ptrToArrayElem on Lowering recognises the shape; the index READ path GEPs the pointee array through the pointer value and loads the element; the write / compound-assign / lvalue / addr-of-element paths and the expression typer resolve the element type through the same helper (their GEP machinery already handled a pointer base). Kept out of getElementType so slice paths don't half-accept a raw pointer base. Regression: examples/0176 (read, write, compound, element ptr + deref).
This commit is contained in:
@@ -1263,6 +1263,12 @@ pub fn lowerIndexExpr(self: *Lowering, ie: *const ast.IndexExpr) Ref {
|
||||
const idx = self.lowerExpr(ie.index);
|
||||
// Infer element type from the object's slice/array type
|
||||
const obj_ty = self.inferExprType(ie.object);
|
||||
// `*[N]T` receiver auto-derefs (issue 0117): `obj` IS the pointer
|
||||
// value — GEP the pointee array and load the element.
|
||||
if (self.ptrToArrayElem(obj_ty)) |elem| {
|
||||
const gep = self.builder.emit(.{ .index_gep = .{ .lhs = obj, .rhs = idx } }, self.module.types.ptrTo(elem));
|
||||
return self.builder.load(gep, elem);
|
||||
}
|
||||
const elem_ty = self.getElementType(obj_ty);
|
||||
return self.builder.emit(.{ .index_get = .{ .lhs = obj, .rhs = idx } }, elem_ty);
|
||||
}
|
||||
@@ -1843,7 +1849,7 @@ pub fn lowerExpr(self: *Lowering, node: *const Node) Ref {
|
||||
const ie = &uop.operand.data.index_expr;
|
||||
const idx = self.lowerExpr(ie.index);
|
||||
const obj_ty = self.inferExprType(ie.object);
|
||||
const elem_ty = self.getElementType(obj_ty);
|
||||
const elem_ty = self.ptrToArrayElem(obj_ty) orelse self.getElementType(obj_ty);
|
||||
const ptr_ty = self.module.types.ptrTo(elem_ty);
|
||||
// For array targets, use the storage pointer (alloca for a
|
||||
// local, global_addr for a module global) so the resulting
|
||||
|
||||
Reference in New Issue
Block a user