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

Generic comparison fails for conditional assignment #19398

Closed
hungrybluedev opened this issue Sep 21, 2023 · 2 comments · Fixed by #19401
Closed

Generic comparison fails for conditional assignment #19398

hungrybluedev opened this issue Sep 21, 2023 · 2 comments · Fixed by #19401
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. 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

@hungrybluedev
Copy link
Member

hungrybluedev commented Sep 21, 2023

V doctor:

V full version: V 0.4.1 52a055b.7169df3
OS: windows, Microsoft Windows 11 Home v22621 64-bit
Processor: 16 cpus, 64bit, little endian, 

getwd: C:\Development\Repositories\VProjects\scratch\generic_clip
vexe: C:\Development\Dependencies\v-main\v.exe
vexe mtime: 2023-09-21 04:50:16

vroot: OK, value: C:\Development\Dependencies\v-main
VMODULES: OK, value: C:\Development\Dependencies\vmodules
VTMP: OK, value: C:\Development\scratch\tmp

Git version: git version 2.42.0.windows.2
Git vroot status: weekly.2023.27-479-g7169df3e
.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

What did you do?
v -g -o vdbg cmd/v && vdbg .\src\main.v

module main

fn clip[N](x []N, min N, max N) []N {
	mut result := []N{cap: x.len}
	for value in x {
		result << if value < min {
			min
		} else if value > max {
			max
		} else {
			value
		}
	}
	return result
}

fn main() {
	a := [1, 2, 3, 4, 5]
	dump(a)
	dump(clip(a, 2, 4))
}

What did you expect to see?

The clipped version of the array: [2, 2, 3, 4, 4]

What did you see instead?

==================
C:/Development/scratch/tmp/main.3925996559115324338.tmp.c:6847: warning: implicit declaration of function 'tcc_backtrace'
C:/Development/scratch/tmp/main.3925996559115324338.tmp.c:12620: error: '{' expected (got ";")
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

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.

@hungrybluedev hungrybluedev added the Bug This tag is applied to issues which reports bugs. label Sep 21, 2023
@spytheman
Copy link
Member

spytheman commented Sep 21, 2023

        mut t := value
        if value < min {
            t = min
        } else if value > max {
            t = max
        }
        result << t

works, generating:

        int t = value;
        if (value < min) {
            t = min;
        } else if (value > max) {
            t = max;
        }
        array_push_noscan((array*)&result, _MOV((int[]){ t }));

The original generated:

_PUSH_MANY_noscan(&result, ((value < min ? (min) : value > max ? (max) : (value))), _t2, Array_int);

i.e. for some reason, it inferred that the value of the if expression is an array of integers, but instead, it is a single integer.

@yuyi98 can you please take a look?

@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Sep 21, 2023
@yuyi98
Copy link
Member

yuyi98 commented Sep 21, 2023

Ok, I'll try.

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. Generics[T] Bugs/feature requests, that are related to the V generics. 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