unisonweb / unison

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

Name conflict between function definition and type constructor crashes ucm #1026

Open anovstrup opened 4 years ago

anovstrup commented 4 years ago

Observed in M1g. To reproduce, add these definitions to a scratch file and then run the add command:

type A = Foo Nat

A.Foo x = 3

Unison exits with

ucm: Unison bug, typechecked file w/ multiple terms named A.Foo: [Ref #vu8vd,Con #d97e0 0 Data]
CallStack (from HasCallStack):
  error, called at src/Unison/Codebase/Editor/HandleInput.hs:1940:12 in unison-parser-typechecker-0.1-3ssAWNgc3jPJasVcPbQb8J:Unison.Codebase.Editor.HandleInput
anovstrup commented 4 years ago

I just realized that I had messed up the original bug report. The name of the definition must be qualified with the type name (i.e., A.Foo x = 3 not Foo x = 3) in order to trigger the bug. I've edited the description accordingly.

anovstrup commented 4 years ago

Transcript here: https://gist.github.com/anovstrup/1123c4fa24d0ca9c85ec73ee0fc8e09b

mitchellwrosen commented 4 years ago

Hm, I added the typechecker label then removed cause I'm now unsure. I think it may just be HandleInput code forgetting about this case (two terms with the same name, one a Con and one a Ref):

  doTerm :: v -> (Path, Branch0 m -> Branch0 m)
  doTerm v = case toList (Names.termsNamed names (Name.fromVar v)) of
    [] -> errorMissingVar v
    [r] -> case Path.splitFromName (Name.fromVar v) of
      Nothing -> errorEmptyVar
      Just split -> BranchUtil.makeAddTermName split r (md v)
    wha -> error $ "Unison bug, typechecked file w/ multiple terms named "
                <> Var.nameStr v <> ": " <> show wha
mitchellwrosen commented 4 years ago

...otoh it does seem awful similar to the DuplicateDeclarations error emitted by the type checker. And you probably don't want to have these two conflicting names added...