Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disallow slicing many-item pointer with different sentinel (rev. 2) #22372

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33409,6 +33409,43 @@ fn analyzeSlice(
break :e try sema.coerce(block, Type.usize, uncasted_end, end_src);
} else break :e try sema.coerce(block, Type.usize, uncasted_end_opt, end_src);
}

// when slicing a many-item pointer, if a sentinel `S` is provided as in `ptr[a.. :S]`, it
// must match the sentinel of `@TypeOf(ptr)`.
if (sentinel_opt != .none) sentinel_check: {
const provided = provided: {
const casted = try sema.coerce(block, elem_ty, sentinel_opt, sentinel_src);
try checkSentinelType(sema, block, sentinel_src, elem_ty);
break :provided try sema.resolveConstDefinedValue(block, sentinel_src, casted, .{
.needed_comptime_reason = "slice sentinel must be comptime-known",
});
};

const msg = msg: {
const parent = if (ptr_sentinel) |expected| parent: {
if (expected.eql(provided, elem_ty, zcu)) break :sentinel_check;
break :parent try sema.errMsg(
sentinel_src,
"sentinel-terminated slicing of many-item pointer must preserve sentinel; expected sentinel {}, found {}",
.{
expected.fmtValue(pt),
provided.fmtValue(pt),
},
);
} else parent: {
break :parent try sema.errMsg(
sentinel_src,
"sentinel-terminated slicing is not permitted on many-item pointer without sentinel",
.{},
);
};
errdefer parent.destroy(sema.gpa);

try sema.errNote(src, parent, "use @ptrCast to cast pointer sentinel", .{});
break :msg parent;
};
return sema.failWithOwnedErrorMsg(block, msg);
}
return sema.analyzePtrArithmetic(block, src, ptr, start, .ptr_add, ptr_src, start_src);
};

Expand Down
3 changes: 0 additions & 3 deletions test/behavior/slice.zig
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,6 @@ test "slice syntax resulting in pointer-to-array" {
comptime assert(@TypeOf(ptr[1..][0..2]) == *[2]u8);
comptime assert(@TypeOf(ptr[1..][0..4]) == *[4]u8);
comptime assert(@TypeOf(ptr[1..][0..2 :4]) == *[2:4]u8);
comptime assert(@TypeOf(ptr[1.. :0][0..2]) == *[2]u8);
comptime assert(@TypeOf(ptr[1.. :0][0..4]) == *[4]u8);
comptime assert(@TypeOf(ptr[1.. :0][0..2 :4]) == *[2:4]u8);

var ptr_z: [*:0]u8 = &array;
comptime assert(@TypeOf(ptr_z[1..][0..2]) == *[2]u8);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
comptime {
var ptr: [*]const u8 = undefined;
_ = ptr[0.. :0];
}

comptime {
var ptrz: [*:0]const u8 = undefined;
_ = ptrz[0.. :1];
}

// error
//
// :3:18: error: sentinel-terminated slicing is not permitted on many-item pointer without sentinel
// :3:12: note: use @ptrCast to cast pointer sentinel
// :8:19: error: sentinel-terminated slicing of many-item pointer must preserve sentinel; expected sentinel 0, found 1
// :8:13: note: use @ptrCast to cast pointer sentinel
Loading