v icon indicating copy to clipboard operation
v copied to clipboard

checker: check error of casting to interface (fix #17522)

Open yuyi98 opened this issue 2 years ago • 0 comments

This PR check error of casting to interface (fix #17522).

  • Check error of casting to interface.
  • Add test.
fn main() {
	dump(foo()!)
}

fn foo() !int {
	// do something
	if true {
		return 1
	}
	return my_error()
}

struct CustomError {
pub:
	msg  string
	code int
}

fn my_error() IError {
	return IError(CustomError{})
}

PS D:\Test\v\tt1> v run .
tt1.v:20:9: notice: `CustomError` doesn't implement method `msg` of interface `IError`. The usage of fields is being deprecated in favor of methods.   
   18 |
   19 | fn my_error() IError {
   20 |     return IError(CustomError{})
      |            ~~~~~~~~~~~~~~~~~~~~~
   21 | }
tt1.v:20:9: error: `CustomError` does not implement interface `IError`, cannot cast `CustomError` to interface `IError`
   18 |
   19 | fn my_error() IError {
   20 |     return IError(CustomError{})
      |            ~~~~~~~~~~~~~~~~~~~~~
   21 | }

yuyi98 avatar Mar 07 '23 09:03 yuyi98