Zulip Chat Archive

Stream: lean4

Topic: Why do docstrings include trailing whitespace?


Thomas Murrills (Aug 09 2025 at 04:41):

Just curious: seeing as the style for docstrings is generally /-- example -/ (or even

/--
example
-/

), with whitespace preceding -/, why do we only call removeLeadingWhitespace in docs#Lean.addDocString and leave trailing whitespace?

To see that trailing whitespace is not handled somewhere else in the code (before rendering, that is), note the space after test:

import Lean

/-- test -/
theorem foo : True := trivial

open Lean in
/-- info: some (test ) -/
#guard_msgs in
run_cmd logInfo m!"{(← findDocString? (← getEnv) `foo)}"

(This isn't crucial; I was just handling different ways of providing docstrings in #28133 and was surprised to note the difference.)

Robin Arnez (Aug 09 2025 at 08:56):

Oh you know what's even funnier? docComments don't even have leading whitespace; that gets trimmed while parsing:

/--
-- heh this is whitespace
/- This is also whitespace -/
It begins here.
-- heh this is not whitespace
/- This also isn't whitespace -/
-/
def hi := 3

Robin Arnez (Aug 09 2025 at 08:56):

Zulip completely messes this up, as expected :-)

Julian Berman (Aug 09 2025 at 09:12):

Not strictly related, but

def foo := 37

/--
/-
/-
-/
theorem stest : True := ⟨⟩

producing

 3:1-3:4: warning:
unused `termination_by?`, function is not recursive

is also a bit strange (though a proper "unterminated comment" does appear at the bottom line as well)

Robin Arnez (Aug 09 2025 at 10:20):

Did you know that this has nothing to do with docstrings?

/-- warning: unused `termination_by?`, function is not recursive -/
#guard_msgs in
def a := 1
arbitrarily_chosen_token /-

Robin Arnez (Aug 09 2025 at 10:22):

The problem is that a token followed by an unterminated comment causes the token to be pushed to the syntax stack but produce an error so satisfySymbolFn doesn't check whether it is actually correct.

Robin Arnez (Aug 09 2025 at 15:20):

Oh and you won't believe what the output of this is (on latest mathlib):

/-- -/ /-

Error message

Thomas Murrills (Aug 09 2025 at 16:09):

Robin Arnez said:

Oh you know what's even funnier? docComments don't even have leading whitespace; that gets trimmed while parsing:

Huh! I was curious, and traced the blame—apparently removing leading spaces was necessary in 2022. Maybe parsing has changed since then?


Last updated: Dec 20 2025 at 21:32 UTC