fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

Inheritance using qualified names results in stack overflow/segfault

Open maxteufel opened this issue 2 years ago • 6 comments

I want to fix the ambiguous naming in flang_dev/content/tutorial/examples/demo1_unit_types.fz by using qualified names.

demo1_unit_types is
  Monoid1 (T type) ref is
    zero T is abstract
    add (a, b T) T is abstract

  accum (T type, a array T, m demo1_unit_types.Monoid1 T) T is
    for
      res := m.zero, m.add res x
      x in a
    else
      res

  sum : Monoid1 i32 is
    zero => 0
    add (a, b i32) => a + b

  product : Monoid1 i32 is
    zero => 1 
    add (a, b i32) => a * b

  i32array     := [ 23, 345 , 3, 1, 3 ]
  totalSum     := accum i32 i32array sum
  totalProduct := accum i32 i32array product

works, but once you change the Monoid1 into demo1_unit_types.Monoid1 in the inheritance of sum and product, there is a stack overflow in the interpreter and a segfault in the C backend-produced binary.

maxteufel avatar Mar 09 '23 12:03 maxteufel

related: #220

michaellilltokiwa avatar Mar 09 '23 12:03 michaellilltokiwa

hm yes, I agree this can be intended behavior. But would this be different if one was using demo1_unit_types.this.Monoid1? This is what is suggested by the compiler, but when actually doing so, the parser complains about this in the function declaration and in inheritance, so

accum (T type, a array T, m demo1_unit_types.this.Monoid1 T) T is
  ...

and

sum : demo1_unit_types.this.Monoid1 i32 is
  ...

is not possible. Perhaps it should be, though?

maxteufel avatar Mar 09 '23 13:03 maxteufel

With #1209, Monoid1 is now automatically expanded to demo1_unit_types.this.Monoid1, so the example give an error now

> ./build/bin/fz ex_1201.fz 

/home/fridi/fuzion/work/ex_1201.fz:22:38: error 1: Incompatible types when passing argument in a call
  totalSum     := accum i32 i32array sum
-------------------------------------^
Actual type for argument #2 'm' does not match expected type.
In call to          : 'demo1_unit_types.accum'
expected formal type: 'demo1_unit_types.Monoid1 i32'
actual type found   : 'demo1_unit_types.this.type.ref sum'
assignable to       : 'Any',
                      'demo1_unit_types.this.type.Monoid1 i32',
                      'demo1_unit_types.this.type.ref sum'
for value assigned  : 'box(demo1_unit_types.this.sum)'
To solve this, you could change the type of the target 'm' to 'demo1_unit_types.this.type.ref sum' or convert the type of the assigned value to 'demo1_unit_types.Monoid1 i32'.


/home/fridi/fuzion/work/ex_1201.fz:23:38: error 2: Incompatible types when passing argument in a call
  totalProduct := accum i32 i32array product
-------------------------------------^
Actual type for argument #2 'm' does not match expected type.
In call to          : 'demo1_unit_types.accum'
expected formal type: 'demo1_unit_types.Monoid1 i32'
actual type found   : 'demo1_unit_types.this.type.ref product'
assignable to       : 'Any',
                      'demo1_unit_types.this.type.Monoid1 i32',
                      'demo1_unit_types.this.type.ref product'
for value assigned  : 'box(demo1_unit_types.this.product)'
To solve this, you could change the type of the target 'm' to 'demo1_unit_types.this.type.ref product' or convert the type of the assigned value to 'demo1_unit_types.Monoid1 i32'.

2 errors.

fixing this by replacing m demo1_unit_types.Monoid1 T in the examples by m Monoid1 T

demo1_unit_types is
  Monoid1 (T type) ref is
    zero T is abstract
    add (a, b T) T is abstract

  accum (T type, a array T, m Monoid1 T) T is
    for
      res := m.zero, m.add res x
      x in a
    else
      res

  sum : Monoid1 i32 is
    zero => 0
    add (a, b i32) => a + b

  product : Monoid1 i32 is
    zero => 1
    add (a, b i32) => a * b

  i32array     := [ 23, 345 , 3, 1, 3 ]
  totalSum     := accum i32 i32array sum
  totalProduct := accum i32 i32array product

works:

 > ./build/bin/fz ex_1201a.fz 
 > 

fridis avatar Mar 17 '23 15:03 fridis

Yes, this works. I changed this to Monoid1 because this is a temporary workaround. The problem reappears when it's called Monoid and you get an ambiguous name error. Which should be fixed by using demo1_unit_types.this.Monoid, but this does not work.

maxteufel avatar Mar 20 '23 08:03 maxteufel

@fridis another ping because this is related to the examples in flang_dev.

maxteufel avatar Mar 29 '23 09:03 maxteufel

@maxteufel can this be closed again?

michaellilltokiwa avatar May 29 '24 09:05 michaellilltokiwa