Skip to content

Commit cceabbb

Browse files
plp127kmill
andauthored
fix: quoting single quote Char (''') (#8742)
This PR fixes a bug where the single-quote character `Char.ofNat 39` would delaborate as `'''`, which causes a parse error if pasted back in to the source code. --------- Co-authored-by: Kyle Miller <[email protected]>
1 parent 8019c6c commit cceabbb

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Init/Data/Repr.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,12 @@ instance : Repr Int where
342342
def hexDigitRepr (n : Nat) : String :=
343343
String.singleton <| Nat.digitChar n
344344

345-
def Char.quoteCore (c : Char) : String :=
345+
def Char.quoteCore (c : Char) (inString : Bool := false) : String :=
346346
if c = '\n' then "\\n"
347347
else if c = '\t' then "\\t"
348348
else if c = '\\' then "\\\\"
349349
else if c = '\"' then "\\\""
350+
else if !inString && c = '\'' then "\\\'"
350351
else if c.toNat <= 31 ∨ c = '\x7f' then "\\x" ++ smallCharToHex c
351352
else String.singleton c
352353
where
@@ -383,7 +384,7 @@ Examples:
383384
-/
384385
def String.quote (s : String) : String :=
385386
if s.isEmpty then "\"\""
386-
else s.foldl (fun s c => s ++ c.quoteCore) "\"" ++ "\""
387+
else s.foldl (fun s c => s ++ c.quoteCore (inString := true)) "\"" ++ "\""
387388

388389
instance : Repr String where
389390
reprPrec s _ := s.quote

tests/lean/run/charQuote.lean

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- This used to delaborate as `'''`, which is a parse error.
2+
3+
/--
4+
info: '\''
5+
-/
6+
#guard_msgs in
7+
#eval '\''

0 commit comments

Comments
 (0)