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

C compilation bug #23356

Closed
mobiuscog opened this issue Jan 3, 2025 · 3 comments · Fixed by #23357
Closed

C compilation bug #23356

mobiuscog opened this issue Jan 3, 2025 · 3 comments · Fixed by #23357
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@mobiuscog
Copy link

mobiuscog commented Jan 3, 2025

V doctor:

V full version: V 0.4.9 7b9b3dd.5eecd04
OS: macos, macOS, 15.2, 24C101
Processor: 20 cpus, 64bit, little endian, Apple M1 Ultra

getwd: /Volumes/Data/code/v/test/src
vexe: /Volumes/Data/code/v/v/v
vexe mtime: 2025-01-03 18:03:15

vroot: OK, value: /Volumes/Data/code/v/v
VMODULES: OK, value: /Users/mobius/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.5 (Apple Git-154)
Git vroot status: weekly.2024.53-18-g5eecd04e
.git/config present: true

CC version: Apple clang version 16.0.0 (clang-1600.0.26.6)
emcc version: N/A
thirdparty/tcc: N/A

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

module main

const image_width = 800
const image_height = 400


struct Colour {
	r u8
	g u8
	b u8
}
const buffer_size = image_width * image_height * sizeof[Colour]()
// Declare a type so we don't have to keep passing the size around
type ImageBuffer = [buffer_size]Colour

fn main() {
	println('Hello World!')
}

What did you see?

================== C compilation error (from cc): ==============
cc: /tmp/v_501/main.01JGPNDXBD2EJD25JFEWYY6MYB.tmp.c:1092:9: error: unknown type name 'Array_fixed_main__Colour_0'
cc:  1092 | typedef Array_fixed_main__Colour_0 main__ImageBuffer;
cc:       |         ^
cc: 1 error generated.
================================================================
(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 .

What did you expect to see?

No errors

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

@felipensp felipensp self-assigned this Jan 3, 2025
@felipensp felipensp added Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Jan 3, 2025
@jorgeluismireles
Copy link

At least there is a C error problem here because [x]Colour needs x to be a constant without pre-calculations.
Next program works:

struct Colour{}
//const x = 800*1000*3
const x = 2_400_000
type ImageBuffer = [x]Colour

But if we uncomment the const x = 800*1000*3 line we got this error:

Running code...
Can't run code. The server returned an error:
/tmp/v_60000/code.01JGPQSKYVJQNSJQDRXP1P8VGT.tmp.c:1088: error: ';' expected (got "main__ImageBuffer")
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 .
Exited with error status 1
Please try again.

See https://play.vlang.io/p/f29d3067b2

@JalonSolov
Copy link
Contributor

The problem (with the current compiler) is that the size of a fixed array must be known at compile time. Calculating const values doesn't happen until runtime (specifically, in the _vinit() routine).

Values such as those shown above are reduced at compile time, but not until the TRANSFORM step, which is long after the compile time evaluation has been done.

So this particular issue brings up 2 things:

  1. V should give a clean error message, instead of allowing it to fall through to a C error.

  2. V should be able to do this type of simple transformation before the value is used for something like a fixed array size.

@jorgeluismireles
Copy link

jorgeluismireles commented Jan 3, 2025

  1. V should be able to do this type of simple transformation before the value is used
    for something like a fixed array size.

Or used for example to set the values of an enum

const x = 1*2
const y = 1*2
enum Num {
	x = x
	y = y
}
println('${Num.x:d} ${Num.y:d}') // prints 2 2

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. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants