v icon indicating copy to clipboard operation
v copied to clipboard

json: Compile crash when json.encode

Open brandonpille opened this issue 2 years ago • 4 comments

Describe the bug

C compile crash saying: error: struct or union expected on the code below on line 7877. I see that it is trying to access a property via the "." instead of "->" while it should use "->". This code was working fine yesterday and fails since I upgraded to latest v version.

This is the Node struct:

pub enum PlatformType {
	unknown
	osx
	ubuntu
	alpine
}

pub enum CPUType {
	unknown
	intel
	arm
	intel32
	arm32
}

[heap]
pub struct Node {
mut:
	executor Executor        [skip; str: skip]
	factory  &BuilderFactory [skip; str: skip]
pub:
	name string = 'mymachine'
pub mut:
	platform    PlatformType
	cputype     CPUType
	done        map[string]string
	environment map[string]string
	params      Params
}

image

Expected Behavior

See description

Current Behavior

See description

Reproduction Steps

See description

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 6e1e406

Environment details (OS name and version, etc.)

V full version: V 0.3.3 5454562.6e1e406 OS: linux, Ubuntu 22.04.1 LTS Processor: 16 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz

getwd: /work/crystallib vexe: /work/v/v vexe mtime: 2023-03-17 10:44:32

vroot: OK, value: /work/v VMODULES: OK, value: /home/brandon/.vmodules VTMP: OK, value: /tmp/v_1000

Git version: git version 2.34.1 Git vroot status: weekly.2023.10-70-g6e1e4062 .git/config present: true

CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

brandonpille avatar Mar 17 '23 11:03 brandonpille

Can you post a link to the complete V source? Or create a minimal example and paste it here?

Without a complete compilable example, it is extremely hard to reproduce an issue, let alone attempt to fix it.

JalonSolov avatar Mar 17 '23 11:03 JalonSolov

module main

import json

pub enum PlatformType {
	unknown
	osx
	ubuntu
	alpine
}

pub enum CPUType {
	unknown
	intel
	arm
	intel32
	arm32
}

[heap]
pub struct Node {
pub:
	name string = 'mymachine'
pub mut:
	platform    PlatformType
	cputype     CPUType
	done        map[string]string
	environment map[string]string
}

pub fn (mut node Node) save() ! {
	data :=  json.encode(node)
}

fn main() {
	mut n := Node {
		platform: .osx
		cputype: .unknown
	}
	n.save() or {
		panic(err)
	}
}

brandonpille avatar Mar 17 '23 12:03 brandonpille

With V 0.3.3 6e1e406 I get the following:

$ v -g foo.v
foo.v:32:2: warning: unused variable: `data`
   30 | 
   31 | pub fn (mut node Node) save() ! {
   32 |     data :=  json.encode(node)
      |     ~~~~
   33 | }
   34 |
/tmp/v_1000/foo.14519348190291310068.tmp.c:2286: error: struct or union expected
builder error: 
==================
C error. This should never happen.

JalonSolov avatar Mar 17 '23 12:03 JalonSolov

Yes. When I look at the c code the fields platform and cputype are accessed with "." instead of "->"

brandonpille avatar Mar 17 '23 12:03 brandonpille