unisonweb / unison

A friendly programming language from the future
https://unison-lang.org
Other
5.75k stars 269 forks source link

`display doc` with `@source{type MyType}` doesn't produce record syntax #5272

Open aryairani opened 1 month ago

aryairani commented 1 month ago

reported by @etorreborre

Input:

```ucm
scratch/main> builtins.mergeio lib.builtin
type Record = { name: Text, age: Nat }
scratch/main> add
README = {{ @source{type Record} }}
scratch/main> add
scratch/main> view README
scratch/main> view Record
scratch/main> display README

I expected display README to show record syntax like view Record, but it doesn't.


Output:
scratch/main> builtins.mergeio lib.builtin

  Done.
type Record = { name: Text, age: Nat }

  Loading changes detected in scratch.u.

  I found and typechecked these definitions in scratch.u. If you
  do an `add` or `update`, here's how your codebase would
  change:

    ⍟ These new definitions are ok to `add`:

      type Record
      Record.age         : Record -> Nat
      Record.age.modify  : (Nat ->{g} Nat)
                           -> Record
                           ->{g} Record
      Record.age.set     : Nat -> Record -> Record
      Record.name        : Record -> Text
      Record.name.modify : (Text ->{g} Text)
                           -> Record
                           ->{g} Record
      Record.name.set    : Text -> Record -> Record
scratch/main> add

  ⍟ I've added these definitions:

    type Record
    Record.age         : Record -> Nat
    Record.age.modify  : (Nat ->{g} Nat) -> Record ->{g} Record
    Record.age.set     : Nat -> Record -> Record
    Record.name        : Record -> Text
    Record.name.modify : (Text ->{g} Text)
                         -> Record
                         ->{g} Record
    Record.name.set    : Text -> Record -> Record
README = {{ @source{type Record} }}

  Loading changes detected in scratch.u.

  I found and typechecked these definitions in scratch.u. If you
  do an `add` or `update`, here's how your codebase would
  change:

    ⍟ These new definitions are ok to `add`:

      README : Doc2
scratch/main> add

  ⍟ I've added these definitions:

    README : Doc2

scratch/main> view README

  README : Doc2
  README = {{     @source{type Record} }}

scratch/main> view Record

  type Record = { name : Text, age : Nat }

scratch/main> display README

      type Record = Record Text Nat

I expected display README to show record syntax like view Record, but it doesn't.



**Environment (please complete the following information):**
 v0.5.25

**Additional context**
The fix should be deployed to Share too; e.g. this should display as a record:
https://share.unison-lang.org/@etorreborre/records-in-doc
<img width="781" alt="image" src="https://github.com/user-attachments/assets/f7a36e03-79ea-4457-b0dc-b297906ae7c0">
aryairani commented 1 month ago

Maybe I'm missing something, but it looks like the doc renderer does call prettyDataDecl which calls getFieldAndAccessorNames, so something more subtle is going wrong, which may hopefully not require a bunch of extra plumbing.

https://github.com/unisonweb/unison/blob/trunk/unison-share-api/src/Unison/Server/Doc.hs#L249 https://github.com/unisonweb/unison/blob/trunk/parser-typechecker/src/Unison/Syntax/DeclPrinter.hs#L135