Skip to content

Commit 7219616

Browse files
authored
chore: legitimize projections on tagged values (#11586)
This PR allows projections on `tagged` values in the IR type system. While executing this branch of code should indeed never happen in practice, enforcing this through the type system would require the compiler to always optimize code to the point where this is not possible. For example in the code: ``` cases x with | none => .... | some => let val : obj := proj[0] x ... ``` static analysis might learn that `x` is always none and transform this to: ``` let x : tagged := none cases x with | none => .... | some => let val : obj := proj[0] x ... ``` Which would be type incorrect if projections on `tagged` were illegitimate. However, we don't want to force static analysis to always simplify code far enough on its own to enforce this invariant.
1 parent 9466a05 commit 7219616

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Lean/Compiler/IR/Checker.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ def checkExpr (ty : IRType) (e : Expr) : M Unit := do
159159
checkObjVar x
160160
| .proj i x =>
161161
let xType ← getType x;
162+
/-
163+
Projections are a valid operation on `tobject`. Thus they should also
164+
be a valid operation for both `object`, `tagged` and unboxed values
165+
as they are subtypes.
166+
-/
162167
match xType with
163168
| .object | .tobject =>
164169
checkObjType ty
@@ -167,6 +172,7 @@ def checkExpr (ty : IRType) (e : Expr) : M Unit := do
167172
checkEqTypes (tys[i]) ty
168173
else
169174
throwCheckerError "invalid proj index"
175+
| .tagged => pure ()
170176
| _ => throwCheckerError s!"unexpected IR type '{xType}'"
171177
| .uproj _ x =>
172178
checkObjVar x

0 commit comments

Comments
 (0)