Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run should use the TypeLookup from the file too #5483

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions parser-typechecker/src/Unison/UnisonFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Unison.UnisonFile
nonEmpty,
termSignatureExternalLabeledDependencies,
topLevelComponents,
typecheckedToTypeLookup,
typecheckedUnisonFile,
Unison.UnisonFile.rewrite,
prepareRewrite,
Expand Down Expand Up @@ -368,6 +369,15 @@ declsToTypeLookup uf =
where
wrangle = Map.fromList . Map.elems

typecheckedToTypeLookup :: TypecheckedUnisonFile v a -> TL.TypeLookup v a
typecheckedToTypeLookup tuf =
TL.TypeLookup
mempty
(wrangle (dataDeclarations' tuf))
(wrangle (effectDeclarations' tuf))
where
wrangle = Map.fromList . Map.elems

-- Returns true if the file has any definitions or watches
nonEmpty :: TypecheckedUnisonFile v a -> Bool
nonEmpty uf =
Expand Down
14 changes: 8 additions & 6 deletions unison-cli/src/Unison/Codebase/Editor/HandleInput/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Unison.UnisonFile (TypecheckedUnisonFile)
import Unison.UnisonFile qualified as UF
import Unison.UnisonFile.Names qualified as UF
import Unison.Util.Defns (Defns (..))
import Unison.Util.Monoid qualified as Monoid
import Unison.Util.Recursion
import Unison.Var qualified as Var

Expand Down Expand Up @@ -110,29 +111,30 @@ getTerm' mainName =
mainToFile (MainTerm.BadType _ ty) = pure $ maybe NoTermWithThatName TermHasBadType ty
mainToFile (MainTerm.Success hq tm typ) =
let v = Var.named (HQ.toText hq)
in checkType typ \otyp ->
in checkType Nothing typ \otyp ->
pure (GetTermSuccess (v, tm, typ, otyp))
getFromFile uf = do
let components = join $ UF.topLevelComponents uf
-- __TODO__: We shouldn’t need to serialize mainName` for this check
let mainComponent = filter ((\v -> Var.name v == HQ.toText mainName) . view _1) components
case mainComponent of
[(v, _, tm, ty)] ->
checkType ty \otyp ->
checkType (Just uf) ty \otyp ->
let runMain = DD.forceTerm a a (Term.var a v)
v2 = Var.freshIn (Set.fromList [v]) v
a = ABT.annotation tm
in pure (GetTermSuccess (v2, runMain, ty, otyp))
_ -> getFromCodebase
checkType :: Type Symbol Ann -> (Type Symbol Ann -> Cli GetTermResult) -> Cli GetTermResult
checkType ty f = do
checkType :: Maybe (TypecheckedUnisonFile Symbol Ann) -> Type Symbol Ann -> (Type Symbol Ann -> Cli GetTermResult) -> Cli GetTermResult
checkType mayTuf ty f = do
Cli.Env {codebase, runtime} <- ask
case Typechecker.fitsScheme ty (Runtime.mainType runtime) of
True -> do
typeLookup <-
tlCodebase <-
Cli.runTransaction $
Codebase.typeLookupForDependencies codebase Defns {terms = Set.empty, types = Type.dependencies ty}
f $! synthesizeForce typeLookup ty
let tlTuf = Monoid.fromMaybe (fmap UF.typecheckedToTypeLookup mayTuf)
f $! synthesizeForce (tlTuf <> tlCodebase) ty
False -> pure (TermHasBadType ty)
in Cli.getLatestTypecheckedFile >>= \case
Nothing -> getFromCodebase
Expand Down
12 changes: 12 additions & 0 deletions unison-src/transcripts/idempotent/fix5448.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
``` unison :hide
type NewType = NewType
main = do NewType
```

You shouldn't have to `add` a type before using it with `run`.

``` ucm
scratch/main> run main

NewType
```
Loading