leanprover / lean4

Lean 4 programming language and theorem prover
https://lean-lang.org
Apache License 2.0
4.76k stars 428 forks source link

Lean.PrettyPrinter puts comments in invalid places #3791

Open eric-wieser opened 8 months ago

eric-wieser commented 8 months ago

Prerequisites

Description

The pretty printer for Command is merging a -- comment line with the following line, causing the result to contain code that is commented out.

Context

[Broader context that the issue occured in. If there was any prior discussion on the Lean Zulip, link it here as well.]

Steps to Reproduce

  1. Run the following:

    import Lean
    open Lean
    
    -- test `Command`
    run_cmd Elab.Command.liftTermElabM do
      let s := "theorem ohno : \n\
        -- commented out:\n\
        1 = 1 := trivial"
      let cmd : Command := ⟨← ofExcept <| Parser.runParserCategory (← getEnv) `command s⟩
      logInfo (← PrettyPrinter.ppCommand cmd)
    
    -- test `Term`
    run_cmd Elab.Command.liftTermElabM do
      let s := "∀ x : ℕ,
        -- commented out:\n\
        x = x"
      let cmd : Term := ⟨← ofExcept <| Parser.runParserCategory (← getEnv) `term s⟩
      logInfo (← PrettyPrinter.ppTerm cmd)

Expected behavior: The resulting syntax should be valid

Actual behavior: It is not; it prints out

theorem ohno :
    -- commented out:1 = 1 :=
  trivial

and

∀ x : ℕ,
  -- commented out:x = x

Versions

4.7.0-rc2 (web editor)

Impact

Add :+1: to issues you consider important. If others are impacted by this issue, please ask them to add :+1: to it.

kim-em commented 7 months ago

I'd be happy to review a PR fixing this; no idea how difficult it is.