Implement fix. There were lots of tests that were inadvertently relying on the bug.

This commit is contained in:
Paul Chiusano 2020-10-22 22:33:12 -04:00
parent f90dc7bd58
commit e784107d64
14 changed files with 74 additions and 26 deletions

View File

@ -89,7 +89,7 @@ resolveNames typeLookupf preexistingNames uf = do
possibleDeps = [ (Name.toText name, Var.name v, r) |
(name, r) <- Rel.toList (Names.terms0 preexistingNames),
v <- Set.toList (Term.freeVars tm),
Name.unqualified name == Name.unqualified (Name.fromVar v) ]
name `Name.endsWithSegments` Name.fromVar v ]
possibleRefs = Referent.toReference . view _3 <$> possibleDeps
tl <- lift . lift . fmap (UF.declsToTypeLookup uf <>)
$ typeLookupf (deps <> Set.fromList possibleRefs)
@ -98,10 +98,11 @@ resolveNames typeLookupf preexistingNames uf = do
(name, shortname, r) <- possibleDeps,
typ <- toList $ TL.typeOfReferent tl r,
let nr = Typechecker.NamedReference name typ (Right r) ] <>
[ (shortname, nr) |
[ (Var.name v, nr) |
(name, r) <- Rel.toList (Names.terms0 $ UF.toNames uf),
v <- Set.toList (Term.freeVars tm),
name `Name.endsWithSegments` Name.fromVar v,
typ <- toList $ TL.typeOfReferent tl r,
let shortname = Name.toText $ Name.unqualified name,
let nr = Typechecker.NamedReference (Name.toText name) typ (Right r) ]
pure (tm, fqnsByShortName, tl)

View File

@ -166,9 +166,9 @@ unique[b28d929d0a73d2c18eac86341a3bb9399f8550c11b5f35eabb2751e6803ccc20] type
d1 Doc.++ d2 =
use Doc
match (d1,d2) with
(Join ds, Join ds2) -> Join (ds Sequence.++ ds2)
(Join ds, _) -> Join (ds `Sequence.snoc` d2)
(_, Join ds) -> Join (d1 `Sequence.cons` ds)
(Join ds, Join ds2) -> Join (ds List.++ ds2)
(Join ds, _) -> Join (ds `List.snoc` d2)
(_, Join ds) -> Join (d1 `List.cons` ds)
_ -> Join [d1,d2]
unique[q1905679b27a97a4098bc965574da880c1074183a2c55ff1d481619c7fb8a1e1] type

View File

@ -33,6 +33,7 @@ import Unison.Var (Var)
import qualified Unison.Var as Var
import qualified Unison.Typechecker.TypeLookup as TL
import Unison.Util.List ( uniqueBy )
import qualified Unison.Name as Name
type Name = Text
@ -62,7 +63,7 @@ data NamedReference v loc =
data Env v loc = Env
{ _ambientAbilities :: [Type v loc]
, _typeLookup :: TL.TypeLookup v loc
, _unqualifiedTerms :: Map Name [NamedReference v loc]
, _termsBySuffix :: Map Name [NamedReference v loc]
}
makeLenses ''Env
@ -232,8 +233,9 @@ typeDirectedNameResolution oldNotes oldType env = do
addTypedComponent :: Context.InfoNote v loc -> State (Env v loc) ()
addTypedComponent (Context.TopLevelComponent vtts)
= for_ vtts $ \(v, typ, _) ->
unqualifiedTerms %= Map.insertWith (<>)
(Var.unqualifiedName v)
for_ (Name.suffixes . Name.unsafeFromText . Var.name $ Var.reset v) $ \suffix ->
termsBySuffix %=
Map.insertWith (<>) (Name.toText suffix)
[NamedReference (Var.name v) typ (Left v)]
addTypedComponent _ = pure ()
@ -276,7 +278,7 @@ typeDirectedNameResolution oldNotes oldType env = do
. join
. maybeToList
. Map.lookup (Text.pack n)
$ view unqualifiedTerms env
$ view termsBySuffix env
resolveNote _ n = btw n >> pure Nothing
dedupe :: [Context.Suggestion v loc] -> [Context.Suggestion v loc]
dedupe = uniqueBy Context.suggestionReplacement

View File

@ -52,19 +52,19 @@ main = 'let
expected = ${expectedText}
-- Write to myFile
h1 = builtins.io.openFile (FilePath fp) Write
h1 = io.openFile (FilePath fp) Write
putText h1 expected
builtins.io.closeFile h1
io.closeFile h1
-- Read from myFile
h2 = builtins.io.openFile (FilePath fp) Read
h2 = builtin.io.openFile (FilePath fp) Read
myC = getText h2
builtins.io.closeFile h2
io.closeFile h2
-- Write what we read from myFile to resultFile
h3 = builtins.io.openFile (FilePath res) Write
h3 = io.openFile (FilePath res) Write
putText h3 myC
builtins.io.closeFile h3
builtin.io.closeFile h3
```
```ucm

View File

@ -5,6 +5,7 @@
module Unison.Name
( Name(Name)
, endsWithSegments
, fromString
, isPrefixOf
, joinDot
@ -87,6 +88,14 @@ toString = Text.unpack . toText
isPrefixOf :: Name -> Name -> Bool
a `isPrefixOf` b = toText a `Text.isPrefixOf` toText b
-- foo.bar.baz `endsWithSegments` bar.baz == True
-- foo.bar.baz `endsWithSegments` baz == True
-- foo.bar.baz `endsWithSegments` az == False (not a full segment)
-- foo.bar.baz `endsWithSegments` zonk == False (doesn't match any segment)
-- foo.bar.baz `endsWithSegments` foo == False (matches a segment, but not at the end)
endsWithSegments :: Name -> Name -> Bool
endsWithSegments n ending = any (== ending) (suffixes n)
-- stripTextPrefix a.b. a.b.c = Just c
-- stripTextPrefix a.b a.b.c = Just .c; you probably don't want to do this
-- stripTextPrefix x.y. a.b.c = Nothing

View File

@ -1,5 +1,5 @@
foo a b =
if a Text.== "" then
if a `Text.eq` "" then
match Text.size b with
1 -> false
_ -> true

View File

@ -11,7 +11,7 @@ main = '(tell get)
replicate : Nat -> '{e} () -> {e} ()
replicate n x =
if n Nat.== 0 then () else
if n `Nat.eq` 0 then () else
!x
replicate (n `drop` 1) x

View File

@ -1,4 +1,4 @@
use Nat drop >=
use Nat drop
use Optional None Some
search : (Nat -> Int) -> Nat -> Nat -> Optional Nat

View File

@ -1,5 +1,5 @@
use Nat drop >=
use Nat drop
use Optional None Some
search : (Nat -> Int) -> Nat -> Nat -> Optional Nat

View File

@ -7,4 +7,4 @@ unfold s f =
Some (hd, s) -> go s (acc `List.snoc` hd)
go s []
> unfold 0 (n -> if n Nat.< 5 then Some (n, n + 1) else None)
> unfold 0 (n -> if n < 5 then Some (n, n + 1) else None)

View File

@ -1 +1 @@
> .builtin.Nat.toFloat 4
> Nat.toFloat 4

View File

@ -30,7 +30,7 @@ from n = unfold n (n -> Some (n, n + 1))
take n s =
step n = cases
{Emit.emit a -> k} ->
if n Nat.== 0 then ()
if n == 0 then ()
else
Emit.emit a
handle k () with step (n `drop` 1)

View File

@ -6,17 +6,17 @@
foo : Int
foo = +1
-- no imports needed here, even though FQNs are builtin.Optional.{None,Some}
-- no imports needed here, even though FQNs are Optional.{None,Some}
ex1 = cases
None -> 0
Some a -> a + 1
-- you can still use the
-- you can still use the FQN
ex2 = cases
Optional.None -> 99
Optional.Some _ -> 0
ex3 = builtin.Optional.None
ex3 = List.at 0 [ 1, 2, 3, 4 ]
-- TDNR would have handled this one before, but TDNR can't do
-- type resolution or pattern resolution

View File

@ -0,0 +1,36 @@
Add `List.zonk` to the codebase:
```unison
List.zonk : [a] -> [a]
List.zonk xs = xs
```
```ucm
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`:
List.zonk : [a] -> [a]
```
Now, typecheck a file with a reference to `Blah.zonk` (which doesn't exist in the codebase). This should fail:
```unison
-- should not typecheck as there's no `Blah.zonk` in the codebase
> Blah.zonk [1,2,3]
```
```ucm
I'm not sure what Blah.zonk means at line 2, columns 3-12
2 | > Blah.zonk [1,2,3]
Whatever it is, it has a type that conforms to [builtin.Nat] -> o.
```