on master help sign in
Fix issue with unparenthesized patterns in pretty printed lambdas pointed out by @JoeyEremondi in #446

Regarding the proposed fix in #446: The pattern pretty printer is correct for cases where you just want the pattern, such as in case-expressions. In some contexts, parentheses are required though, so this fix addresses those places specifically.

commit93321eca632785779fd82d96aae1c42ab775e76b authorEvan Czaplicki <info@elm-lang.org> dateTue 14 Jan 2014, 16:27 (13 years ago) parent6febb89
1 file changed+7−6
compiler/SourceSyntax/Expression.hs+7−6
compiler/SourceSyntax/Expression.hs+7−6
@@ -111,8 +111,10 @@ instance Pretty def => Pretty (Expr' def) where
111111 P.text "-" <> prettyParens e
112112 Binop op e1 e2 -> P.sep [ prettyParens e1 <+> P.text op', prettyParens e2 ]
113113 where op' = if Help.isOp op then op else "`" ++ op ++ "`"
114− Lambda p e -> let (ps,body) = collectLambdas (Location.none $ Lambda p e)
115− in P.text "\\" <> P.sep ps <+> P.text "->" <+> pretty body
114+ Lambda p e -> P.text "\\" <> args <+> P.text "->" <+> pretty body
115+ where
116+ (ps,body) = collectLambdas (Location.none $ Lambda p e)
117+ args = P.sep (map Pattern.prettyParens ps)
116118 App _ _ -> P.hang func 2 (P.sep args)
117119 where func:args = map prettyParens (collectApps (Location.none expr))
118120 MultiIf branches -> P.text "if" $$ nest 3 (vcat $ map iff branches)
@@ -179,12 +181,11 @@ collectApps lexpr@(Location.L _ expr) =
179181 App a b -> collectApps a ++ [b]
180182 _ -> [lexpr]
181183
182−collectLambdas :: LExpr' def -> ([Doc], LExpr' def)
184+collectLambdas :: LExpr' def -> ([Pattern.Pattern], LExpr' def)
183185collectLambdas lexpr@(Location.L _ expr) =
184186 case expr of
185− Lambda pattern body ->
186− let (ps, body') = collectLambdas body
187− in (pretty pattern : ps, body')
187+ Lambda pattern body -> (pattern : ps, body')
188+ where (ps, body') = collectLambdas body
188189 _ -> ([], lexpr)
189190
190191prettyParens :: (Pretty def) => LExpr' def -> Doc