value: implement Value.eqlAdvanced on slices

* Support comparison between implicitly casted array pointer and slice.
 * Support comparison between slices with different value tags.

Closes #12700
This commit is contained in:
Jacob Young
2022-10-17 22:10:22 -04:00
committed by Veikka Tuominen
parent edc842ff18
commit 687a7d38a0
2 changed files with 36 additions and 0 deletions

View File

@@ -2260,6 +2260,28 @@ pub const Value = extern union {
}
return true;
},
.Pointer => switch (ty.ptrSize()) {
.Slice => {
const a_len = switch (a_ty.ptrSize()) {
.Slice => a.sliceLen(mod),
.One => a_ty.childType().arrayLen(),
else => unreachable,
};
if (a_len != b.sliceLen(mod)) {
return false;
}
var ptr_buf: Type.SlicePtrFieldTypeBuffer = undefined;
const ptr_ty = ty.slicePtrFieldType(&ptr_buf);
const a_ptr = switch (a_ty.ptrSize()) {
.Slice => a.slicePtr(),
.One => a,
else => unreachable,
};
return try eqlAdvanced(a_ptr, ptr_ty, b.slicePtr(), ptr_ty, mod, sema_kit);
},
.Many, .C, .One => {},
},
.Struct => {
// A struct can be represented with one of:
// .empty_struct_value,

View File

@@ -382,3 +382,17 @@ test "generic struct as parameter type" {
try S.doTheTest(u32, .{ .int = 123 });
try S.doTheTest2(i32, .{ .int = 456 });
}
test "slice as parameter type" {
const S = struct {
fn internComptimeString(comptime str: []const u8) *const []const u8 {
return &struct {
const intern: []const u8 = str;
}.intern;
}
};
const source_a = "this is a string";
try expect(S.internComptimeString(source_a[1..2]) == S.internComptimeString(source_a[1..2]));
try expect(S.internComptimeString(source_a[2..4]) != S.internComptimeString(source_a[5..7]));
}