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

xml encoding fails with C compiler error #23429

Closed
louis77 opened this issue Jan 11, 2025 · 2 comments · Fixed by #23432
Closed

xml encoding fails with C compiler error #23429

louis77 opened this issue Jan 11, 2025 · 2 comments · Fixed by #23432
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@louis77
Copy link
Contributor

louis77 commented Jan 11, 2025

Describe the bug

Converting a xml.XMLNode to a string fails with a C compiler error, when the node's children reference a string variable, like shown in the example. However, it works when the string is cloned.

Reproduction Steps

module main

import encoding.xml

fn main() {
	str := "hello"

	node := xml.XMLNode{
		name: "soapenv:Envelope",
		attributes: {
			"xmlns:soapenv": 'http://schemas.xmlsoap.org/soap/envelope/',
			"xmlns:io": 'http://example.org/'
		},
		children: [
			xml.XMLNode{
				name: "soapenv:Header"
			},
			xml.XMLNode{
				name: "soapenv:Body",
				children: [str]
			}
		]
	}

	dump(node.pretty_str("  ", 1, xml.default_entities_reverse))
}

The problem disappears, when the string is cloned instead:

	xml.XMLNode{
		name: "soapenv:Body",
		children: [str.clone()]
	}

Expected Behavior

The encoding should work without a call to string.clone(). This should be handled inside the encoding.xml library.

Current Behavior

The V compiler fails while building with the following message:

% v run xml.v
================== C compilation error (from cc): ==============
cc: /tmp/v_501/xml.01JH9D67CFF9PPBTYQ1R9NGKGJ.tmp.c:8809:878: error: initializing 'encoding__xml__XMLCData *' (aka 'struct encoding__xml__XMLCData *') with an expression of incompatible type 'string' (aka 'struct string')
cc:  8809 |         ,.children = new_array_from_c_array(2, 2, sizeof(encoding__xml__XMLNodeContents), _MOV((encoding__xml__XMLNodeContents[2]){encoding__xml__XMLNode_to_sumtype_encoding__xml__XMLNodeContents(ADDR(encoding__xml__XMLNode, (((encoding__xml__XMLNode){.name = _SLIT("soapenv:Header"),.attributes = new_map(sizeof(string), sizeof(string), &map_hash_string, &map_eq_string, &map_clone_string, &map_free_string),.children = __new_array(0, 0, sizeof(encoding__xml__XMLNodeContents)),})))), encoding__xml__XMLNode_to_sumtype_encoding__xml__XMLNodeContents(ADDR(encoding__xml__XMLNode, (((encoding__xml__XMLNode){.name = _SLIT("soapenv:Body"),.attributes = new_map(sizeof(string), sizeof(string), &map_hash_string, &map_eq_string, &map_clone_string, &map_free_string),.children = new_array_from_c_array(1, 1, sizeof(encoding__xml__XMLNodeContents), _MOV((encoding__xml__XMLNodeContents[1]){string_clone(str)})),}))))})),});
cc:       |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ^~~~~~~~~~~~~~~~~
cc: /tmp/v_501/xml.01JH9D67CFF9PPBTYQ1R9NGKGJ.tmp.c:250:38: note: expanded from macro 'ADDR'
cc:   250 | #define ADDR(type, expr) (&((type[]){expr}[0]))
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 .

Possible Solution

The pretty_str function should handle necessary allocations/cloning of strings.

Additional Information/Context

No response

V version

V 0.4.9 63f5f4a

Environment details (OS name and version, etc.)

|V full version      |V 0.4.9 7af8faf.63f5f4a
|:-------------------|:-------------------
|OS                  |macos, macOS, 15.2, 24C101
|Processor           |8 cpus, 64bit, little endian, Apple M2
|Memory              |0.33GB/16GB
|                    |
|V executable        |/Users/user/Git/nv/v
|V last modified time|2025-01-10 15:29:47
|                    |
|V home dir          |OK, value: /Users/user/Git/nv
|VMODULES            |OK, value: /Users/user/.vmodules
|VTMP                |OK, value: /tmp/v_501
|Current working dir |OK, value: /Users/user/Git/vproj/cmd
|                    |
|Git version         |git version 2.39.5 (Apple Git-154)
|V git status        |weekly.2025.1-29-g63f5f4a6-dirty
|.git/config present |true
|                    |
|cc version          |Apple clang version 16.0.0 (clang-1600.0.26.4)
|gcc version         |Apple clang version 16.0.0 (clang-1600.0.26.4)
|clang version       |Homebrew clang version 18.1.8
|tcc version         |tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
|tcc git status      |thirdparty-macos-arm64 713692d4
|emcc version        |N/A
|glibc version       |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.

@louis77 louis77 added the Bug This tag is applied to issues which reports bugs. label Jan 11, 2025
@felipensp
Copy link
Member

felipensp commented Jan 11, 2025

A workound would be:
children: [xml.XMLNodeContents(str)]

@felipensp
Copy link
Member

Short test case:

type SumFoo = int | string

struct Foo {
	bar []SumFoo
}

struct Bar {
	foo []Foo
}

fn main() {
	str := 'foobar'
	_ := Bar{
		foo: [Foo{
			bar: [str],
		}]
	}	
}

@felipensp felipensp added Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Jan 11, 2025
@felipensp felipensp self-assigned this Jan 11, 2025
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. Status: Confirmed This bug has been confirmed to be valid by a contributor. 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