Repository navigation
C error when assigning variable to const int_type ptrΒ #19146
Copy link
Copy link
Closed
Labels
BugThis tag is applied to issues which reports bugs.This tag is applied to issues which reports bugs.Type: SpecificationEverything related to the formal specification of how V should behaveEverything related to the formal specification of how V should behaveUnit: CheckerBugs/feature requests, that are related to the type checker.Bugs/feature requests, that are related to the type checker.Unit: CompilerBugs/feature requests, that are related to the V compiler in general.Bugs/feature requests, that are related to the V compiler in general.
Description
Describe the bug
Builder error:
const x = 3
fn main() {
_ := &x
}Works:
const x = int(3)
fn main() {
_ := &x
}Expected Behavior
Both versions work.
Current Behavior
/tmp/v_1000/const_ptr.13403185457188648864.tmp.c:12442: error: lvalue expected
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
Reproduction Steps
above
Possible Solution
Lines 5311 to 5319 in 815439a
| if typ == ast.int_type { | |
| // TODO: use g.const_decl_write_precomputed here too. | |
| // For now, use #define macros, so existing code compiles | |
| // with -cstrict. Add checker errors for overflows instead, | |
| // so V can catch them earlier, instead of relying on the | |
| // C compiler for that. | |
| g.const_decl_simple_define(mod, name, ct_value.str()) | |
| return true | |
| } |
Additional Information/Context
No response
V version
0.4.0 815439a
Environment details (OS name and version, etc.)
OS: linux, Pop!_OS 22.04 LTS
Processor: 16 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
getwd: /home/turiiya/Dev/vlang/v
vexe: /home/turiiya/Dev/vlang/v/v
vexe mtime: 2023-08-16 05:27:47
vroot: OK, value: /home/turiiya/Dev/vlang/v
VMODULES: OK, value: /home/turiiya/.vmodules
VTMP: OK, value: /tmp/v_1000
Git version: git version 2.34.1
Git vroot status: weekly.2023.33
.git/config present: true
CC version: cc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugThis tag is applied to issues which reports bugs.This tag is applied to issues which reports bugs.Type: SpecificationEverything related to the formal specification of how V should behaveEverything related to the formal specification of how V should behaveUnit: CheckerBugs/feature requests, that are related to the type checker.Bugs/feature requests, that are related to the type checker.Unit: CompilerBugs/feature requests, that are related to the V compiler in general.Bugs/feature requests, that are related to the V compiler in general.
Activity
[-]C error when assigning ptr variable to const int_type[/-][+]C error when assigning variable to const int_type ptr[/+]spytheman commentedon Aug 16, 2023
imho the first case should be a checker error instead.
const x = 3is treated as a literal, and implemented as a C macro.It is substituted everywhere it is used at compile time, and thus does not have a specific address.
const x = int(3)is implemented as a constant global in C, i.e. it has a single location, and thus an address as well.ttytm commentedon Aug 16, 2023
For me a proper error would be satisfying enough.
I think this will also be the solution that will be the simplest to implement while covering the most cases at the same time.