Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen error - unwrapping an option field in a struct doesn't seem to unwrap it correctly #22892

Closed
el-gringo opened this issue Nov 17, 2024 · 2 comments · Fixed by #22895
Closed
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Option Type Bugs/feature requests, that are related to `?Type`. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@el-gringo
Copy link
Contributor

el-gringo commented Nov 17, 2024

Describe the bug

Unwrapping an option field in a struct doesn't seem to be correctly unwrapped.

Reproduction Steps

Code: https://play.vlang.io/p/5b48070739

struct LayoutTree {
    mut:
        root ?usize
}

fn LayoutTree.new() LayoutTree {
    return LayoutTree {
        root: ?usize(none)
    }
}

fn test1() {
	root := usize(1)
	mut list := []usize {}
	list << root
	println("list: ${list}")
}

fn test2() {
	mut tree := LayoutTree.new()
	mut list := []usize {}
	tree.root = ?usize(1)
	println("tree.root: ${tree.root}")
	if tree.root != none {
		println("is not none: ${tree.root}")
    	list << tree.root
		println("parents ${list}")
	}
}

fn test3() {
	mut tree := LayoutTree.new()
	mut list := []usize {}
	tree.root = ?usize(1)
	// extract root in an other variable before unwrapping
    root := tree.root
	println("tree.root: ${root}")
	if root != none {
		println("is not none: ${root}")
    	list << root
		println("parents ${list}")
	}
}

fn main() {
	test1() // pass
	test2() // fail
	test3() // pass
}

Expected Behavior

Unwrapping an option field in a struct with if should not ask to unwrap it again

Current Behavior

Output:

code.v:26:11: error: unwrapped Option cannot be used in an infix expression
   24 |     if tree.root != none {
   25 |         println("is not none: ${tree.root}")
   26 |         list << tree.root
      |              ~~
   27 |         println("parents ${list}")
   28 |     }
Exited with error status 1

Possible Solution

Extracting the struct option field in a variable, then unwrap it with a if.

struct LayoutTree {
    mut:
        root ?usize
}

fn LayoutTree.new() LayoutTree {
    return LayoutTree {
        root: ?usize(none)
    }
}

fn main() {
	mut tree := LayoutTree.new()
	mut list := []usize {}
	tree.root = ?usize(1)
	// extract root in an other variable before unwrapping
    root := tree.root
	println("tree.root: ${root}")
	if root != none {
		println("is not none: ${root}")
    	list << root
		println("parents ${list}")
	}
}

Additional Information/Context

No response

V version

V 0.4.8 f1333be.5bba92a

Environment details (OS name and version, etc.)

V full version: V 0.4.8 f1333be.5bba92a
OS: linux, Debian GNU/Linux 12 (bookworm) (VM)
Processor: 2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz

getwd: /home/admin/playground
vexe: /home/admin/v/v
vexe mtime: 2024-11-17 18:25:30

vroot: OK, value: /home/admin/v
VMODULES: OK, value: .vmodules
VTMP: OK, value: /tmp/v_0

Git version: git version 2.39.5
Git vroot status: Error: fatal: detected dubious ownership in repository at '/home/admin/v'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v
.git/config present: true

CC version: cc (Debian 12.2.0-14) 12.2.0
emcc version: N/A
thirdparty/tcc status: Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc

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-21335

@el-gringo el-gringo added the Bug This tag is applied to issues which reports bugs. label Nov 17, 2024
@spytheman
Copy link
Member

The original cgen error was caused by this code:

import datatypes { DoublyLinkedList }

pub type LayoutBoxId = usize;
pub struct LayoutBox {
}

pub struct LayoutTree {
    mut:
        root ?LayoutBoxId
        boxes []LayoutBox
}


pub fn LayoutTree.new() LayoutTree {
    return LayoutTree {
        root: ?LayoutBoxId(none),
        boxes: []LayoutBox {},
    }
}

fn main() {
	mut tree := LayoutTree.new()
	tree.root = 1
	println("tree: ${tree}")
	if tree.root != none {
		println("is not none")
		mut parents := DoublyLinkedList[LayoutBoxId] {}
    	parents.push_back(tree.root)
		println("parents ${parents}")
	}
}

From: https://play.vlang.io/p/7fc4324df7

@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Option Type Bugs/feature requests, that are related to `?Type`. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels Nov 17, 2024
@felipensp
Copy link
Member

felipensp commented Nov 17, 2024

The selector option unwrap was not implemented yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Option Type Bugs/feature requests, that are related to `?Type`. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants