Skip to content

C error and random values when handling option for aliased fixed array #22927

Closed

Description

@gechandesu

V doctor:

V full version: V 0.4.8 5ec9bb5.9f6150e
OS: linux, Linux version 6.6.8-arch1-1 (linux@archlinux) (gcc (GCC) 13.2.1 20230801, GNU ld (GNU Binutils) 2.41.0) #1 SMP PREEMPT_DYNAMIC Thu, 21 Dec 2023 19:01:01 +0000
Processor: 16 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i5-1240P

getwd: /home/ge/Code/v/netaddr/fixed_array_issues
vexe: /home/ge/.vlang/v
vexe mtime: 2024-11-20 22:55:01

vroot: OK, value: /home/ge/.vlang
VMODULES: OK, value: /home/ge/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.1
Git vroot status: weekly.2024.47-4-g9f6150ee
.git/config present: true

CC version: cc (GCC) 14.2.1 20240910
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

What did you do?
./v -g -o vdbg cmd/v && ./vdbg fixed_array_option_return_handling_right.v

module main

import encoding.binary

pub type Addr = [4]u8

pub fn Addr.from_u32(a u32) Addr {
	mut bytes := [4]u8{}
	binary.big_endian_put_u32_fixed(mut bytes, a)
	return Addr(bytes)
}

pub fn (a Addr) u32() u32 {
	return binary.big_endian_u32_fixed(a)
}

struct Net {
	netaddr   Addr
	broadcast Addr
}

// returns Nth IP-address from the network if exists, else none
fn (n Net) nth(num i64) ?Addr {
	mut addr := Addr{}
	if num >= 0 {
		addr = Addr.from_u32(n.netaddr.u32() + u32(num))
	} else {
		addr = Addr.from_u32(n.broadcast.u32() + u32(num))
	}
	if !(n.netaddr.u32() < addr.u32() && addr.u32() < n.broadcast.u32()) {
		return none
	}
	return addr
}

fn main() {
	net := Net{
		netaddr:   Addr([u8(172), 16, 16, 0]!)
		broadcast: Addr([u8(172), 16, 16, 3]!)
	}
	res1 := net.nth(1) or { Addr([4]u8{}) } // Compiles, but res1 have random value on each run
	res2 := net.nth(1) or { Addr{} } // C error
	assert res1 == [u8(172), 16, 16, 1]!
	assert res2 == [u8(172), 16, 16, 1]!
}

What did you expect to see?

Assertion passed. No C errors.

What did you see instead?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/fixed_array_option_return_handling_right.01JD5WK42AMV2XB24P1JAK56SA.tmp.c:14382: warning: cast between pointer and integer of different size
cc: /tmp/v_1000/fixed_array_option_return_handling_right.01JD5WK42AMV2XB24P1JAK56SA.tmp.c:14403: error: lvalue expected
... (the original output was 3 lines long, and was truncated to 2 lines)
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21369

Activity

  1. changed the title [-]C error when handling option for aliased fixed array[/-] [+]C error and random values when handling option for aliased fixed array[/+] on Nov 20, 2024
  2. gechandesu commented on Nov 20, 2024

    @gechandesu
    ContributorAuthor

    I was in a hurry with the report, the error with the random result was already fixed here #22912 and is also not reproduced in the current example, but I have an example where this behavior persists, I will update the example.

  3. gechandesu commented on Nov 20, 2024

    @gechandesu
    ContributorAuthor

    I was in a hurry with the report, the error with the random result was already fixed here #22912 and is also not reproduced in the current example, but I have an example where this behavior persists, I will update the example.

    Example updated.

  4. self-assigned this
    on Nov 21, 2024
  5. added a commit that references this issue on Nov 21, 2024
    17f962d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions