Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29297,7 +29297,44 @@ fn analyzeSlice(
array_ty = double_child_ty;
elem_ty = double_child_ty.childType();
} else {
return sema.fail(block, src, "slice of single-item pointer", .{});
const emit_note = emit_note: {
const start_val = try sema.resolveDefinedValue(block, start_src, uncasted_start) orelse {
break :emit_note false;
};
const start = try start_val.getUnsignedIntAdvanced(sema.mod.getTarget(), sema) orelse {
break :emit_note false;
};
const end = end: {
if (uncasted_end_opt == .none) {
break :end 1;
} else {
const end_val = try sema.resolveDefinedValue(block, end_src, uncasted_end_opt) orelse {
break :emit_note false;
};
break :end try end_val.getUnsignedIntAdvanced(sema.mod.getTarget(), sema) orelse {
break :emit_note false;
};
}
};
break :emit_note (start == 0) and (end == 1);
};
if (emit_note) {
const msg = msg: {
const msg = try sema.errMsg(block, src, "slice of single-item pointer", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(
block,
src,
msg,
"single-item pointer can be coerced to array using '@as(*[1]{}, ptr)'",
.{ptr_ptr_child_ty.childType().fmt(mod)},
);
break :msg msg;
};
return sema.failWithOwnedErrorMsg(msg);
} else {
return sema.fail(block, src, "slice of single-item pointer", .{});
}
}
},
.Many, .C => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export fn entry(ptr: *i32) void {
const slice = ptr[0..];
_ = slice;
}

// error
// backend=stage2
// target=native
//
// :2:22: error: slice of single-item pointer
// :2:22: note: single-item pointer can be coerced to array using '@as(*[1]i32, ptr)'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*[1]i32 is not an array, it is a pointer to an array. Compile errors must be pedantically precise.