Commit Graph

350 Commits

Author SHA1 Message Date
Folkert
73bd647a7d
constrain module stuff 2022-03-02 19:56:30 +01:00
Folkert
36c2b78c30
fix copy paste mistake 2022-03-02 19:08:42 +01:00
Folkert
a4889fd571
we are compiling 2022-03-02 18:48:34 +01:00
Folkert
01a7fe77d4
even more wip 2022-03-02 17:32:50 +01:00
Folkert
b8fd6992a2
More wip 2022-03-02 15:29:45 +01:00
Folkert
ec099bbdec
WIP 2022-03-02 14:46:30 +01:00
Folkert
bd31503855
Merge remote-tracking branch 'origin/trunk' into builtins-in-roc 2022-02-28 18:55:20 +01:00
Folkert
db1669154e
WIP 2022-02-27 21:53:56 +01:00
ayazhafiz
07b1829732 Improve error reporting for patterns not matching opaques 2022-02-27 00:11:11 -05:00
ayazhafiz
059c324268 Error reporting for type mismatches involving opaques 2022-02-27 00:10:12 -05:00
ayazhafiz
b6d7229525 Infer + checking tests for opaques 2022-02-27 00:10:12 -05:00
ayazhafiz
86aa0df661 Add constraint generation for opaque types 2022-02-27 00:10:11 -05:00
Folkert
c0d3543d5a
make Str + Result work 2022-02-26 17:52:24 +01:00
Brendan Hansknecht
f7c0e2ef19 Merge remote-tracking branch 'origin/trunk' into single-quote-literal 2022-02-24 10:13:39 -08:00
ayazhafiz
f8c7349193 Simplify pattern constraint generation
At the time we introduced presence constraints for tag unions, I added a
"destruct_position" variable so that we didn't change the typechecking
semantics for everything all at once, and because I wasn't totally sure
what I was doing was correct. But now we're more confident in this
approach, and every pattern is by definition a destructuring, so there
is no need for this flag.

Also should fix some potential bugs we didn't notice before with presence
constraints in closure variables, though I can't find a good test to
reproduce this, since closure variables are hidden from the user.
2022-02-24 01:08:18 -05:00
Folkert
d1d7cfef44
reimplement Result 2022-02-23 17:52:13 +01:00
ayazhafiz
90de82e295 Validation of opaques during canonicalization 2022-02-21 18:25:19 -05:00
ayazhafiz
6b53692aac Canonicalize opaque types 2022-02-20 13:47:01 -05:00
ayazhafiz
c5d918e68c Include floats in bounds for unspecified numbers 2022-02-06 15:04:12 -05:00
ayazhafiz
3fffca48bb Bye bye dead code! 2022-02-06 15:04:12 -05:00
ayazhafiz
c80c842a93 Clippy 2022-02-06 15:04:12 -05:00
ayazhafiz
8dc92ccd97 Second pass 2022-02-06 15:04:12 -05:00
ayazhafiz
5e0d90ac53 First pass 2022-02-06 15:04:12 -05:00
ayazhafiz
e7dcc2daa5 Move NumWidth to roc_can 2022-02-02 00:23:43 -05:00
ayazhafiz
ae5766fdf5 Folkert's suggestions 2022-02-01 23:42:28 -05:00
ayazhafiz
df8113ce32 Typecheck numeric suffixes in patterns 2022-02-01 23:35:14 -05:00
ayazhafiz
a6f7579c07 Parse and expand numeric bounds in canonicalization pass 2022-02-01 22:50:46 -05:00
ayazhafiz
9f72b2710f Run linter 2022-02-01 22:49:52 -05:00
ayazhafiz
e03592930f Typecheck numeric literals with suffixes in expressions
Part of #2350
2022-02-01 22:49:50 -05:00
ayazhafiz
320827167f Parse number literal width suffixes
Supports [u,i][8,16,32,64,128] and [nat,dec]

Part of #2350
2022-02-01 22:48:48 -05:00
ayazhafiz
4e942b3e5d Make nested datatypes into errors
I was hoping to add nested datatypes into the language, but it turns out
doing so is quite tricky and not all that useful with Roc's current
compilation model. Basically every implementation strategy I could think
of ended up requiring a uniform representation for the data layout
(or some ugly workaround). Furhermore it increased the complexity of the
checker/mono IR generator a little bit - basically, we must always pass
around the alias definitions of nested datatypes and instantiate them
at usage sites, rather than being able to unroll aliases as we currently
do during canonicalization.

So, especially because we don't support polymorphic recursion anyway, I
think it may be better to simply disallow any kind of nested datatypes
in the language. In any case, Stephanie Weirich [seems to think nested
datatypes are not needed](https://www.cis.upenn.edu/~plclub/blog/2020-12-04-nested-datatypes/).

Closes #2293
2022-01-31 22:58:01 -05:00
ayazhafiz
0eede1cd86 Generate unique symbols for shadowing identifiers
This code has a shadowing error:

```
b = False
f = \b -> b
f b
```

but prior to this commit, the compiler would hit an internal error
during monomorphization and not even get to report the error. The reason
was that when we entered the closure `\b -> b`, we would try to
introduce the identifier `b` to the scope, see that it shadows an
existing identifier, and not insert the identifier. But this meant that
when checking the body of `\b -> b`, we would think that we captured the
value `b` in the outer scope, but that's incorrect!

The present patch fixes the issue by generating new symbols for
shadowing identifiers, so deeper scopes pick up the correct reference.
This also means in the future we may be able to compile and execute code
with shadows, even though it will still be an error.

Closes #2343
2022-01-23 12:35:31 -05:00
ayazhafiz
8e7ca57458 Close tag unions that are in the left hand side of an assignment 2021-12-30 19:51:14 -06:00
ayazhafiz
fda6c70835 Mark patterns in lambda argument position as having a presence constraint
Closes #2299
2021-12-30 18:21:28 -06:00
ayazhafiz
9a813b6c49 Reorder constraint introduction
This avoids an unnecessary clone if the conditional is false
2021-12-23 19:40:18 -06:00
ayazhafiz
b4c9068676 Make pattern presence constraints an enum variant 2021-12-23 19:40:18 -06:00
ayazhafiz
b97ff380e3 Presence constraints for tag union types
This work is related to restricting tag union sizes in input positions.
As an example, for something like

```
\x -> when x is
    A M -> X
    A N -> X
    A _ -> X
```

we'd like to infer `[A [M, N]* ]` rather than the `[A, [M, N]* ]*` we
infer today. Notice the difference is that the former type tells us we
only accepts `A`s, but the argument of the `A` can be `M`, `N` or
anything else (hence the `_`).

So what's the idea? It's an encoding of the "must have"/"might have"
design discussed in https://github.com/rtfeldman/roc/issues/1758. Let's
take our example above and walk through unification of each branch.

Suppose `x` starts off as a flex var `t`.

```
\x -> when x is
    A M -> X
```

Now we introduce a new kind of constraint called a "presence"
constraint. It says "t has at least [A [M]]". I'll notate this as `t +=
[A [M]]`. When `t` is free as it is here, this is equivalent to `t ~
[A [M]]`.

```
\x -> when x is
    ...
    A N -> X
```

At this branch we introduce the presence constraint `[A [M]] += [A [N]]`.
Notice that there's two tag unions we care about resolving here - one is
the toplevel one that says "I have an `A ...` inside of me", and the
other one is the tag union that's the tyarg to `A`. They are distinct
and at different depths.

For the toplevel one, we first figure out if the number of tags in the
union needs to expand. It does not - we're hoping to resolve the type
`[A [M, N]]`, which only has `A` in the toplevel union. So, we don't
need to do anything extra there, other than the merge the nested tag
unions.

We recurse on the shared tags, and now we have the presence constraint
`[M] += [N]`. At this point it's important to remember that the left and
right hand types are backed by type variables, so this is really
something like `t11 [M] += t12 [N]`, where `[M]` and `[N]` are just what
we know the variables `t11` and `t12` to be at this moment. So how do we
solve for `t11 [M, N]` from here? Well, we can encode this constraint as
a type variable definition and a unification constraint we already know
how to solve:

```
New definition: t11 [M]a    (a fresh)
New constraint: a ~ t12 [N]
```

That's it; upon unification, `t11 [M, N]` falls out.

Okay, last step.

```
\x -> when x is
    ...
    A _ -> X
```

We now have `[A [M, N]] += [A a]`, where `a` is a fresh unbound
variable. Again nothing has to happen on the toplevel. We walk down and
find `t11 [M, N] += t21 a`. This is actually called an "open constraint"; we
differentiate it at the time we generate constraints because it follows
syntactically from the presence of an `_`, but it's semantically
equivalent to the presence constraint `t11 [M, N] += t21 a`. It's just
called opening because literally the only way `t11 [M, N] += t21 a` can
be true is if we set `t11 a`. Well, actually, we assume `a` is a tag
union, so we just make `t11` the open tag union `[M, N]a`. Since `a` is
unbound, this eventually becomes a wildcard and hence falls out `[M, N]*`.
Also, once we open a tag union with an open constraint, we never close
it again.

That's it. The rest falls out recursively. This gives us a really easy
way to encode these ordering constraints in the unification-based system
we have today with minimal additional intervention. We do have to patch
variables in-place sometimes, and the additive nature of these
constraints feels about out-of-place relative to unification, but it
seems to work well.

Resolves #1758
2021-12-23 19:40:18 -06:00
Joshua Warner
22e2545fd6 format 2021-12-22 20:46:42 -08:00
Joshua Warner
f19220473a Rename Located -> Loc 2021-12-22 19:18:22 -08:00
Anton-4
cf80f510cb change import for rust analyzer 2021-12-09 14:55:43 +01:00
Chelsea Troy
6cf755ad8d Resolve a bunnnch of merge conflicts 2021-11-29 23:14:29 -06:00
ayazhafiz
d352d2cdf8 Revert "Include annotation type signatures in Expected struct"
This reverts commit 6e4fd5f06a1ae6138659b0073b4e2b375a499588.

This idea didn't work out because cloning the type and storing it on a
variable still resulted in the solver trying to uify the variable with
the type. When there were errors, which there certainly would be if we
tried to unify the variable with a structure that had nested flex/rigid
vars, the nested flex/rigid vars would inherit those errors, and the
program wouldn't typecheck.

Since the motivation here was to expose the signature type to
`reporting` so that we could modify it with suggestions, we should
instead pass that information along in something analogous to the
`Expected` struct.
2021-11-25 13:24:42 -05:00
ayazhafiz
ee34e79790 Include annotation type signatures in Expected struct
To provide better error messages and suggestions related to changing
type annotations, we now pass annotation type signatures all the way
down through the constraint solver. At constraint generation we
associate the type signature with a unique variable, and during error
reporting, we pull out an `ErrorType` corresponding to the original type
signature, by looking up the unique variable. This gives us two nice
things:

1. It means we don't have to pass the original, AST-like type
   annotation, which can be quite large, to everyone who looks at an
   expectation.
2. It gives us a translation from a `Type` to an `ErrorType` for free
   using the existing translation procedure in `roc_types::subs`,
   without having to create a new translation function.
2021-11-25 11:16:17 -05:00
ayazhafiz
62873fed81 Propogate original annotation region down in AnnotationSource
This makes it easier for error reporting to find the relevant
annotations that were part of a type error, and display that in the
error message presented to a user.
2021-11-25 11:15:31 -05:00
Folkert
3139e73cf9 optimize non-capturing closure type 2021-11-24 23:14:53 +01:00
Folkert
cb8e9acb92 special-case the closure tags 2021-11-24 21:14:58 +01:00
Richard Feldman
763809805b Drop unused alias info 2021-11-21 13:35:23 -05:00
ayazhafiz
30955a1eb8 Take syntactic sugar into account when reporting errors
Previously, a program like

```roc
word = "word"

if True then 1 else "\(word) is a word"
```

would report an error like

```
── TYPE MISMATCH ───────────────────────────────────────────────────────────────

This `if` has an `else` branch with a different type from its `then` branch:

3│  if True then 1 else "\(word) is a word"
                        ^^^^^^^^^^^^^^^^^^

This concat all produces:

    Str

but the `then` branch has the type:

    Num a

I need all branches in an `if` to have the same type!
```

but this is a little bit confusing, since the user shouldn't have to
know (or care) that string interpolations are equivalent to
concatenations under the current implementation.

Indeed we should make this fully transparent. We now word the error
message by taking into account the way calls are made. To support the
case shown above, we introduce the `CalledVia::Sugar` variant to
represent the fact that some calls may be the result of desugaring the
surface syntax.

This commit also demonstrates the usage of `CalledVia` to produce better
error messages where we use binary comparison operators like `<`. There
are more improvements we can make here for all `CalledVia` variants, but
this is a good starting point to demonstrate the usage of the new
procedure.

Closes #1714
2021-11-18 20:20:33 -05:00
Brendan Hansknecht
bddc08c977 Remove unused dependencies 2021-11-05 16:58:11 -07:00
Folkert
c5005d3dd1 Merge remote-tracking branch 'origin/trunk' into partialproc-by-reference 2021-11-03 15:32:39 +01:00
Folkert
a15183a7d1 refactor 2021-11-03 14:09:04 +01:00
Anton-4
a3fc724df3 removed unused maplit 2021-11-03 10:50:25 +01:00
Folkert
dfbdeab6f3 use u32, not unsigned32 2021-10-02 20:24:05 +02:00
Eric Correia
8272ea876f Get it all to compile! 2021-10-02 13:48:07 -04:00
Folkert
72194b87df fix typo 2021-09-24 21:58:25 +02:00
Folkert
23e8f6c687 only introduce rigid once! 2021-09-24 21:53:41 +02:00
Jared Ramirez
d69b9173fc Also store f64 for things like pattern comparisons, etc 2021-08-18 17:18:13 -07:00
Folkert
24ddc4b1e8 further optimizations 2021-08-13 13:25:36 +02:00
Folkert
894ef2b7b2 Merge remote-tracking branch 'origin/trunk' into alias-nominal-equality 2021-07-30 23:34:16 +02:00
Folkert
e805e63db6 typos 2021-07-30 16:54:22 +02:00
Folkert
13b05e54e8 Merge remote-tracking branch 'origin/clippy-1.54' into alias-nominal-equality 2021-07-30 14:25:50 +02:00
Folkert
899cbeabd7 fix extra ampersands 2021-07-29 17:32:08 +02:00
Folkert
27c3d57e35 BROKEN 2021-07-29 17:22:25 +02:00
Folkert
4cefbec5c7 store lambda set in alias types 2021-07-28 15:26:25 +02:00
Folkert
ab7f4a70a1 remove list_var 2021-06-13 16:56:59 +02:00
Anton-4
314503cf7f fixed typos, added typos checking to CI 2021-06-05 20:02:54 +02:00
Richard Feldman
75ec2ecc7b
Merge pull request #1300 from rtfeldman/specialize-lowlevel
Polyvariant defunctionalization
2021-05-24 19:54:17 -04:00
Folkert
e81087f913 Merge remote-tracking branch 'origin/trunk' into specialize-lowlevel 2021-05-24 15:17:28 +02:00
Chadtech
ac7386fd27
Merge branch 'trunk' into hash-links 2021-05-24 09:14:52 -04:00
Folkert
fbb711b2ca Merge remote-tracking branch 'origin/trunk' into specialize-lowlevel 2021-05-24 15:11:40 +02:00
Folkert
f480919186 Merge remote-tracking branch 'origin/trunk' into no-arg-tag-union-func 2021-05-24 14:44:32 +02:00
Richard Feldman
c8665fa9b2
Merge branch 'trunk' into hash-links 2021-05-24 08:25:11 -04:00
Richard Feldman
a7518a675a Clean up some clippy warnings 2021-05-23 20:28:23 -04:00
tarjei
0ee15f15ee Unify FunctionOrTagUnion with regular tags and functions 2021-05-23 23:14:17 +02:00
Chadtech
12c0067348 Created scope and idents for module docs, but they appear to be empty when the doc links function tries to use them 2021-05-22 14:06:56 -04:00
Eric Henry
d34f984169 Starting to add no arg tag union 2021-05-17 17:07:19 -04:00
Folkert
a055fa3626 fix record accessors 2021-05-14 13:07:35 +02:00
Folkert
694a896d54 fix closure size inference 2021-05-09 16:07:52 +02:00
Folkert
86ffd430d7 introduce missing var 2021-05-05 21:35:39 +02:00
Folkert
b01377f868 save the stack 2021-04-23 11:49:32 +02:00
Folkert
903583f1d4 implement basic type error reporting 2021-04-23 09:41:49 +02:00
Folkert
4dd0ee6543 constrain expect 2021-04-23 09:36:12 +02:00
Richard Feldman
0a50b0b488 Change license to UPL-1.0
Closes https://github.com/rtfeldman/roc/issues/1199
2021-04-21 19:18:44 -04:00
Richard Feldman
792a18816a Update authors in Cargo.toml 2021-04-11 08:11:52 -04:00
Folkert
112e97c4a2 improve circular definition error reporting 2021-04-10 21:39:20 +02:00
Folkert
ff3f231155 remove Mode from stdlib 2021-04-01 21:42:38 +02:00
Folkert
ca04411523 fix tests/more uniq removal 2021-02-07 22:50:33 +01:00
Folkert
b80865778e literal patterns should have type Int a or Float a 2021-01-04 23:35:29 +01:00
Jared Ramirez
18d0141942 [WIP] Merge local changes with new branch 2020-12-31 13:08:50 -06:00
rvcas
1ab7dc54ad fix: gen_num tests 2020-12-30 21:18:08 -05:00
rvcas
b510d6e94d fix: float error in repl 2020-12-30 20:47:11 -05:00
rvcas
c33cab57f2 Merge branch 'int-float-size-gen' into temp_numbers 2020-12-30 19:41:44 -05:00
rvcas
127c4e1bcc feat: add inner var to IntLiteral and FloatLiteral 2020-12-29 20:52:54 -05:00
Jared Ramirez
791a0e9404 Use precision in mono/ir to determine which number to generate 2020-12-29 09:33:55 -06:00
Jared Ramirez
48f964adf4 Add percision variable in fp/integer eq constraints 2020-12-28 16:22:34 -06:00
Folkert
caaf8e76c1 reintroduce multiple uniqueness vars in numbers 2020-12-22 20:13:54 +01:00
rvcas
b734b3de58 fix: should not check args.is_empty() 2020-12-22 09:05:58 -05:00
rvcas
e91fc2f1ff fix: clean up uniq and adjust PatternCategory for int type in constrain 2020-12-22 09:05:19 -05:00
rvcas
3ea4858eb5 fix(clippy): do not need let binding 2020-12-22 09:05:19 -05:00
rvcas
d08757f161 fix: uniqueness for Num types 2020-12-22 09:05:19 -05:00
rvcas
aa4b376134 add new layer to the Num types 2020-12-22 09:05:19 -05:00
Anton-4
b085469602 Replaced large_enum_variant explanation with link to issue #800 2020-12-14 16:09:16 +01:00
Richard Feldman
0b620539ef Report accurate regions in unused imports 2020-12-13 22:51:23 -05:00
Richard Feldman
572c7cb3dd Report an error for lookups of unexposed values 2020-12-10 23:59:15 -05:00
Folkert
b669202d86 fix wildcards not recognized as rigids 2020-12-11 01:49:54 +01:00
rvcas
4987721154 feat(Num): rename Num.Int to Num.I64 2020-12-07 19:17:20 -05:00
rvcas
f7bd803509 feat(Num): rename Float to F64 2020-11-24 10:25:21 -05:00
Folkert
197abd8553 change constraints gen to give better errors 2020-11-15 02:06:34 +01:00
Folkert
23fe8e5c07 fix another nasty rigid problem 2020-11-15 00:23:32 +01:00
Folkert
ec3868ed7e small rigid fix 2020-11-14 02:45:05 +01:00
Folkert
815b942f29 cleanup 2020-11-13 01:59:09 +01:00
Folkert
78e0886eb1 clippy 2020-11-13 01:42:31 +01:00
Folkert
bde82c3bb6 add Store constraint that does not report errors 2020-11-12 15:47:56 +01:00
Folkert
75d18eb8ba no more rigid mistakes? 2020-11-12 15:31:25 +01:00
Folkert
7a719172bc more rigid fixes? 2020-11-12 14:55:46 +01:00
Folkert
eaf94f2cfc uncomment more of rbtree 2020-11-12 00:45:47 +01:00
Folkert
21685b6f8f add tests for the rigid issue 2020-11-11 22:55:46 +01:00
Folkert
22592eff80 fix issue with rigids 2020-11-11 22:51:28 +01:00
Folkert
0ba612ecdb refactor 2020-11-11 21:56:25 +01:00
Folkert
62a82d1135 extract untyped args helper 2020-11-11 21:46:12 +01:00
Folkert
d6e7e8381c make it clearer the variables come from the def's pattern 2020-11-11 20:49:04 +01:00
Folkert
984cf744e3 make Storage store where it came from 2020-11-11 01:16:30 +01:00
Folkert
c64a6da745 make Storage store where it came from 2020-11-11 01:14:15 +01:00
Folkert
3aa6455795 fix problem with recursive definition constraint gen 2020-11-10 19:40:35 +01:00
Folkert
3cd132dd51 introduce foreign call 2020-11-05 23:35:56 +01:00
Folkert
2acbfa91f1 remove def_symbol field 2020-11-04 14:59:41 +01:00
Folkert
c09b66a9cc add HostExposedAlias 2020-11-04 13:19:57 +01:00
Folkert
b075c2d394 clean up imports 2020-10-31 15:36:46 +01:00
Folkert
6c578440e0 remove uniqueness alias lifting 2020-10-31 15:35:17 +01:00
Folkert
4bd723b353 remove more unneeded alias things 2020-10-31 15:33:43 +01:00
Folkert
09ad286123 remove unused function 2020-10-31 15:29:36 +01:00
Folkert
8b3b677439 remove aliases from Let and LetCon 2020-10-31 15:27:24 +01:00
Folkert
9e981c264e fix clippy issues 2020-10-30 22:28:55 +01:00
Folkert
58467d6515 get tests back up an running 2020-10-30 16:58:53 +01:00
Folkert
cba841601b fix some uniqueness issues 2020-10-30 01:50:57 +01:00
Folkert
73e0ea2e5d fix some mistakes 2020-10-30 00:35:40 +01:00
Folkert
8a50d48ce2 expand aliases used in constraint gen 2020-10-29 22:31:14 +01:00
Folkert
f13c28fe7f collect and instantiate hidden variables 2020-10-25 19:11:03 +01:00
Folkert
2c55fa5ec5 clippy 2020-10-24 21:23:55 +02:00
Folkert
9d2a4a7d39 move SolvedType -> Type conversion to solved_types.rs 2020-10-24 01:17:51 +02:00
Folkert
e446ecd9a9 factor out closure constraint generation 2020-10-21 13:47:02 +02:00
Folkert
4db09b10aa refactor ClosureLayout 2020-10-21 13:38:46 +02:00
Folkert
c8c1a0678a code gen for variably-sized closures 2020-10-21 13:01:27 +02:00
Folkert
3408a31453 preparations for specializing closures 2020-10-20 00:33:10 +02:00
Folkert
6d866ff58d so defs get different treatment... 2020-10-19 15:44:32 +02:00
Folkert
607799b96e steps towards closures and Effects 2020-10-17 20:53:57 +02:00
Folkert
edfc96628e WIP 2020-10-17 14:53:57 +02:00
Folkert
70a53bd544 cleanup 2020-10-17 00:56:40 +02:00
Folkert
defa408e83 clippy fixes 2020-10-17 00:40:01 +02:00
Folkert
acd275f57c uniqueness closure size inference 2020-10-16 21:07:39 +02:00