Skip to content

Commit

Permalink
fix: miscompilation in constant folding
Browse files Browse the repository at this point in the history
closes #4306
  • Loading branch information
leodemoura committed May 31, 2024
1 parent 007b423 commit 948a241
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Lean/Compiler/ConstFolding.lean
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ def foldCharOfNat (beforeErasure : Bool) (a : Expr) : Option Expr := do
else
return mkUInt32Lit 0

def foldToNat (_ : Bool) (a : Expr) : Option Expr := do
def foldToNat (size : Nat) (_ : Bool) (a : Expr) : Option Expr := do
let n ← getNumLit a
return mkRawNatLit n
return mkRawNatLit (n % size)


def uintFoldToNatFns : List (Name × UnFoldFn) :=
numScalarTypes.foldl (fun r info => (info.toNatFn, foldToNat) :: r) []
numScalarTypes.foldl (fun r info => (info.toNatFn, foldToNat info.size) :: r) []

def unFoldFns : List (Name × UnFoldFn) :=
[(``Nat.succ, foldNatSucc),
Expand Down
24 changes: 24 additions & 0 deletions tests/lean/run/4306.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/--
info: 12776324570088369205
-/
#guard_msgs in
#eval (123456789012345678901).toUInt64

/--
info: 12776324570088369205
-/
#guard_msgs in
#eval (123456789012345678901).toUInt64.toNat

/--
error: application type mismatch
Lean.ofReduceBool false._nativeDecide_1 true (Eq.refl true)
argument has type
true = true
but function has type
Lean.reduceBool false._nativeDecide_1 = true → false._nativeDecide_1 = true
-/
#guard_msgs in
theorem false : False := by
have : (123456789012345678901).toUInt64.toNat = 123456789012345678901 := by native_decide
simp [Nat.toUInt64] at this

0 comments on commit 948a241

Please sign in to comment.