Skip to content

issue getting index of an array with ref arg #17800

@c7zn

Description

@c7zn

Describe the bug

When trying to access an array, you're able to use a &int as an index by creating a new &int that equals your old &int value. This doesn't usually work as the compiler prevents &int's from being used as an index

pub fn read_int(bytes []u8, mut offset &int) int {
	defer {
		offset += 4
	}

        offset_d := offset
	return bytes[offset_d] | ( int(bytes[offset_d+ 1]) << 8 ) | ( int(bytes[offset_d+ 2]) << 16 ) | ( int(bytes[offset_d + 3]) << 24 )
}

Expected Behavior

error: non-integer index int

Current Behavior

Instead, it returns the value stored at that index.

Reproduction Steps

pub fn read_int(bytes []u8, mut offset &int) int {
	defer {
		offset += 4
	}

        offset_d := offset
	return bytes[offset_d] | ( int(bytes[offset_d+ 1]) << 8 ) | ( int(bytes[offset_d+ 2]) << 16 ) | ( int(bytes[offset_d + 3]) << 24 )
}

The above code results in zero errors, while the below code gives an error.

pub fn read_int(bytes []u8, mut offset &int) int {
	defer {
		offset += 4
	}
	return bytes[offset] | ( int(bytes[offset + 1]) << 8 ) | ( int(bytes[offset + 2]) << 16 ) | ( int(bytes[offset + 3]) << 24 ) // error: non-integer index `int` 
}
reader/file_reader.v:6:14: error: non-integer index `int` (array type `[]u8`)
    4 | 
    5 |
    6 |     return bytes[offset] | ( int(bytes[offset + 1]) << 8 ) | ( int(bytes[offset + 2]) << 16 ) | ( int(bytes[offset + 3]) << 24 )
      |                 ~~~~~~~~
    7 | }
    8 |

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 130f35c.5e48817

Environment details (OS name and version, etc.)

windows, Microsoft Windows 11 Home v22621 64-bit

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions