std meta: fix use of alignOf in meta.cast

This commit is contained in:
Lachlan Easton 2020-09-03 20:16:12 +10:00 committed by Andrew Kelley
parent 39a80cf59e
commit 2a58e30bd5

View File

@ -705,34 +705,34 @@ pub fn Vector(comptime len: u32, comptime child: type) type {
pub fn cast(comptime DestType: type, target: anytype) DestType { pub fn cast(comptime DestType: type, target: anytype) DestType {
const TargetType = @TypeOf(target); const TargetType = @TypeOf(target);
switch (@typeInfo(DestType)) { switch (@typeInfo(DestType)) {
.Pointer => { .Pointer => |dest_ptr| {
switch (@typeInfo(TargetType)) { switch (@typeInfo(TargetType)) {
.Int, .ComptimeInt => { .Int, .ComptimeInt => {
return @intToPtr(DestType, target); return @intToPtr(DestType, target);
}, },
.Pointer => |ptr| { .Pointer => |ptr| {
return @ptrCast(DestType, @alignCast(ptr.alignment, target)); return @ptrCast(DestType, @alignCast(dest_ptr.alignment, target));
}, },
.Optional => |opt| { .Optional => |opt| {
if (@typeInfo(opt.child) == .Pointer) { if (@typeInfo(opt.child) == .Pointer) {
return @ptrCast(DestType, @alignCast(@alignOf(opt.child.Child), target)); return @ptrCast(DestType, @alignCast(dest_ptr, target));
} }
}, },
else => {}, else => {},
} }
}, },
.Optional => |opt| { .Optional => |dest_opt| {
if (@typeInfo(opt.child) == .Pointer) { if (@typeInfo(dest_opt.child) == .Pointer) {
switch (@typeInfo(TargetType)) { switch (@typeInfo(TargetType)) {
.Int, .ComptimeInt => { .Int, .ComptimeInt => {
return @intToPtr(DestType, target); return @intToPtr(DestType, target);
}, },
.Pointer => |ptr| { .Pointer => {
return @ptrCast(DestType, @alignCast(ptr.alignment, target)); return @ptrCast(DestType, @alignCast(@alignOf(dest_opt.child.Child), target));
}, },
.Optional => |target_opt| { .Optional => |target_opt| {
if (@typeInfo(target_opt.child) == .Pointer) { if (@typeInfo(target_opt.child) == .Pointer) {
return @ptrCast(DestType, @alignCast(@alignOf(target_opt.child.Child), target)); return @ptrCast(DestType, @alignCast(@alignOf(dest_opt.child.Child), target));
} }
}, },
else => {}, else => {},