From 49e1012baf67c8af93a6a2b2a4738d09fbf1cc88 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 9 Dec 2024 13:14:44 -0300 Subject: [PATCH] markused: fix short program with selector option unwrapping only (fix #23109) (#23111) --- vlib/v/checker/checker.v | 4 ++++ vlib/v/tests/skip_unused/selector_option.run.out | 1 + .../selector_option.skip_unused.run.out | 1 + vlib/v/tests/skip_unused/selector_option.vv | 15 +++++++++++++++ 4 files changed, 21 insertions(+) create mode 100644 vlib/v/tests/skip_unused/selector_option.run.out create mode 100644 vlib/v/tests/skip_unused/selector_option.skip_unused.run.out create mode 100644 vlib/v/tests/skip_unused/selector_option.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 631cfdab76..0b48afca9e 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1726,6 +1726,10 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type { c.check_or_expr(node.or_block, unwrapped_typ, c.expected_or_type, node) c.expected_or_type = ast.void_type } + if c.pref.skip_unused && node.or_block.kind != .absent + && !c.table.used_features.option_or_result { + c.table.used_features.option_or_result = true + } return field.typ } if mut method := c.table.sym(c.unwrap_generic(typ)).find_method_with_generic_parent(field_name) { diff --git a/vlib/v/tests/skip_unused/selector_option.run.out b/vlib/v/tests/skip_unused/selector_option.run.out new file mode 100644 index 0000000000..0cfbf08886 --- /dev/null +++ b/vlib/v/tests/skip_unused/selector_option.run.out @@ -0,0 +1 @@ +2 diff --git a/vlib/v/tests/skip_unused/selector_option.skip_unused.run.out b/vlib/v/tests/skip_unused/selector_option.skip_unused.run.out new file mode 100644 index 0000000000..0cfbf08886 --- /dev/null +++ b/vlib/v/tests/skip_unused/selector_option.skip_unused.run.out @@ -0,0 +1 @@ +2 diff --git a/vlib/v/tests/skip_unused/selector_option.vv b/vlib/v/tests/skip_unused/selector_option.vv new file mode 100644 index 0000000000..94a1844551 --- /dev/null +++ b/vlib/v/tests/skip_unused/selector_option.vv @@ -0,0 +1,15 @@ +module main + +struct GameObject { +mut: + id int = 1 + parent ?&GameObject +} + +fn main() { + mut gameobject := GameObject{} + gameobject.parent = &GameObject{ + id: 2 + } + println('${gameobject.parent?.id}') +}