Skip to content

pointer math fails to modify alignment of type #1528

Description

@andrewrk
const std = @import("std");

test "aoeu" {
    var arr: [10]u8 align(2) = undefined;
    var x: usize = 1;

    const ptr = ([*]u8)(&arr);
    @compileLog(@typeOf(ptr));
    const ptr2 = ptr + 1;
    @compileLog(@typeOf(ptr2));
    const ptr3 = ptr + x;
    @compileLog(@typeOf(ptr3));
}

expected output:

| [*]align(2) u8
| [*]align(1) u8
| [*]align(1) u8

actual output:

| [*]align(2) u8
| [*]align(2) u8
| [*]align(2) u8

When we add a comptime known value to the pointer, we know what the new alignment should be.

When we add a runtime value to the pointer, it should make the type have alignment 1 no matter what, because the value could have made it unaligned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions