Fix two parse issues related to newlines#499
Open
maksbotan wants to merge 1 commit intohaskell:mainfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bugs fixed
When the Data (records) step reformatted declarations containing long type signatures or
deriving viaclauses with gap strings, the GHC pretty-printer (showPpr) could insert newline characters into rendered type expressions. These newlines ended up embedded inside singleLinesentries rather than being split into separate lines. Subsequent steps--most notably simple_align--would then miscount line numbers, causing them to modify the wrong source lines and corrupt identifiers elsewhere in the file (e.g. garbling variable names in unrelated record fields).The bug manifested in two scenarios:
showPprwould line-wrap types likeM.Map (Name, SomeLongKeyType, AnotherVeryLongKeyName) SomeVeryLongResultType, injecting\ninto the output of the Data step.deriving viawith gap strings -- Haskell gap strings ("foo\<newline> \bar") contain literal newlines that passed throughshowOutputableand intoLinesentries unsplit.In both scenarios,
stylish-haskellfailed with parser error instead of producing any results.Summary
Fix
showOutputableto avoid inserting newlines: ReplaceGHC.showPpr(which uses the pretty-printer's line-wrapping logic) withGHC.showSDocOneLineso that rendered types are always single-line strings. This prevents the Data step from producingLinesentries containing embedded\ncharacters.Handle embedded newlines in
Printer.putText: MakeputTextsplit on\nand emit propernewlinecalls, so that even if a GHC pretty-printed string contains newlines (e.g. gap strings inderiving viaclauses), the resultingLinesare correctly segmented. This prevents subsequent steps (likesimple_align) from miscounting line numbers and corrupting identifiers.Add regression tests: Two integration tests (
case01,case02) inRegressions.hsthat exercise the fullrecords->simple_align->trailing_whitespacepipeline on inputs that previously triggered the bug -- records with long types thatshowPprwould wrap, andderiving viaclauses with gap strings.Disclaimer
Idea of the fix was produced by claude opus 4.6 and later polished by myself. Suggestions how to improve this PR are more than welcome.