| ... | ... | @@ -15249,15 +15249,25 @@ fn analyzeArithmetic( |
| 15249 | 15249 | return sema.failWithInvalidPtrArithmetic(block, src, "pointer-pointer", "subtraction"); |
| 15250 | 15250 | } |
| 15251 | 15251 | |
| 15252 | | // MLUGG TODO: these semantics are insane and matching them is causing my soul to fragment into a thousand pieces |
| 15252 | // TODO: these semantics are really weird. Pointer subtraction works in increments |
| 15253 | // of the pointer child for indexable pointers (excluding pointers to vectors), |
| 15254 | // which makes sense, but we also allow it for arbitrary single-item pointers, which |
| 15255 | // leads to the weird result that subtraction of '*T' works completely differently |
| 15256 | // depending on whether 'T' is an array. That seems dangerous and confusing, and |
| 15257 | // requires the odd logic below. This behavior originally came from a now-removed |
| 15258 | // function `Type.elemType2`, which was removed precisely *because* the thing it did |
| 15259 | // wasn't really well-defined; for that reason, these semantics were probably |
| 15260 | // largely accidental to begin with. We should change the langauge to avoid this |
| 15261 | // confusing behavior. For instance, perhaps pointer subtraction should only work on |
| 15262 | // indexable pointers. |
| 15253 | 15263 | const lhs_elem_ty = ty: { |
| 15254 | 15264 | const ptr_elem_ty = lhs_ty.childType(zcu); |
| 15255 | | if (lhs_ty.ptrSize(zcu) == .one and ptr_elem_ty.isArrayOrVector(zcu)) break :ty ptr_elem_ty.childType(zcu); |
| 15265 | if (lhs_ty.ptrSize(zcu) == .one and ptr_elem_ty.zigTypeTag(zcu) == .array) break :ty ptr_elem_ty.childType(zcu); |
| 15256 | 15266 | break :ty ptr_elem_ty; |
| 15257 | 15267 | }; |
| 15258 | 15268 | const rhs_elem_ty = ty: { |
| 15259 | 15269 | const ptr_elem_ty = rhs_ty.childType(zcu); |
| 15260 | | if (rhs_ty.ptrSize(zcu) == .one and ptr_elem_ty.isArrayOrVector(zcu)) break :ty ptr_elem_ty.childType(zcu); |
| 15270 | if (rhs_ty.ptrSize(zcu) == .one and ptr_elem_ty.zigTypeTag(zcu) == .array) break :ty ptr_elem_ty.childType(zcu); |
| 15261 | 15271 | break :ty ptr_elem_ty; |
| 15262 | 15272 | }; |
| 15263 | 15273 | if (lhs_elem_ty.toIntern() != rhs_elem_ty.toIntern()) { |