Skip to content

Commit f2b3e86

Browse files
authored
Fix an issue that causes the typecheck analyzer to short-circuit (#86)
The analyzer should keep analyzing past nodes whose types are unknown, because they might have children whose types are known.
1 parent 87320bf commit f2b3e86

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/main/kotlin/org/pkl/lsp/analyzers/TypeCheckAnalyzer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ class TypeCheckAnalyzer(project: Project) : Analyzer(project) {
3131
override fun doAnalyze(node: PklNode, holder: DiagnosticsHolder): Boolean {
3232
if (node !is PklExpr) return true
3333

34-
val module = node.enclosingModule ?: return true
34+
val module = node.enclosingModule ?: return false
3535
val project = module.project
3636
val base = project.pklBaseModule
3737
val context = node.containingFile.pklProject
3838

3939
val expectedType = node.inferExprTypeFromContext(base, mapOf(), context)
40-
if (expectedType == Type.Unknown) return false
40+
if (expectedType == Type.Unknown) return true
4141

4242
val exprType = node.computeExprType(base, mapOf(), context)
4343
val exprValue = lazy { node.toConstraintExpr(base, context).evaluate(ConstraintValue.Error) }

src/test/files/DiagnosticsSnippetTests/inputs/typecheck/constraints.pkl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@ datasize5: DataSize(isDecimalUnit) = 5.mib
5353
string1: String(isEmpty) = "hello"
5454

5555
string2: String(isRegex) = "\\"
56+
57+
typealias MyStr = String(matches(Regex(#"\d*"#)))
58+
59+
listing1 = new Listing<MyStr> {
60+
"Invalid"
61+
}
62+
63+
listing2: Listing<MyStr> = new {
64+
"Invalid"
65+
}

src/test/files/DiagnosticsSnippetTests/outputs/typecheck/constraints.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,21 @@
165165
| Error: Constraint violation.
166166
| Required: isRegex
167167
| Found: "\"
168-
168+
169+
typealias MyStr = String(matches(Regex(#"\d*"#)))
170+
171+
listing1 = new Listing<MyStr> {
172+
"Invalid"
173+
^^^^^^^^^
174+
| Error: Constraint violation.
175+
| Required: matches(Regex(#"\d*"#))
176+
| Found: "Invalid"
177+
}
178+
179+
listing2: Listing<MyStr> = new {
180+
"Invalid"
181+
^^^^^^^^^
182+
| Error: Constraint violation.
183+
| Required: matches(Regex(#"\d*"#))
184+
| Found: "Invalid"
185+
}

0 commit comments

Comments
 (0)