mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG] functions that raise with memory primary result type have problems

Open sa- opened this issue 1 year ago • 1 comments

Bug Description

When I try to raise before assigning values, I get an error that seems like it incorrectly refers to self.shape when I'd like to refer to the function parameter

error: Expression [13]:11:13: use of uninitialized value 'self.shape'
            raise Error("only 2d for now")
            ^

Expression [13]:9:17: 'self' declared here
    fn __init__(inout self, shape: DynamicVector[Int], data: DynamicVector[F64]) raises:

It works fine if I raise after the assignments though

Steps to Reproduce

struct Tensor:
    var shape: DynamicVector[Int]
    var data: DynamicVector[F64]
    
    fn __init__(inout self, shape: DynamicVector[Int], data: DynamicVector[F64]) raises:
        if shape.size > 2:
            raise Error("only 2d for now")
        self.shape = shape
        self.data = data

Workaround:

struct Tensor:
    var shape: DynamicVector[Int]
    var data: DynamicVector[F64]
    
    fn __init__(inout self, shape: DynamicVector[Int], data: DynamicVector[F64]) raises:
        self.shape = shape
        self.data = data
        if shape.size > 2:
            raise Error("only 2d for now")

sa- avatar May 22 '23 11:05 sa-

Thanks for filing. This is a known issue with raising inside a function with a memory primary result type. @stumpOS is already working on this I think

Mogball avatar May 22 '23 15:05 Mogball

This is fixed and will be available in the June 28 release.

stumpOS avatar Jun 21 '23 21:06 stumpOS