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, cgen: comptimecall type resolution on method args #20091

Closed
enghitalo opened this issue Dec 4, 2023 · 0 comments · Fixed by #20092
Closed

checker, cgen: comptimecall type resolution on method args #20091

enghitalo opened this issue Dec 4, 2023 · 0 comments · Fixed by #20092
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. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@enghitalo
Copy link
Contributor

enghitalo commented Dec 4, 2023

Describe the bug

same of solved in #20070, but with methods

Reproduction Steps

struct Human {
	name string
}

enum Animal {
	dog
	cat
}

type Entity = Animal | Human

struct Encoder {}

@[sumtype_to: Animal]
fn (ent Entity) json_cast_to_animal() Animal {
	return ent as Animal
}

@[sumtype_to: Human]
fn (ent Entity) json_cast_to_human() Human {
	return ent as Human
}

fn (e &Encoder) encode[T](val T) {
	$if T is $sumtype {
		$for method in T.methods {
			if method.attrs.len >= 1 {
				if method.attrs[0].contains('sumtype_to') {
					if val.type_name() == method.attrs[0].all_after('sumtype_to:').trim_space() {
						println(val.$method())
						e.encode(val.$method()) // Problem here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!!!11!
					}
				}
			}
		}
	} $else {
		// error('cannot encode value with ${typeof(val).name} type')
	}
}


e := Encoder{}

e.encode(Entity(Human{'Monke'}))
e.encode(Entity(Animal.cat))

Expected Behavior

Human{
    name: 'Monke'
}
cat

Current Behavior

20065.v:36:16: error: cannot use `string` as `Human` in argument 1 to `Encoder.encode`
   34 |                     if val.type_name() == method.attrs[0].all_after('sumtype_to:').trim_space() {
   35 |                         println(val.$method())
   36 |                         e.encode(val.$method()) // Problem here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!!!11!
      |                                  ~~~~~~~~~~~~~
   37 |                     }
   38 |                 }
20065.v:36:16: error: cannot use `string` as `Animal` in argument 1 to `Encoder.encode`
   34 |                     if val.type_name() == method.attrs[0].all_after('sumtype_to:').trim_space() {
   35 |                         println(val.$method())
   36 |                         e.encode(val.$method()) // Problem here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!!!11!
      |                                  ~~~~~~~~~~~~~
   37 |                     }
   38 |                 }

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.3 cce21a4

Environment details (OS name and version, etc.)

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 Dec 4, 2023
@enghitalo enghitalo changed the title checker, cgen: fix comptimecall type resolution on method args checker, cgen: comptimecall type resolution on method args Dec 4, 2023
@felipensp felipensp self-assigned this Dec 4, 2023
@felipensp felipensp added Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Dec 5, 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: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
2 participants