Skip to content

Commit a2d125b

Browse files
committed
Finished printing of items
Finished testing (and consequently fixing) printing of impl's and trait's. In the process, I realized `SelfKind` was only an intermediate type used to print `Arg`s that should be self's. This led to removing `SelfKind` completely - its function is entirely inlined in `printArg` now.
1 parent f74c934 commit a2d125b

File tree

4 files changed

+85
-30
lines changed

4 files changed

+85
-30
lines changed

language-rust.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ test-suite tests
5959
, Cabal >= 1.10.0
6060
, transformers >=0.5 && <0.6
6161
, HUnit
62-
, pretty >=1.1 && <1.2
6362
, wl-pprint-annotated >= 0.0.1 && < 0.0.2
6463
, test-framework
6564
, test-framework-hunit >= 0.3.0

src/Language/Rust/Pretty.hs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -657,14 +657,20 @@ printItem (Item ident attrs node vis x) = annotate x $ align $ printOuterAttrs a
657657
Union s g -> hsep [ printVis vis, "union", printStruct s g ident True True ]
658658
DefaultImpl u t -> hsep [ printVis vis, printUnsafety u, "impl", printTraitRef t, "for", "..", "{ }" ]
659659
Impl u p g t ty i -> let generics = case g of { Generics [] [] _ _ -> mempty; _ -> printGenerics g }
660-
traitref = perhaps (\t' -> printTraitRef t' <+> "for") t
661-
in hsep [ printVis vis, printUnsafety u, "impl" <> generics, printPolarity p <> traitref, printType ty
662-
, printWhereClause (whereClause g), braceAttrs (printInnerAttrs attrs) (printImplItem `map` i) ]
663-
Trait u g tys i -> let tys' = map (\t -> case t of TraitTyParamBound ptr Maybe -> Left ("for ?" <+> printTraitRef (traitRef ptr))
664-
_ -> Right t)
660+
traitref = perhaps (\t' -> printPolarity p <> printTraitRef t' <+> "for") t
661+
in hsep [ printVis vis, printUnsafety u, "impl", generics
662+
, traitref <+> printType ty
663+
, printWhereClause (whereClause g)
664+
, block (vsep (printInnerAttrs attrs : (printImplItem `map` i)))
665+
]
666+
Trait u g tys i -> let tys' = map (\t -> case t of
667+
TraitTyParamBound ptr Maybe -> Left ("for ?" <+> printTraitRef (traitRef ptr))
668+
_ -> Right t)
665669
tys
666-
in hsep [ printVis vis, printUnsafety u, "trait", printIdent ident <> printGenerics g, hsep (lefts tys')
667-
, printBounds ":" (rights tys'), printWhereClause (whereClause g), braceAttrs mempty (printTraitItem `map` i) ]
670+
in hsep [ printVis vis, printUnsafety u, "trait", printIdent ident <> printGenerics g
671+
, hsep (lefts tys'), printBounds ":" (rights tys'), printWhereClause (whereClause g)
672+
, block (vsep (printTraitItem `map` i))
673+
]
668674
MacItem m -> printMac m Paren <> ";"
669675

670676

@@ -806,16 +812,16 @@ printFnArgsAndRet (FnDecl args ret var x) = annotate x ("(" <> align (fillSep ar
806812

807813
-- aka print_arg TODO double check this
808814
printArg :: Arg a -> Bool -> Doc a
809-
printArg (Arg (Infer x') (Just pat) x) True = annotate x (annotate x' (printPat pat))
815+
printArg (Arg (Infer x') (Just pat) x) True = annotate x $ annotate x' (printPat pat)
810816
printArg (Arg ty Nothing x) _ = annotate x (printType ty)
817+
printArg (Arg ty (Just (IdentP (ByValue m) "self" Nothing x')) x) _ = annotate x $ annotate x' $
818+
case ty of
819+
ImplicitSelf x'' -> annotate x'' (printMutability m <+> "self")
820+
Rptr lt m (ImplicitSelf x''') x'' -> annotate x'' $ annotate x'' $
821+
"&" <> perhaps printLifetime lt <+> printMutability m <+> "self"
822+
_ -> printMutability m <+> "self" <> ":" <+> printType ty
811823
printArg (Arg ty (Just pat) x) _ = annotate x (printPat pat <> ":" <+> printType ty)
812824

813-
-- print_explicit_self
814-
printExplicitSelf :: SelfKind a -> Doc a
815-
printExplicitSelf (ValueSelf mut) = printMutability mut <+> "self"
816-
printExplicitSelf (Region lifetime_m mut) = "&" <> perhaps printLifetime lifetime_m <+> printMutability mut <+> "self"
817-
printExplicitSelf (Explicit ty mut) = printMutability mut <+> "self" <> ":" <+> printType ty
818-
819825
-- aka print_lifetime
820826
printLifetime :: Lifetime a -> Doc a
821827
printLifetime (Lifetime n x) = annotate x ("'" <> printName n)
@@ -871,8 +877,8 @@ printBindingMode (ByValue Mutable) = "mut"
871877

872878
-- aka print_fn_header_info
873879
printFnHeaderInfo :: Unsafety -> Constness -> Abi -> Visibility a -> Doc a
874-
printFnHeaderInfo u c a v = foldr1 (<+>) parts
875-
where parts = [ printVis v, case c of { Const -> "const"; _ -> mempty }, printUnsafety u, printAbi a, "fn" ]
880+
printFnHeaderInfo u c a v = hsep [ printVis v, case c of { Const -> "const"; _ -> mempty }
881+
, printUnsafety u, printAbi a, "fn" ]
876882

877883
printAbi :: Abi -> Doc a
878884
printAbi Rust = mempty

src/Language/Rust/Syntax/AST.hs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -667,15 +667,6 @@ data RangeLimits
667667
| Closed -- ^ Inclusive at the beginning and end
668668
deriving (Eq, Enum, Bounded, Show)
669669

670-
-- | Alternative representation for Args describing self parameter of methods.
671-
-- E.g. &mut self as in fn foo(&mut self)
672-
-- https://docs.serde.rs/syntex_syntax/ast/enum.SelfKind.html
673-
data SelfKind a
674-
= ValueSelf Mutability -- ^ self, mut self
675-
| Region (Maybe (Lifetime a)) Mutability -- ^ &'lt self, &'lt mut self
676-
| Explicit (Ty a) Mutability -- ^ self: TYPE, mut self: TYPE
677-
deriving (Eq, Functor, Show)
678-
679670
-- | A statement.
680671
-- https://docs.serde.rs/syntex_syntax/ast/struct.Stmt.html
681672
-- Inlined [StmtKind](https://docs.serde.rs/syntex_syntax/ast/enum.StmtKind.html)

tests/PrettyTest.hs

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,61 @@ prettyItems = testGroup "printing items"
368368
, testFlatten "impl std::Debug for .. { }" (printItem (Item (mkIdent "") [] (DefaultImpl Normal (TraitRef (Path False [std,debug] ()) ())) InheritedV ()))
369369
, testFlatten "unsafe impl Debug for .. { }" (printItem (Item (mkIdent "") [] (DefaultImpl Unsafe (TraitRef (Path False [debug] ()) ())) InheritedV ()))
370370
, testFlatten "impl Debug for i32 { }" (printItem (Item (mkIdent "") [] (Impl Normal Positive (Generics [] [] (WhereClause [] ()) ()) (Just (TraitRef (Path False [debug] ()) ())) i32 []) InheritedV ()))
371-
{- TODO unfinished -}
371+
, testFlatten "pub impl !Debug for i32 where 'lt: 'gt { }" (printItem (Item (mkIdent "") [] (Impl Normal Negative (Generics [] [] (WhereClause [RegionPredicate (Lifetime (Name "lt") ()) [Lifetime (Name "gt") ()] ()] ()) ()) (Just (TraitRef (Path False [debug] ()) ())) i32 []) PublicV ()))
372+
, testFlatten "impl <T> GenVal<T> {\n fn value(&self) -> &T { return 1; }\n}"
373+
(printItem (Item (mkIdent "") [] (Impl Normal Positive
374+
(Generics [] [TyParam [] (mkIdent "T") [] Nothing ()] (WhereClause [] ()) ())
375+
Nothing
376+
(PathTy Nothing (Path False [("GenVal", AngleBracketed [] [PathTy Nothing (Path False [(mkIdent "T", AngleBracketed [] [] [] ())] ()) ()] [] ())] ()) ())
377+
[ ImplItem (mkIdent "value") InheritedV Final []
378+
(MethodI (MethodSig Normal NotConst Rust
379+
(FnDecl [Arg (Rptr Nothing Immutable (ImplicitSelf ()) ()) (Just (IdentP (ByValue Immutable) "self" Nothing ())) ()]
380+
(Just (Rptr Nothing Immutable (PathTy Nothing (Path False [(mkIdent "T", AngleBracketed [] [] [] ())] ()) ()) ()))
381+
False ())
382+
(Generics [] [] (WhereClause [] ()) ()))
383+
retBlk)
384+
()
385+
]) InheritedV ()))
386+
, testFlatten "#[cfgo]\nimpl i32 {\n #![cfgi]\n fn value(&self) -> i32 { return 1; }\n pub const pi: i32 = 1;\n default type Size = i32;\n}"
387+
(printItem (Item (mkIdent "") [cfgI,cfgO] (Impl Normal Positive
388+
(Generics [] [] (WhereClause [] ()) ())
389+
Nothing
390+
i32
391+
[ ImplItem (mkIdent "value") InheritedV Final []
392+
(MethodI (MethodSig Normal NotConst Rust
393+
(FnDecl [Arg (Rptr Nothing Immutable (ImplicitSelf ()) ()) (Just (IdentP (ByValue Immutable) "self" Nothing ())) ()]
394+
(Just i32)
395+
False ())
396+
(Generics [] [] (WhereClause [] ()) ()))
397+
retBlk)
398+
()
399+
, ImplItem (mkIdent "pi") PublicV Final [] (ConstI i32 _1) ()
400+
, ImplItem (mkIdent "Size") InheritedV Default [] (TypeI i32) ()
401+
]) InheritedV ()))
402+
, testFlatten "unsafe trait Show { }" (printItem (Item (mkIdent "Show") [] (Trait Unsafe (Generics [] [] (WhereClause [] ()) ()) [] []) InheritedV ()))
403+
, testFlatten "trait Show<T> : 'l1 + for<'l3: 'l1 + 'l2> Debug + 'l2 { }" (printItem (Item (mkIdent "Show") []
404+
(Trait Normal
405+
(Generics [] [TyParam [] (mkIdent "T") [] Nothing ()] (WhereClause [] ()) ())
406+
[ RegionTyParamBound (Lifetime (Name "l1") ())
407+
, TraitTyParamBound (PolyTraitRef [LifetimeDef [] (Lifetime (Name "l3") ()) [Lifetime (Name "l1") (), Lifetime (Name "l2") ()] ()] (TraitRef (Path False [debug] ()) ()) ()) None
408+
, RegionTyParamBound (Lifetime (Name "l2") ())]
409+
[]) InheritedV ()))
410+
, testFlatten "pub trait Show {\n fn value(&self) -> i32 ;\n const pi: i32 = 1;\n const e: i32;\n type Size = i32;\n type Length : 'l3;\n type SomeType : 'l1 = f64;\n}"
411+
(printItem (Item (mkIdent "Show") [] (Trait Normal (Generics [] [] (WhereClause [] ()) ()) []
412+
[ TraitItem (mkIdent "value") []
413+
(MethodT (MethodSig Normal NotConst Rust
414+
(FnDecl [Arg (Rptr Nothing Immutable (ImplicitSelf ()) ()) (Just (IdentP (ByValue Immutable) "self" Nothing ())) ()]
415+
(Just i32)
416+
False ())
417+
(Generics [] [] (WhereClause [] ()) ()))
418+
Nothing)
419+
()
420+
, TraitItem (mkIdent "pi") [] (ConstT i32 (Just _1)) ()
421+
, TraitItem (mkIdent "e") [] (ConstT i32 Nothing) ()
422+
, TraitItem (mkIdent "Size") [] (TypeT [] (Just i32)) ()
423+
, TraitItem (mkIdent "Length") [] (TypeT [RegionTyParamBound (Lifetime (Name "l3") ())] Nothing) ()
424+
, TraitItem (mkIdent "SomeType") [] (TypeT [RegionTyParamBound (Lifetime (Name "l1") ())] (Just f64)) ()
425+
]) PublicV ()))
372426
]
373427

374428
-- | Test pretty-printing of statements (flattened).
@@ -392,11 +446,16 @@ prettyStatements = testGroup "printing statements"
392446
, testFlatten "println!(foo)" (printStmt (MacStmt (Mac (Path False [println] ()) [ Token mempty (IdentTok (mkIdent "foo")) ] ()) NoBracesMac [] ()))
393447
]
394448

395-
449+
-- | Default pretty-printing
396450
testRender :: String -> Doc a -> Test
397-
testRender str doc = testCase str $ str @=? display (renderPrettyDefault doc)
451+
testRender str doc = testCase (escapeNewlines str) $ str @=? display (renderPrettyDefault doc)
398452

453+
-- | This tries to make it so that the `Doc` gets rendered onto only one line.
399454
testFlatten :: String -> Doc a -> Test
400-
testFlatten str doc = testCase str $ str @=? display (renderPretty 0.5 1000 (flatten doc))
455+
testFlatten str doc = testCase (escapeNewlines str) $ str @=? display (renderPretty 0.5 1000 (flatten doc))
456+
457+
-- | Utility function for escaping newlines (and only newlines)
458+
escapeNewlines :: String -> String
459+
escapeNewlines = concatMap (\c -> if c == '\n' then "\\n" else [c])
401460

402461

0 commit comments

Comments
 (0)