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

Undefined ident: b when using mut b := '' after cond := node in [aa.S1, aa.S2] #19413

Closed
yuyi98 opened this issue Sep 22, 2023 · 0 comments · Fixed by #19414
Closed

Undefined ident: b when using mut b := '' after cond := node in [aa.S1, aa.S2] #19413

yuyi98 opened this issue Sep 22, 2023 · 0 comments · Fixed by #19414
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@yuyi98
Copy link
Member

yuyi98 commented Sep 22, 2023

Describe the bug

Undefined ident: b when using mut b := '' after cond := node in [aa.S1, aa.S2]

This is a reproducible example, what actually went wrong was assign_stmt() in gen/c/assign.v.

	mut af := g.is_autofree && !g.is_builtin_mod && node.op == .assign && node.left_types.len == 1
		&& (node.left[0] is ast.Ident || node.left[0] is ast.SelectorExpr)
	// node.left_types[0] in [ast.string_type, ast.array_type] &&
	mut sref_name := ''
	mut type_to_free := ''

If using node.left[0] in [ast.Ident, ast.SelectorExpr] instead of (node.left[0] is ast.Ident || node.left[0] is ast.SelectorExpr)

	mut af := g.is_autofree && !g.is_builtin_mod && node.op == .assign && node.left_types.len == 1
		&& node.left[0] in [ast.Ident, ast.SelectorExpr]
	// node.left_types[0] in [ast.string_type, ast.array_type] &&
	mut sref_name := ''
	mut type_to_free := ''

This will result in a compilation error.

Reproduction Steps

aa/aa.v

module aa

pub type MySumType = S1 | S2

pub struct S1 {}

pub struct S2 {}

tt1.v

import aa

fn main() {
	node := aa.MySumType(aa.S1{})

	cond := node in [aa.S1, aa.S2]
	mut b := 'b'
	mut c := 'c'

	if cond {
		println('${cond} --- ${b} ---- ${c}')
	}
}

Expected Behavior

output:

true --- b ---- c

Current Behavior

PS D:\Test\v\tt1> v run .
tt1.v:7:6: warning: unused variable: `aa.b`
    5 |
    6 |     cond := node in [aa.S1, aa.S2]
    7 |     mut b := 'b'
      |         ^
    8 |     mut c := 'c'
    9 |
tt1.v:8:6: warning: unused variable: `aa.c`
    6 |     cond := node in [aa.S1, aa.S2]
    7 |     mut b := 'b'
    8 |     mut c := 'c'
      |         ^
    9 |
   10 |     if cond {
tt1.v:11:26: error: undefined ident: `b`
    9 |
   10 |     if cond {
   11 |         println('${cond} --- ${b} ---- ${c}')
      |                                ^
   12 |     }
   13 | }

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.1 ed42341

Environment details (OS name and version, etc.)

V full version: V 0.4.1 ed42341
OS: windows, Microsoft Windows 11 Home China v22621 64-bit
Processor: 16 cpus, 64bit, little endian,

getwd: D:\Vlang\v
vexe: D:\Vlang\v\v.exe
vexe mtime: 2023-09-22 13:09:55

vroot: OK, value: D:\Vlang\v
VMODULES: OK, value: C:\Users\yuyi9.vmodules
VTMP: OK, value: C:\Users\yuyi9\AppData\Local\Temp\v_0

Git version: git version 2.37.3.windows.1
Git vroot status: weekly.2023.38-22-ged423416
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 e90c2620

Important

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority
for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@yuyi98 yuyi98 added the Bug This tag is applied to issues which reports bugs. label Sep 22, 2023
@vlang vlang locked and limited conversation to collaborators Sep 22, 2023
@vlang vlang unlocked this conversation Sep 22, 2023
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant