Skip to content

Invalid pointer casts permitted by in-memory coercion to covariant types #15874

@mlugg

Description

@mlugg

Zig Version

0.11.0-dev.3031+f40539e5d

Steps to Reproduce and Observed Behavior

pub fn main() void {
    var e: error{Foo} = error.Foo;
    const p: *error{ Foo, Bar } = &e;
    p.* = error.Bar;
    @import("std").log.info("{}", .{e});
}

Output:
info: error.Bar
This output should be impossible, since e is of type error{Foo} so should not be able to hold the value error.Bar.

Expected Behavior

A compile error should be triggered.

This happens because a pointer *T is allowed to coerce to a pointer *U whenever T is in-memory coercible to U. When the target pointer is not const, this is insufficient: it is also a requirement that U be in-memory coercible to T, so that writes to the pointer cannot assign invalid values to the destination.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions