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

Checker: wrong cast on methods mutable params #20027

Closed
enghitalo opened this issue Nov 28, 2023 · 2 comments · Fixed by #20134
Closed

Checker: wrong cast on methods mutable params #20027

enghitalo opened this issue Nov 28, 2023 · 2 comments · Fixed by #20134
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@enghitalo
Copy link
Contributor

enghitalo commented Nov 28, 2023

Describe the bug

I think it is a method problem already the the follow code works.

module main

struct Struct {
	a string
	b int
}

struct Decoder {}

pub fn decode[T](src []u8) !T {
	mut val := T{}

	mut d := Decoder{}

	decode_[T](src, mut val)!

	return val
}

pub fn decode_[T](data []u8, mut val T) ! {
	$if T is $struct {
		mut keys := ['a', 'b']
		for mut key in keys {

			decode_(data, mut key)!

			$for field in T.fields {

				if field.name == key {
					mut value := val.$(field.name)
					// println(val.$(field.name))
					// decode_(data, mut val.$(field.name))!
					decode_(data, mut value)!
					val.$(field.name) = value
				}
			}
		}
	}
}

de := decode[Struct]([])!
println(de)

Reproduction Steps

module main

struct Struct {
    a string
    b int
}

struct Decoder {}

pub fn decode[T](src []u8) !T {
    mut val := T{}

    mut d := Decoder{}

    d.decode[T](src, mut val)!

    return val
}

pub fn (mut d Decoder) decode[T](data []u8, mut val T) ! {
    $if T is $struct {
        mut keys := ['a', 'b']
        for mut key in keys {

            d.decode(data, mut key)!

            $for field in T.fields {

                if field.name == key {
                    mut value := val.$(field.name)
                    // println(val.$(field.name))
                    // d.decode(data, mut val.$(field.name))!
                    d.decode(data, mut value)!
                    val.$(field.name) = value
                }
            }
        }
    }
}

de := decode[Struct]([])!
println(de)

Expected Behavior

Struct{
a: ''
b: 0
}

Current Behavior

discardable.v:33:25: error: cannot use `int` as `&string` in argument 2 to `Decoder.decode`
   31 |                     // println(val.$(field.name))
   32 |                     // d.decode(data, mut val.$(field.name))!
   33 |                     d.decode(data, mut value)!
      |                                        ~~~~~
   34 |                     val.$(field.name) = value
   35 |                 }

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.3 782bf86

Environment details (OS name and version, etc.)

Linux Ubuntu

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.

@enghitalo enghitalo added the Bug This tag is applied to issues which reports bugs. label Nov 28, 2023
@felipensp felipensp added the Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. label Nov 28, 2023
@enghitalo enghitalo changed the title compitime?: wrong cast on methods params Checker: wrong cast on methods params Nov 28, 2023
@shove70
Copy link
Contributor

shove70 commented Dec 5, 2023

This should be able to be closed, @felipensp has solved it.

@felipensp felipensp self-assigned this Dec 5, 2023
@felipensp
Copy link
Member

This should be able to be closed, @felipensp has solved it.

I've solved the mut value part, but the comptimeselector part needs a fix.

@enghitalo enghitalo changed the title Checker: wrong cast on methods params Checker: wrong cast on methods mutable params Dec 10, 2023
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: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants