v icon indicating copy to clipboard operation
v copied to clipboard

Sumtypes: V sometimes thinks 1 == &1 (hidden dereferencing that shouldn't be there?)

Open elimisteve opened this issue 4 years ago • 7 comments

$ v doctor
OS: linux, Debian GNU/Linux 9.13 (stretch) (VM)
Processor: 2 cpus, 64bit, little endian, Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
CC version: cc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516

getwd: /home/user/django_projects/v/examples/elimisteve
vmodules: /home/user/.vmodules
vroot: /home/user/django_projects/v
vexe: /home/user/django_projects/v/v
vexe mtime: 2021-09-19 02:02:46
is vroot writable: true
is vmodules writable: true
V full version: V 0.2.4 e76be4b.6799f3a

Git version: git version 2.11.0
Git vroot status: weekly.2021.37-37-g6799f3ac
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 333c14de

What did you do?

$ cat sum_types2.v
type Abc = int | string

fn main() {
	b := 1
	a := Abc(b)

	match a {
		int {
			println(a == &b)  // <-- Notice here we are taking the address of b
		}
		else {}
	}
}

$ v run sum_types2.v
true

What did you expect to see?

Either a compiler error saying I can't compare an int with an address, or false.

What did you see instead?

$ v run sum_types2.v
true

elimisteve avatar Sep 19 '21 02:09 elimisteve

You're not comparing an int with an address. a holds the pointer to the Abc sumtype you created, and you have &b which means a reference (pointer) to b, so... you're comparing a pointer to a pointer.

JalonSolov avatar Sep 19 '21 02:09 JalonSolov

@JalonSolov Nope. Run this to prove it to yourself:

type Abc = int | string

fn main() {
	b := 1
	a := Abc(b)

	match a {
		int {
			println(typeof(a).name) // `a` is of course an int here
			println(a == 1) // Prints `true` (as it should)
			println(a == &b) // Prints `true` even though `&b` is not 1; this is wrong
		}
		else {}
	}
}

elimisteve avatar Sep 19 '21 02:09 elimisteve

@JalonSolov Smart casting ftw: https://github.com/vlang/v/blob/master/doc/docs.md#smart-casting

elimisteve avatar Sep 19 '21 02:09 elimisteve

FYI this is still an issue with V 0.2.4 2a53566

elimisteve avatar Dec 14 '21 03:12 elimisteve

Image Confirm that the code can run normally V 0.4.10 dbc4071

Avey777 avatar Jun 03 '25 08:06 Avey777

@Avey777 The issue said it should be an error as int can't be compared with an address.

Delta456 avatar Jun 03 '25 08:06 Delta456

@Avey777 The issue said it should be an error as int can't be compared with an address.

ok I misunderstood it.

Avey777 avatar Jun 03 '25 14:06 Avey777