Skip to content

C error when assigning variable to const int_type ptrΒ #19146

Closed

Description

@ttytm

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

v/vlib/v/gen/c/cgen.v

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

Activity

  1. added
    BugThis tag is applied to issues which reports bugs.
    on Aug 16, 2023
  2. changed the title [-]C error when assigning ptr variable to const int_type[/-] [+]C error when assigning variable to const int_type ptr[/+] on Aug 16, 2023
  3. spytheman commented on Aug 16, 2023

    @spytheman
    Contributor

    imho the first case should be a checker error instead.

    const x = 3 is 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.

  4. added
    Unit: CheckerBugs/feature requests, that are related to the type checker.
    Unit: CompilerBugs/feature requests, that are related to the V compiler in general.
    Type: SpecificationEverything related to the formal specification of how V should behave
    on Aug 16, 2023
  5. ttytm commented on Aug 16, 2023

    @ttytm
    MemberAuthor

    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.

  6. self-assigned this
    on Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Type: SpecificationEverything related to the formal specification of how V should behaveUnit: CheckerBugs/feature requests, that are related to the type checker.Unit: CompilerBugs/feature requests, that are related to the V compiler in general.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions