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. mut type cast of unwrapped interface #23017

Closed
phcreery opened this issue Nov 30, 2024 · 0 comments · Fixed by #23056
Closed

C error. mut type cast of unwrapped interface #23017

phcreery opened this issue Nov 30, 2024 · 0 comments · Fixed by #23056
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@phcreery
Copy link
Contributor

phcreery commented Nov 30, 2024

Describe the bug

src/message/message.v

module message

pub struct Message {
pub mut:
	text string = 'Init'
	m2   ?&Message
}

pub fn (mut m Message) update_text(new_text string) {
	m.text = new_text
}

pub fn (mut m Message) update_ext_text(new_text string) {
	m.m2?.text = new_text
}

src/main.v

module main

import time
import message

// Build this example with `v -live .`

type LocalMessageType = message.Message

interface IMessage {
mut:
	text string
	update_text(new_text string)
}

struct App {
mut:
	message &IMessage
}

pub fn (mut m LocalMessageType) update_text_externally(new_text string) {
	m.text = new_text
}

@[live]
fn print_message(mut mdef IMessage) {
	if mut mdef is message.Message {
		// mut m := LocalMessageType(*mdef) // works. with -live: Unhandled Exception 0xC0000005
		// mut m := &LocalMessageType(mdef) // works, warning. with -live: Unhandled Exception 0xC0000005
		mut m := LocalMessageType(mdef) // does not work, C compilation error

		dump(mdef)
		dump(*mdef)
		dump(m)

		m.update_text('Updated within')
		m.update_ext_text('Updated ext')
		m.update_text_externally('Updated externally')
		println('${m.text}, ${m.m2?.text}')
	}
}

fn main() {
	for {
		mut m2 := &message.Message{
			text: 'Init External'
		}
		mut mdef := &message.Message{
			text: 'Init'
			m2:   m2
		}
		mut app := &App{
			message: mdef
		}
		print_message(mut app.message)
		time.sleep(500 * time.millisecond)
	}
}
> v run .      
INFO: compile with `v -live . `, if you want to use the @[live] function main.print_message .
================== C compilation error (from tcc): ==============
cc: C:/Users/phcre/AppData/Local/Temp/v_0/vtests.01JDYPQX81M8CS5V5Q38G0PNJC.tmp.c:7803: warning: implicit declaration of function 'tcc_backtrace'
cc: C:/Users/phcre/AppData/Local/Temp/v_0/vtests.01JDYPQX81M8CS5V5Q38G0PNJC.tmp.c:17195: error: cannot convert 'struct src__message__Message *' to 'struct src__message__Message'
... (the original output was 3 lines long, and was truncated to 2 lines)
=================================================================
(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 .

Reproduction Steps

Type cast an unwrapped interface

Expected Behavior

A checker warning

Current Behavior

C compile error

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 5092626.e4d24e8

Environment details (OS name and version, etc.)

> v doctor
V full version: V 0.4.8 5092626.e4d24e8
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 8 cpus, 64bit, little endian, 

getwd: C:\Users\phcre\Documents\v\vtests
vexe: C:\Users\phcre\Documents\v\v\v.exe
vexe mtime: 2024-11-24 01:17:49

vroot: OK, value: C:\Users\phcre\Documents\v\v
VMODULES: OK, value: C:\Users\phcre\.vmodules
VTMP: OK, value: C:\Users\phcre\AppData\Local\Temp\v_0

Git version: git version 2.24.1.windows.2
Git vroot status: weekly.2024.37-553-ge4d24e89 (40 commit(s) behind V master)
.git/config present: true

CC version: cc (Rev6, Built by MSYS2 project) 13.1.0
emcc version: emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.44 (bec42dac7873903d09d713963e34020c22a8bd2d)
thirdparty/tcc status: thirdparty-windows-amd64 b425ac82

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.

Huly®: V_0.6-21458

@phcreery phcreery added the Bug This tag is applied to issues which reports bugs. label Nov 30, 2024
@phcreery phcreery changed the title C error. type cast of interface C error. type cast of unwrapped interface Nov 30, 2024
@phcreery phcreery changed the title C error. type cast of unwrapped interface C error. mut type cast of unwrapped interface Nov 30, 2024
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Dec 1, 2024
@felipensp felipensp self-assigned this Dec 3, 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. 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