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 error found - error: lvalue expected when use @[heap] on a struct #21416

Closed
forchid opened this issue May 4, 2024 · 0 comments · Fixed by #21415
Closed

C error found - error: lvalue expected when use @[heap] on a struct #21416

forchid opened this issue May 4, 2024 · 0 comments · Fixed by #21415
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Option Type Bugs/feature requests, that are related to `?Type`. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@forchid
Copy link

forchid commented May 4, 2024

Describe the bug

>v run linkedlist.v
==================
C:/Users/Administrator/AppData/Local/Temp/v_0/linkedlist.01HX1VDT6BBT28Z16ZCX03WCB5.tmp.c:7376: warning: implicit declaration of function 'tcc_backtrace'
C:/Users/Administrator/AppData/Local/Temp/v_0/linkedlist.01HX1VDT6BBT28Z16ZCX03WCB5.tmp.c:16211: error: lvalue expected
...
==================
(Use `v -cg` to print the entire error message)

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 .

Reproduction Steps

V source code

import time
// 41s.
struct Node<T> {
  mut:
    data T
    prev ?&Node<T>
    next ?&Node<T>
}

struct LinkedList<T> {
  mut:
    size usize
    head ?&Node<T>
}

fn new_linked_list[T]() &LinkedList<T> {
  return &LinkedList<T>{}
}

fn (mut li LinkedList<T>) add[T](data T) {
  mut node := &Node<T>{ data, none, none }
  
  if li.head == none {
    li.head = node
    node.next = node
    node.prev = node
  } else {
    node.next = li.head
    node.prev = li.head?.prev
    node.prev?.next = node
    li.head?.prev = node
  }
  
  li.size += 1
}

fn (mut li LinkedList<T>) pop[T]() ?T {
  if li.head == none {
    return none
  }
  if li.size == 1 {
    data := li.head?.data
    li.head?.next = none
    li.head?.prev = none
    li.head = none
    li.size -= 1
    return data
  }
  
  mut tail := li.head?.prev?
  mut curr := tail.prev?
  curr.next = li.head
  li.head?.prev = curr
  
  tail.next = none
  tail.prev = none
  li.size -= 1
  
  return tail.data
}

@[heap] // It's ok if commenting this line
struct Integer {
  mut:
    value int
}

fn (mut i Integer) inc() {
  i.value += 1
}

fn main() {
max_itr := 10
t := time.now()
for itr in 0 .. max_itr {
  mut a := &Integer{1}
  b := a
  a.inc()
  assert a.value == b.value && b.value == 2

  mut list := new_linked_list<Integer>()
  println('Itr#$itr list size: $list.size')
  
  list.add(Integer{10})
  println('Itr#$itr list size: $list.size')
  list.add(Integer{20})
  println('Itr#$itr list size: $list.size')
  
  mut n := list.pop()
  println('Itr#$itr list size: $list.size, data: ${n?}')
  n = list.pop()
  println('Itr#$itr list size: $list.size, data: ${n?}')
  n = list.pop()
  println('Itr#$itr list size: $list.size, data: $n')
  
  max := 1000_0000
  step := 100_0000
  for i in 0 .. max {
    mut item := Integer{i};
    //item.inc()
    list.add(item)
    item.inc()
    assert item.value == i + 1
    if i % step == 0 {
      println('Itr#$itr list add: $list.size, i: $i')
    }
  }
  for i in 0 .. max {
    list.pop()
    //item := list.pop()
    //assert item?.value == max - i
    if i % step == 0 {
      println('Itr#$itr list pop: $list.size, i: $i')
    }
  }
  println('Itr#$itr list size: $list.size')
}
  d := time.since(t)
  println('Bye(time $d)!')
}

Run it

>v run linkedlist.v

Expected Behavior

Run it normally.

Current Behavior

error: lvalue expected.
C error found.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.5 0459519

Environment details (OS name and version, etc.)

>v doctor
V full version: V 0.4.5 937a36a.0459519
OS: windows, Microsoft Windows 10 רҵ v19042 64 λ
Processor: 4 cpus, 64bit, little endian,

getwd: D:\code\lang\vlang\ds
vexe: D:\Programs\v\v.exe
vexe mtime: 2024-05-02 06:28:06

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

Git version: git version 2.33.1.windows.1
Git vroot status: Error: fatal: not a git repository (or any of the parent directories): .git
.git/config present: false

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

thirdparty/tcc: N/A

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.

@forchid forchid added the Bug This tag is applied to issues which reports bugs. label May 4, 2024
@felipensp felipensp self-assigned this May 4, 2024
@felipensp felipensp added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Option Type Bugs/feature requests, that are related to `?Type`. labels May 4, 2024
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. Option Type Bugs/feature requests, that are related to `?Type`. 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.

2 participants