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

ConstraintSystem error in type-proofs #741

Open
i-walker opened this issue Sep 8, 2020 · 1 comment
Open

ConstraintSystem error in type-proofs #741

i-walker opened this issue Sep 8, 2020 · 1 comment

Comments

@i-walker
Copy link
Member

i-walker commented Sep 8, 2020

Currently, this diagnostic isn't covered yet Errors.TYPE_INFERENCE_UPPER_BOUND_VIOLATED for type-proofs
Given this code example:

fun <A : @Given Semigroup<A>> List<A>.collapse(
        initial: A,
        f: @Given() (A) -> A = given() // here ::id is injected
      ): A =
        fold(initial) { acc: A, a: A ->
          acc.combine(f(a))
        }
      
@Given
internal fun <A> id(a: A): A = a
      
val result = listOf("Hello ", "is it me", "your looking for").collapse(String.empty())

Compilation reads 2 errors:

  • error found: /var/folders/qq/0zkljkj12jb9m0jnwftykyd00000gn/T/Kotlin-Compilation3131983258667752054/sources/Source.kt: (17, 63): Type parameter bound for A in fun <A : Semigroup> List.collapse(initial: A, f: (A) -> A = ...): A
    is not satisfied: inferred type String is not a subtype of Semigroup
  • error found: /var/folders/qq/0zkljkj12jb9m0jnwftykyd00000gn/T/Kotlin-Compilation3131983258667752054/sources/Source.kt: (11, 9): Unresolved reference: combine

the current constraint system only contains these constraints:

Constraint(kind=SUB_TYPE, subtype=String, superType=A, position=VALUE_PARAMETER_POSITION(0))
Constraint(kind=SUB_TYPE, subtype=List<String>, superType=List<A>, position=RECEIVER_POSITION)

When looking into the constraint system regarding type inference. One would need to recreate a ConstraintSystem where the Proof from A -> Semigroup<A> or more concretely String -> Semigroup<String> is provided such that
String and Semigroup<String> are both Subtypes of A. Evidently, as one can go from String to Semigroup.
The constraint system allows a rebuild with new constraints, or other manipulations and after that one needs to check for isSuccessful.
That can be done, among other ways, in Analysis through suppressing Diagnostics or with the storageComponent and manipulating the constraint system manually.

Please check if by supplying a successful constraint system the second error will be resolved, too. If not then SyntheticResolution needs to be reviewed.

I am leaving it here in case someone want's to pick it up.

@i-walker
Copy link
Member Author

i-walker commented Sep 8, 2020

A very rough draft how to solve it by suppressing Diagnostics, if the explanation above doesn't make sense:

fun CompilerContext.suppressTypeInferenceUpperBoundViolated(diagnostic: Diagnostic):Boolean=
  diagnostic.factory == Errors.TYPE_INFERENCE_UPPER_BOUND_VIOLATED &&
    diagnostic.safeAs<DiagnosticWithParameters1<KtExpression, InferenceErrorData>>()?.let {error ->
      val expression = error.psiElement
      val inferenceError = error.a
      val constraintSystem = inferenceError.constraintSystem

      val newsystem =
        constraintSystem.toBuilder().run {
          val subType: KotlinType = TODO()
          val superType: KotlinType = TODO()

          if(coerceProof(subType, superType) != null){ // proof exists
            addSubtypeConstraint(TODO(), TODO(), TODO())
            build()
          } else {
            build() // rebuild the old system
          }
        }
      newsystem.status.isSuccessful()
    } ?: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant