apple / pkl-intellij

JetBrains editor plugins providing Pkl language support
https://pkl-lang.org/intellij/current/index.html
Apache License 2.0
49 stars 10 forks source link

Cannot flow-type through qualified access #21

Open bvalyou opened 5 months ago

bvalyou commented 5 months ago

Relates to https://github.com/apple/pkl/issues/402

Summary

Subtype checks do not register within a compound conditional if the object being type checked is dereferenced from another variable.

How to reproduce

It's actually pretty interesting - I've spent the last 20 minutes trying to find a minimal reproducible example and after talking through it for a few minutes, I found it.

This does not reproduce it:

import "@k8s/K8sResource.pkl"
import "@k8s/api/core/v1/ConfigMap.pkl"

function testCompoundCondition(resource: K8sResource) = resource is ConfigMap
  && resource.metadata.name == "foo" // metadata autocompletes

This does:

import "@k8s/K8sResource.pkl"
import "@k8s/api/core/v1/ConfigMap.pkl"

class ThingWithK8sResource {
  resource: K8sResource
}

function testCompoundCondition(thing: ThingWithK8sResource) = thing.resource is ConfigMap
  && thing.resource.metadata.name == "foo" // Unresolved reference: metadata

More details

When amending a Listing<K8sResource> to update a named ConfigMap, this member predicate line is not recognizing that we already know the K8sResource is of subtype ConfigMap, which has a metadata attribute:

[[this is ConfigMap && metadata.name == environment.resourceName("app-env")]] {
image

On the subsequent line the amend body properly identifies the data attribute as belonging to the ConfigMap, but it's not resolved within the predicate itself.

Versions

Pkl 0.25.2 Pkl IntelliJ 0.26.0

bioball commented 5 months ago

Thanks for the report. This is a hard problem; most other language plugins can't do this either. The only one I'm aware of that can do this is Kotlin.

holzensp commented 5 months ago

I know that Hack struggled with this too, at least, after any method was called on the outer object. In their case, it's duet mutability. Since Pkl is immutable, this should (famous last words) be generally decidable.

Thanks for a great minimal repro, though!