Skip to content

Haddock comment to the last data constructor is attached incorrectly #421

@Lev135

Description

@Lev135

Working around the stylish-haskell bug I was very surprised to see, that haddock comments in the AST produced by ghc-lib-parser are located in the such strange way: all comments, except the last one, are attached to the DataDecl, while the last one --- to the TyClD.

Consider the following declaration:

data Dat
  = Foo -- ^ foo
  | Bar -- ^ bar
  | Baz -- ^ baz

We get this AST:

L
  RealSrcSpan (@RealSrcSpan' <SrcLoc "<string>" 1 1> <SrcLoc "<string>" 1 1>) Nothing
  HsModule
    EpAnn
      Anchor (@RealSrcSpan' <SrcLoc "<string>" 1 1> <SrcLoc "<string>" 1 1>) UnchangedAnchor
      AnnsModule [] (AnnList Nothing Nothing Nothing [] [])
      EpaCommentsBalanced
        []
        [L
          Anchor (@RealSrcSpan' <SrcLoc "<string>" 5 1> <SrcLoc "<string>" 5 1>) UnchangedAnchor
          EpaComment EpaEofComment (@RealSrcSpan' <SrcLoc "<string>" 5 1> <SrcLoc "<string>" 5 1>)]
    VirtualBraces 1
    Nothing
    Nothing
    []
    [L
      SrcSpanAnn
        EpAnn
          Anchor (@RealSrcSpan' <SrcLoc "<string>" 1 1> <SrcLoc "<string>" 4 8>) UnchangedAnchor
          AnnListItem []
          EpaComments
            [L
              Anchor
                @RealSrcSpan' <SrcLoc "<string>" 4 9> <SrcLoc "<string>" 4 17>
                UnchangedAnchor
              EpaComment
                EpaLineComment "-- ^ baz"
                @RealSrcSpan' <SrcLoc "<string>" 4 5> <SrcLoc "<string>" 4 8>]
        RealSrcSpan
          @RealSrcSpan' <SrcLoc "<string>" 1 1> <SrcLoc "<string>" 4 8>
          Just (BufSpan (BufPos 0) (BufPos 50))
      TyClD
        NoExtField
        DataDecl
          EpAnn
            Anchor (@RealSrcSpan' <SrcLoc "<string>" 1 1> <SrcLoc "<string>" 4 8>) UnchangedAnchor
            [AddEpAnn
              AnnData
              EpaSpan (@RealSrcSpan' <SrcLoc "<string>" 1 1> <SrcLoc "<string>" 1 5>),
            AddEpAnn
              AnnEqual
              EpaSpan (@RealSrcSpan' <SrcLoc "<string>" 2 3> <SrcLoc "<string>" 2 4>)]
            EpaComments
              [L
                Anchor
                  @RealSrcSpan' <SrcLoc "<string>" 2 9> <SrcLoc "<string>" 2 17>
                  UnchangedAnchor
                EpaComment
                  EpaLineComment "-- ^ foo"
                  @RealSrcSpan' <SrcLoc "<string>" 2 5> <SrcLoc "<string>" 2 8>,
              L
                Anchor
                  @RealSrcSpan' <SrcLoc "<string>" 3 9> <SrcLoc "<string>" 3 17>
                  UnchangedAnchor
                EpaComment
                  EpaLineComment "-- ^ bar"
                  @RealSrcSpan' <SrcLoc "<string>" 3 5> <SrcLoc "<string>" 3 8>]
          L
            SrcSpanAnn
              EpAnnNotUsed
              RealSrcSpan
                @RealSrcSpan' <SrcLoc "<string>" 1 6> <SrcLoc "<string>" 1 9>
                Just (BufSpan (BufPos 5) (BufPos 8))
            Unqual (OccName <Dat>)
          HsQTvs NoExtField []
          Prefix
          HsDataDefn
            NoExtField
            DataType
            Nothing
            Nothing
            Nothing
            [L
              SrcSpanAnn
                EpAnn
                  Anchor
                    @RealSrcSpan' <SrcLoc "<string>" 2 5> <SrcLoc "<string>" 2 8>
                    UnchangedAnchor
                  AnnListItem
                    [AddVbarAnn
                      EpaSpan (@RealSrcSpan' <SrcLoc "<string>" 3 3> <SrcLoc "<string>" 3 4>)]
                  EpaComments []
                RealSrcSpan
                  @RealSrcSpan' <SrcLoc "<string>" 2 5> <SrcLoc "<string>" 2 8>
                  Just (BufSpan (BufPos 13) (BufPos 16))
              ConDeclH98
                EpAnn
                  Anchor
                    @RealSrcSpan' <SrcLoc "<string>" 2 5> <SrcLoc "<string>" 2 8>
                    UnchangedAnchor
                  []
                  EpaComments []
                L
                  SrcSpanAnn
                    EpAnnNotUsed
                    RealSrcSpan
                      @RealSrcSpan' <SrcLoc "<string>" 2 5> <SrcLoc "<string>" 2 8>
                      Just (BufSpan (BufPos 13) (BufPos 16))
                  Unqual (OccName <Foo>)
                False
                []
                Nothing
                PrefixCon [] []
                Nothing,
            L
              SrcSpanAnn
                EpAnn
                  Anchor
                    @RealSrcSpan' <SrcLoc "<string>" 3 5> <SrcLoc "<string>" 3 8>
                    UnchangedAnchor
                  AnnListItem
                    [AddVbarAnn
                      EpaSpan (@RealSrcSpan' <SrcLoc "<string>" 4 3> <SrcLoc "<string>" 4 4>)]
                  EpaComments []
                RealSrcSpan
                  @RealSrcSpan' <SrcLoc "<string>" 3 5> <SrcLoc "<string>" 3 8>
                  Just (BufSpan (BufPos 30) (BufPos 33))
              ConDeclH98
                EpAnn
                  Anchor
                    @RealSrcSpan' <SrcLoc "<string>" 3 5> <SrcLoc "<string>" 3 8>
                    UnchangedAnchor
                  []
                  EpaComments []
                L
                  SrcSpanAnn
                    EpAnnNotUsed
                    RealSrcSpan
                      @RealSrcSpan' <SrcLoc "<string>" 3 5> <SrcLoc "<string>" 3 8>
                      Just (BufSpan (BufPos 30) (BufPos 33))
                  Unqual (OccName <Bar>)
                False
                []
                Nothing
                PrefixCon [] []
                Nothing,
            L
              SrcSpanAnn
                EpAnnNotUsed
                RealSrcSpan
                  @RealSrcSpan' <SrcLoc "<string>" 4 5> <SrcLoc "<string>" 4 8>
                  Just (BufSpan (BufPos 47) (BufPos 50))
              ConDeclH98
                EpAnn
                  Anchor
                    @RealSrcSpan' <SrcLoc "<string>" 4 5> <SrcLoc "<string>" 4 8>
                    UnchangedAnchor
                  []
                  EpaComments []
                L
                  SrcSpanAnn
                    EpAnnNotUsed
                    RealSrcSpan
                      @RealSrcSpan' <SrcLoc "<string>" 4 5> <SrcLoc "<string>" 4 8>
                      Just (BufSpan (BufPos 47) (BufPos 50))
                  Unqual (OccName <Baz>)
                False
                []
                Nothing
                PrefixCon [] []
                Nothing]
            []]
    Nothing
    Nothing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions