Thanks to visit codestin.com
Credit goes to github.com

Skip to content

incorrect defer usage cause error: field not found: field #25148

@kbkpbot

Description

@kbkpbot

Describe the bug

An incorrectly use of defer in a $for loop, will cause C error.

Reproduction Steps

d.v

struct Struct {
        s string
        i int
}

fn main() {
        tst := Struct{
                s: 'tst-s'
                i: 42
        }

        $for field in Struct.fields {
                $if field.typ is string {
                        defer {
                                if tst.$(field.name) == 'tst-s' {
                                        println('found tst-s')
                                }
                        }
                }
        }
}
v d.v

Expected Behavior

a error report about incorrect use of defer.

Current Behavior

$ v d.v
================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/d.01K353S3XNQ49EDW7VZ44K8WG3.tmp.c:1615: error: field not found: field
=================================================================
(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 .

Possible Solution

fix checker or cgen

Additional Information/Context

more discussion can be found at PR #25146

V version

V 0.4.11 9f91064

Environment details (OS name and version, etc.)

V full version V 0.4.11 9f91064
OS linux, Ubuntu 24.04.3 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 12.11GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-08-21 01:24:03
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/mars/v/bug/comptime/j
Git version git version 2.43.0
V git status weekly.2025.17-604-g14a1b59d
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.5) 2.39

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.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.ComptimeFeatures processed during compile time, like $if, $for, $env etcdefer {}Problems related to the `defer{}` implementation.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions