Skip to content

Commit 5bc4ab3

Browse files
committed
Try to fix "do" scoping
1 parent 230eb53 commit 5bc4ab3

File tree

6 files changed

+55
-37
lines changed

6 files changed

+55
-37
lines changed

parser-typechecker/package.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ library:
1111
other-modules: Paths_unison_parser_typechecker
1212

1313
dependencies:
14-
- recover-rtti
1514
- ListLike
1615
- aeson
1716
- async

parser-typechecker/src/Unison/Syntax/TermParser.hs

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import Unison.ABT qualified as ABT
3434
import Unison.Builtin.Decls qualified as DD
3535
import Unison.ConstructorReference (ConstructorReference, GConstructorReference (..))
3636
import Unison.ConstructorType qualified as CT
37-
import Unison.Debug qualified as Debug
3837
import Unison.HashQualified qualified as HQ
3938
import Unison.HashQualifiedPrime qualified as HQ'
4039
import Unison.Name (Name)
@@ -111,7 +110,7 @@ rewriteBlock = do
111110
rewriteTermlike kw mk = do
112111
kw <- quasikeyword kw
113112
lhs <- term
114-
(_spanAnn, rhs) <- layoutBlock "==>"
113+
(_openAnn, _spanAnn, rhs) <- layoutBlock "==>"
115114
pure (mk (ann kw <> ann rhs) lhs rhs)
116115
rewriteTerm = rewriteTermlike "term" DD.rewriteTerm
117116
rewriteCase = rewriteTermlike "case" DD.rewriteCase
@@ -233,10 +232,10 @@ matchCase = do
233232
[ Nothing <$ quasikeyword "otherwise",
234233
Just <$> infixAppOrBooleanOp
235234
]
236-
(_spanAnn, t) <- layoutBlock "->"
235+
(_openAnn, _spanAnn, t) <- layoutBlock "->"
237236
pure (guard, t)
238237
let unguardedBlock = label "case match" do
239-
(_spanAnn, t) <- layoutBlock "->"
238+
(_openAnn, _spanAnn, t) <- layoutBlock "->"
240239
pure (Nothing, t)
241240
-- a pattern's RHS is either one or more guards, or a single unguarded block.
242241
guardsAndBlocks <- guardedBlocks <|> (pure @[] <$> unguardedBlock)
@@ -454,10 +453,12 @@ lam p = label "lambda" $ mkLam <$> P.try (some prefixDefinitionName <* reserved
454453
in Term.lam' (ann (head vs) <> ann b) annotatedArgs b
455454

456455
letBlock, handle, ifthen :: (Monad m, Var v) => TermP v m
457-
letBlock = label "let" $ (snd <$> layoutBlock "let")
456+
letBlock = label "let" $ do
457+
(_openAnn, _spanAnn, tm) <- layoutBlock "let"
458+
pure tm
458459
handle = label "handle" do
459-
(handleSpan, b) <- block "handle"
460-
(_withSpan, handler) <- layoutBlock "with"
460+
(_handleOpenAnn, handleSpan, b) <- block "handle"
461+
(_withOpenAnn, _withSpan, handler) <- layoutBlock "with"
461462
-- We don't use the annotation span from 'with' here because it will
462463
-- include a dedent if it's at the end of block.
463464
-- Meaning the newline gets overwritten when pretty-printing and it messes things up.
@@ -492,9 +493,9 @@ lamCase = do
492493

493494
ifthen = label "if" do
494495
start <- peekAny
495-
(_spanAnn, c) <- block "if"
496-
(_spanAnn, t) <- block "then"
497-
(_spanAnn, f) <- layoutBlock "else"
496+
(_ifOpenAnn, _spanAnn, c) <- block "if"
497+
(_thenAnn, _spanAnn, t) <- block "then"
498+
(_elseAnn, _spanAnn, f) <- layoutBlock "else"
498499
pure $ Term.iff (ann start <> ann f) c t f
499500

500501
text :: (Var v) => TermP v m
@@ -628,11 +629,17 @@ doc2Block = do
628629
docTop d = \case
629630
Doc.Section title body -> pure $ Term.apps' (f d "Section") [docParagraph d title, Term.list (gann body) body]
630631
Doc.Eval code ->
631-
Term.app (gann d) (f d "Eval") . addDelay . snd
632-
<$> subParse (block' False False "syntax.docEval" (pure $ pure ()) $ Ann.External <$ P.eof) code
632+
let inner = do
633+
(_openAnn, ann, tm) <- (block' False False "syntax.docEval" (pure $ pure ()) $ Ann.External <$ P.eof)
634+
pure (ann, tm)
635+
in Term.app (gann d) (f d "Eval") . addDelay . snd
636+
<$> subParse inner code
633637
Doc.ExampleBlock code ->
634-
Term.apps' (f d "ExampleBlock") . (Term.nat (gann d) 0 :) . pure . addDelay . snd
635-
<$> subParse (block' False True "syntax.docExampleBlock" (pure $ pure ()) $ Ann.External <$ P.eof) code
638+
let inner = do
639+
(_openAnn, ann, tm) <- (block' False True "syntax.docExampleBlock" (pure $ pure ()) $ Ann.External <$ P.eof)
640+
pure (ann, tm)
641+
in Term.apps' (f d "ExampleBlock") . (Term.nat (gann d) 0 :) . pure . addDelay . snd
642+
<$> subParse inner code
636643
Doc.CodeBlock label body ->
637644
pure $
638645
Term.apps'
@@ -1111,9 +1118,8 @@ delayQuote = P.label "quote" do
11111118

11121119
delayBlock :: (Monad m, Var v) => P v m (Ann {- Ann spanning the whole block -}, Term v Ann)
11131120
delayBlock = P.label "do" do
1114-
(spanAnn, b) <- layoutBlock "do"
1115-
let argSpan = (ann b {- would be nice to use the annotation for 'do' here, but it's not terribly important -})
1116-
pure $ (spanAnn, DD.delayTerm (ann b) argSpan b)
1121+
(openAnn, spanAnn, b) <- layoutBlock "do"
1122+
pure $ (spanAnn, DD.delayTerm (ann b) openAnn b)
11171123

11181124
bang :: (Monad m, Var v) => TermP v m
11191125
bang = P.label "bang" do
@@ -1264,7 +1270,7 @@ destructuringBind = do
12641270
let boundVars' = snd <$> boundVars
12651271
_ <- P.lookAhead (openBlockWith "=")
12661272
pure (p, boundVars')
1267-
(_spanAnn, scrute) <- layoutBlock "=" -- Dwight K. Scrute ("The People's Scrutinee")
1273+
(_eqAnn, _spanAnn, scrute) <- layoutBlock "=" -- Dwight K. Scrute ("The People's Scrutinee")
12681274
let guard = Nothing
12691275
let absChain vs t = foldr (\v t -> ABT.abs' (ann t) v t) t vs
12701276
thecase t = Term.MatchCase p (fmap (absChain boundVars) guard) $ absChain boundVars t
@@ -1310,7 +1316,7 @@ binding = label "binding" do
13101316
Nothing -> do
13111317
-- we haven't seen a type annotation, so lookahead to '=' before commit
13121318
(lhsLoc, name, args) <- P.try (lhs <* P.lookAhead (openBlockWith "="))
1313-
(_bodySpanAnn, body) <- block "="
1319+
(_eqAnn, _bodySpanAnn, body) <- block "="
13141320
verifyRelativeName' (fmap Name.unsafeParseVar name)
13151321
let binding = mkBinding lhsLoc args body
13161322
-- We don't actually use the span annotation from the block (yet) because it
@@ -1323,7 +1329,7 @@ binding = label "binding" do
13231329
when (L.payload name /= L.payload nameT) $
13241330
customFailure $
13251331
SignatureNeedsAccompanyingBody nameT
1326-
(_bodySpanAnn, body) <- block "="
1332+
(_eqAnn, _bodySpanAnn, body) <- block "="
13271333
let binding = mkBinding lhsLoc args body
13281334
-- We don't actually use the span annotation from the block (yet) because it
13291335
-- may contain a bunch of white-space and comments following a top-level-definition.
@@ -1339,10 +1345,30 @@ binding = label "binding" do
13391345
customFailure :: (P.MonadParsec e s m) => e -> m a
13401346
customFailure = P.customFailure
13411347

1342-
block :: forall m v. (Monad m, Var v) => String -> P v m (Ann, Term v Ann)
1348+
block ::
1349+
forall m v.
1350+
(Monad m, Var v) =>
1351+
String ->
1352+
P
1353+
v
1354+
m
1355+
( Ann {- annotation of block-open symbol, e.g. 'do', 'let' -},
1356+
Ann {- annotation for whole block -},
1357+
Term v Ann
1358+
)
13431359
block s = block' False False s (openBlockWith s) closeBlock
13441360

1345-
layoutBlock :: forall m v. (Monad m, Var v) => String -> P v m (Ann, Term v Ann)
1361+
layoutBlock ::
1362+
forall m v.
1363+
(Monad m, Var v) =>
1364+
String ->
1365+
P
1366+
v
1367+
m
1368+
( Ann {- annotation of block-open symbol, e.g. 'do', 'let' -},
1369+
Ann {- annotation for whole layout block -},
1370+
Term v Ann
1371+
)
13461372
layoutBlock s = block' False False s (openBlockWith s) optionalCloseBlock
13471373

13481374
-- example: use Foo.bar.Baz + ++ x
@@ -1421,15 +1447,15 @@ block' ::
14211447
String ->
14221448
P v m (L.Token ()) ->
14231449
P v m end ->
1424-
P v m (Ann {- ann which spans the whole block -}, Term v Ann)
1450+
P v m (Ann {- span for the opening token, e.g. the "do" or opening bracket -}, Ann {- ann which spans the whole block -}, Term v Ann)
14251451
block' isTop implicitUnitAtEnd s openBlock closeBlock = do
14261452
open <- openBlock
14271453
(names, imports) <- imports
14281454
_ <- optional semi
14291455
statements <- local (\e -> e {names}) $ sepBy semi statement
14301456
end <- closeBlock
14311457
body <- substImports names imports <$> go open statements
1432-
pure (ann open <> ann end, body)
1458+
pure (ann open, ann open <> ann end, body)
14331459
where
14341460
statement = asum [Binding <$> binding, DestructuringBind <$> destructuringBind, Action <$> blockTerm]
14351461
go :: L.Token () -> [BlockElement v] -> P v m (Term v Ann)
@@ -1448,7 +1474,6 @@ block' isTop implicitUnitAtEnd s openBlock closeBlock = do
14481474
step elem result = case elem of
14491475
Binding ((a, v), tm) -> do
14501476
let fullLetRecSpan = ann a <> ann result
1451-
Debug.debugM Debug.Temp "letrec" (v, a)
14521477
pure $
14531478
Term.consLetRec
14541479
isTop

parser-typechecker/src/Unison/Typechecker/Context.hs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ import Unison.Typechecker.TypeLookup qualified as TL
105105
import Unison.Typechecker.TypeVar qualified as TypeVar
106106
import Unison.Var (Var)
107107
import Unison.Var qualified as Var
108-
import qualified Unison.Debug as Debug
109-
import Debug.RecoverRTTI (anythingToString)
110108
import Data.Set.NonEmpty (NESet)
111109

112110
type TypeVar v loc = TypeVar.TypeVar (B.Blank loc) v
@@ -1243,7 +1241,6 @@ synthesizeWanted (Term.Let1Top' top binding boundVarAnn e) = do
12431241
when (Var.isAction (ABT.variable e)) $
12441242
-- enforce that actions in a block have type ()
12451243
subtype tbinding (DDB.unitType (ABT.annotation binding))
1246-
Debug.debugM Debug.Temp "Let1Top" (ABT.variable e, v', anythingToString boundVarAnn, tbinding)
12471244
appendContext [Ann v' boundVarAnn tbinding]
12481245
(t, w) <- synthesize (ABT.bindInheritAnnotation e (Term.var () v'))
12491246
t <- applyM t
@@ -1345,7 +1342,6 @@ synthesizeWanted e
13451342
let it = existential' l B.Blank i
13461343
ot = existential' l B.Blank o
13471344
et = existential' l B.Blank e
1348-
Debug.debugM Debug.Temp "synthesizeWanted:Lam" (arg, anythingToString boundVarAnn, i, e, o)
13491345
appendContext $
13501346
[existential i, existential e, existential o, Ann arg boundVarAnn it]
13511347

@@ -1913,7 +1909,6 @@ annotateLetRecBindings isTop letrec =
19131909
pure $ (e, existential' (loc binding) B.Blank vt, vloc)
19141910
(bindings, bindingTypes, vlocs) <- unzip3 <$> traverse f bindings
19151911
appendContext (zipWith3 Ann vs vlocs bindingTypes)
1916-
Debug.debugM Debug.Temp "annotateLetRecBindings" (zip vs (anythingToString <$> vlocs))
19171912
-- check each `bi` against its type
19181913
Foldable.for_ (zip3 vs bindings bindingTypes) $ \(v, b, t) -> do
19191914
-- note: elements of a cycle have to be pure, otherwise order of effects
@@ -1930,7 +1925,6 @@ annotateLetRecBindings isTop letrec =
19301925
bindingTypesGeneralized = zipWith gen bindingTypes bindingArities
19311926
annotations = zipWith3 Ann vs vlocs bindingTypesGeneralized
19321927

1933-
Debug.debugM Debug.Temp "annotateLetRecBindings2" (zip vs (anythingToString <$> vlocs))
19341928
appendContext annotations
19351929
let vTypes = vs `zip` bindingTypesGeneralized
19361930
pure (body, vTypes)
@@ -2466,7 +2460,6 @@ checkWanted want m (Type.Forall' body) = do
24662460
checkWanted want (Term.Lam' boundVarAnn body) (Type.Arrow'' i es o) = do
24672461
x <- ABT.freshen body freshenVar
24682462
markThenRetract0 x $ do
2469-
Debug.debugM Debug.Temp "checkWanted:Lam" (x, anythingToString boundVarAnn)
24702463
extendContext (Ann x boundVarAnn i)
24712464
body <- pure $ ABT.bindInheritAnnotation body (Term.var () x)
24722465
checkWithAbilities es body o

parser-typechecker/unison-parser-typechecker.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ library
219219
, network-uri
220220
, nonempty-containers
221221
, pretty-simple
222-
, recover-rtti
223222
, regex-tdfa
224223
, semialign
225224
, semigroups

unison-cli/src/Unison/LSP/Hover.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ hoverInfo uri pos =
124124

125125
hoverInfoForLocalVar :: MaybeT Lsp Text
126126
hoverInfoForLocalVar = do
127+
Debug.debugM Debug.Temp "pos" pos
127128
let varFromNode = do
128129
node <- LSPQ.nodeAtPosition uri pos
129130
Debug.debugM Debug.Temp "node" node
@@ -140,7 +141,6 @@ hoverInfo uri pos =
140141
FileAnalysis {localBindingTypes} <- FileAnalysis.getFileAnalysis uri
141142
Debug.debugM Debug.Temp "localBindingTypes" localBindingTypes
142143
Debug.debugM Debug.Temp "localVar" localVar
143-
Debug.debugM Debug.Temp "pos" pos
144144
(_range, typ) <- hoistMaybe $ IM.lookupMin $ IM.intersecting localBindingTypes (IM.ClosedInterval pos pos)
145145

146146
pped <- lift $ ppedForFile uri

unison-cli/src/Unison/LSP/Queries.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,10 @@ findSmallestEnclosingNode pos term
240240
Term.And l r -> findSmallestEnclosingNode pos l <|> findSmallestEnclosingNode pos r
241241
Term.Or l r -> findSmallestEnclosingNode pos l <|> findSmallestEnclosingNode pos r
242242
Term.Lam a -> findSmallestEnclosingNode pos a
243-
Term.LetRec _isTop xs y -> findSmallestEnclosingNode pos y <|> altSum (findSmallestEnclosingNode pos <$> xs)
244-
Term.Let _isTop a b -> findSmallestEnclosingNode pos b <|> findSmallestEnclosingNode pos a
243+
Term.LetRec _isTop xs y ->
244+
findSmallestEnclosingNode pos y <|> altSum (findSmallestEnclosingNode pos <$> xs)
245+
Term.Let _isTop a b ->
246+
findSmallestEnclosingNode pos b <|> findSmallestEnclosingNode pos a
245247
Term.Match a cases ->
246248
findSmallestEnclosingNode pos a
247249
<|> altSum (cases <&> \(MatchCase pat grd body) -> ((PatternNode <$> findSmallestEnclosingPattern pos pat) <|> (grd >>= findSmallestEnclosingNode pos) <|> findSmallestEnclosingNode pos body))

0 commit comments

Comments
 (0)