fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

AoC: type inference failure for cotype failed in case of inferred result type from lambda being inner type of lambda

Open fridis opened this issue 1 year ago • 0 comments

I ran into this during 6 dec 24 Advent of Code:

This code

f(x ()->T) =>
f ()->
  a is
    x => a
  a.x

results in

> ~/fuzion/work/build/bin/fz type_infer_error5.fz 

/home/fridi/fuzion/advent_of_code/fuzion_aoc/2024/06/type_infer_error5.fz:2:3: error 1: Failed to infer actual type parameters
f ()->
  a is
    x => a
  a.x

In call to 'Function.type', no actual type parameters are given and inference of the type parameters failed.
Expected type parameters: 'THIS#TYPE, R, A...'
Type inference failed for one type parameter 'R'

one error.

which is strange since the error complains about the cotype's type parameter. Would be better to complain about the original Function type's type parameter or --even better-- to just infer the type correctly even though the outer type is a generated lambda.

A related problem occurs with the following code

f(x ()->T) =>
f ()->
  a is
    x => a
  a

which complains that argument type when calling f is wrong:

 > ~/fuzion/work/build/bin/fz type_infer_error6.fz 

/home/fridi/fuzion/advent_of_code/fuzion_aoc/2024/06/type_infer_error6.fz:2:3: error 1: Incompatible types when passing argument in a call
f ()->
  a is
    x => a
  a

Actual type for argument #1 'x' does not match expected type.
In call to          : 'f'
expected formal type: 'Function λ.this.call.this.a'
actual type found   : 'universe.this.λ'
assignable to       : 'Any',
                      'Function universe.this.λ.call.this.a',
                      'universe.this.λ'
for value assigned  : 
()->
  a is
    x => a
  a

To solve this, you could change the type of the target 'x' to 'universe.this.λ' or convert the type of the assigned value to 'Function λ.this.call.this.a'.

one error.

In both cases, the underlying problem seems to be that the inferred type for T depends on the type of the feature created for the lambda, which makes this an unusable type outside of the lambda. We should either handle this case in a way that it does not produce these errors, or produce an error message that complains about this situation and explains the problem better.

fridis avatar Dec 07 '24 19:12 fridis