Skip to content

Check if the SDoc starts with a single quote (#22488)

Vladislav Zavialov requested to merge wip/int-index/ppr-space-quote into master

This patch fixes pretty-printing of character literals inside promoted lists and tuples.

When we pretty-print a promoted list or tuple whose first element starts with a single quote, we want to add a space between the opening bracket and the element:

'[True]    -- ok
'[ 'True]  -- ok
'['True]   -- not ok

If we don't add the space, we accidentally produce a character literal '['.

Before this patch, pprSpaceIfPromotedTyCon inspected the type as an AST and tried to guess if it would be rendered with a single quote. However, it missed the case when the inner type was itself a character literal:

'[ 'x']  -- ok
'['x']   -- not ok

Instead of adding this particular case, I opted for a more future-proof solution: check the SDoc directly. This way we can detect if the single quote is actually there instead of trying to predict it from the AST. The new function is called spaceIfSingleQuote.

Edited by Vladislav Zavialov

Merge request reports