1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-06 08:07:37 +03:00
Commit Graph

86 Commits

Author SHA1 Message Date
Viktor Kleen
19bed669f9
Track more metrics (#1635)
* Track thunk allocation

* Track `Program::new_from_files` as `Program::new`

* Track environment inserts and gets

* Implement tracking histograms

* Track environment layer size on `get`

* Track number of environment layers traversed on `get`

* Update core/src/environment.rs

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>

* Formatting

---------

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
2023-09-28 16:31:16 +00:00
Yann Hamdaoui
f06bda9239
Implement contract deduplication optimization (#1631)
* Duplicate contrat elision: first tentative

* Fix contract deduplication

The previous attempt wasn't fruitful, because we don't pass the initial
environment to the contract equality checker. This commit thread the
initial environment through the virtual machine and various functions to
make it available to the contract deduplicator

* Use contract deduplication for annotations as well

Use a special contract deduplication to avoid pretty printing many many
identical type annotations when evaluating. The actual contract
applications were already elided as far as evaluation is concerned, but
contracts annotations were still accumulated without deduplication.

* Rename elision->dedup which is better descriptive

* Fix clippy warning

* Fix cargo doc warnings

* Cosmetic improvements

* Mention idempotency assumption in the manual

* Update core/src/typecheck/eq.rs

Co-authored-by: Taeer Bar-Yam <Radvendii@users.noreply.github.com>

* Fix REPL query not working anymore

* Update core/src/typecheck/eq.rs

Co-authored-by: jneem <joeneeman@gmail.com>

* Formatting

* Fix clippy error

* Update core/src/program.rs

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

---------

Co-authored-by: Taeer Bar-Yam <Radvendii@users.noreply.github.com>
Co-authored-by: jneem <joeneeman@gmail.com>
Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>
2023-09-28 16:23:43 +00:00
Viktor Kleen
c4b7e0dd5c
Implement a first solution for collecting metrics during a Nickel evaluation (#1633)
* Implement prototype metrics collection and reporting

* Add a few sample metrics

* Document some of the rationale

* Remove unused `metrics-utils`

* Don't enable metrics by default

* Report metrics to stderr
2023-09-27 10:55:03 +00:00
Viktor Kleen
0433886d11
Make eval an explicit subcommand and restructure the CLI somewhat (#1632)
* Make `eval` CLI explicit and remove `-f`

* Implement multiple file inputs as merging

* Use `add_string` instead of `add_term`

* `format` cannot create files anymore

* Update snapshot tests

* Separate customize mode from the `export` subcommand

* Decouple customize mode from export and make it available for every evaluation

* Disable an `unused_mut` warning

* Apply suggestions from code review

Co-authored-by: jneem <joeneeman@gmail.com>

* Add a short option for `export --format`

* Update cli/src/eval.rs

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>

---------

Co-authored-by: jneem <joeneeman@gmail.com>
Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
2023-09-26 20:31:30 +00:00
Erin van der Veen
83d189c0c1
Pin and update Topiary (#1624) 2023-09-22 13:44:47 +00:00
Viktor Kleen
f3d7430476
Remove the double contract application introduced in #1194 (#1625)
Because of #1228 we syntactically forbid contracts from containing free
type variables. This was implemented in #1271 and #1272. As a result,
the double contract application in #1194 became dead code. This PR
removes the `%dualize%` primop, the `dualize` field in `Label` and the
double application logic when constructing recursive environments for
records.
2023-09-22 13:44:10 +00:00
Viktor Kleen
17adb43f87
Run a REPL session for nickel #repl snippets (#1615)
* Run a REPL session when checking `nickel #repl` snippets

* Adjust examples in the manual for new checks

* Fix some leftover error messages

* Address code review
2023-09-22 11:35:30 +00:00
Yann Hamdaoui
71d62de379
Assume -> ApplyContract (#1617)
* Assume -> ApplyContract

At the very beginning of Nickel, the duality between a type annotation and
contract annotation was reflected in their original naming, the assume
construct (contract annotation) and the promise construct (a type
annotation).

This terminology isn't used anymore, and isn't very telling for new
contributors. This PR get rid of old references to `Assume` and
`Promise`, and rename the `Assume` primitive operation to the more
boring but more explicit `ApplyContract`.

* Formatting
2023-09-21 17:33:53 +00:00
jneem
df15becc5d
Context completion (#1584)
* Add context completion

* Review comments

* Use either record completion or env completion, but not both

* Add test case for env completion

* Fix compilation
2023-09-21 14:33:53 +00:00
jneem
7937e5a175
Move goto away from the linearizer (#1610)
* Move goto away from the linearizer

* Add regression test for 881

* Add tests, add ident lookup

* Review comments

* Better name
2023-09-19 20:30:56 +00:00
Viktor Kleen
b02961324d
Testing Nickel snippets in the manual (#1611)
* First pass on testing snippets in `manual/syntax.md`

* Remove explicit `TestCase`

* Remove explicit panic

* Add test annotations to the manual

* Stop filtering markdown files

* Update `typing.md` example

* Apply suggestions from code review

Co-authored-by: jneem <joeneeman@gmail.com>

* Some tweaks to multi line string examples

* Move from `nickel-...` info strings to `nickel #...`

---------

Co-authored-by: jneem <joeneeman@gmail.com>
2023-09-19 13:09:58 +00:00
gtsiam
365e14da36
Cleanup the code a bit (a lot) (#1595)
* Removed explicit state from Traverse trait

* Make clippy happy

* Consistent whitespace between documented items

I kept running into this while going through the codebase,
so I thought I'd fix it once and for all.

And no, I did not do this by hand. So I might have missed some cases.

* More formatting.

Mainly includes wrapping of comments to the 100 column mark,
as well as some more formatting on macros and strings.

* restore try_map_state in Traverse impl

* Removed IdentKind

It was technically only used in the PartialEq implmentation of Thunk.
Still, I don't think that had any effect.

* Fix doc comment

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@gmail.com>

---------

Co-authored-by: Taeer Bar-Yam <taeer.bar-yam@tweag.io>
Co-authored-by: Yann Hamdaoui <yann.hamdaoui@gmail.com>
Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
2023-09-18 11:38:33 +00:00
jneem
b2459d3172
Type-based completion in the new completer (#1577)
* Collect types into a table

* Type-based completion

This also adds a feature flag for turning off the old completer.

* Remove bogus file

* Explicitly visit type annotations

* Add a test for annotated types

* Only do one linearization traversal

* Fix docs, and avoid some clones

* Combine, and resolve metadata

* Add a test

* Slight improvements to the lsp spec

* Refactor

* Remove commented-out code

* Remove the confusing initial-env thing

* Complete the refactor, and document a little better

* Fix some rebase issues

* Clean up some unused code

* Remove obsolete doc

* Review comments
2023-09-15 19:19:45 +00:00
Yann Hamdaoui
27b8c0ede5
Backport 1.2.0 release to master (#1603)
* Bump versions for 1.2.0 release

* Update Cargo.lock

* Update RELEASING guide

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Add 1.2.0 release notes

* Depend from published tree-sitter-nickel

* Add warning about crates.io and format feature in READMEs

* Fix formatting_external borrowing issue

---------

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>
2023-09-14 18:14:32 +00:00
Viktor Kleen
fca5046c43
Put embedded topiary formatting behind a feature flag for NLS (#1604)
* Prevent unused import warnings when building without `doc`

* Put embedded topiary formatting behind a feature flag for NLS, as well

* Stop sending a formatting edit if nothing changed
2023-09-14 12:59:53 +00:00
Yann Hamdaoui
0eb45e8feb
Remove recursive priorities (#1600)
Recursive priorites were added pre-RFC005, where their semantics was
straightforward. Because metadata could appear anywhere, recursive
priorities (or also called push priorities) just amounted to have a
primop that pushed the priorities down a term, in one lazy step.

Since RFC005, this isn't possible anymore, because only record field can
hold metadata. It's now much more intricate to implement, because when
encountering a `rec force` at the field level, we first need to know if
there's any value able to receive it underneath this field to know if we
should attach it to the field or no. To know that, we might need to
evaluate the field first, and then decide.

Before sorting out both the new semantics and implementation, this
commit disable the syntax (but keep the rest of the machinery) from
1.2.0 so that we can do some design before shipping new recursive
priorities or scraping them entirely. Recursive priorities were
undocumented, and should have been disabled from 1.0.0, so it's not
considered a breaking change.
2023-09-13 13:04:19 +00:00
Viktor Kleen
7c9e771ddd
Statically merge partial record definitions (#1599)
* Use `try_str_chunk_as_static_str` instead of duplicating the logic

* Simplify `merge_fields`

* Statically merge piecewise record definitions

* Replicate the behaviour of `eval::merge::merge_fields` more closely
2023-09-13 08:51:26 +00:00
Yann Hamdaoui
c79bc9e6f6
Fix LSP regression (#1583)
* Fix LSP not showing hover information

After the bidirectional refactoring (better separating the rules between
`infer` and `check`), the LSP was experiencing regressions, not showing
hover information in some cases, in particular within statically typed
blocks.

It turns out the separation between `infer` and `check` makes less clear
which function is responsible for adding the term to the linearizer:
sometimes it's `check`, sometimes it's `infer`, and it really depends on
the call graph and where each function is called from.

It turns out the previous implementation was adding many terms in
double, and this caused the initial regression observed.

Morever, fixing is made hard by the fact that `infer` now doesn't have a
type to provide right now for the linearizer: it first need to, well,
infer it. But the linearizer currently relies on AST nodes being visited
in order, so we can't either call to it whenever we want.

The situation seemed intricate enough that it was easier to separate
"visiting the term" from "amending the type of the term". This commit
thus adds a `retype` method to the `Linearizer` trait, plus some
id-related signature changes. Now, the typechecking code can add a term
to the linearizer as soon as it visits it, and set the type later. More
discipline has been introduced so that `check` and `infer` don't repeat
the same term visits anymore.

Doing so, this commit also cleans a bit the LSP implementation which was
very hard to change getting it right: `IdGen` in particular is gone
(which was maintaining a counter by hand in parallel to the
linearization's size, which was very error-prone, and if any mismatch
occurred, anything could happen). The truth is, the refactor should be
much more involved to make the code maintainable, but this commit is
just a fix for the 1.2.0 release to get out, and the linearizer is a
legacy component that will be scraped soon when the AST-based LSP will
reach feature parity with the linearizer.

* Add Hover capability to tests

* Add regression test for hover after bidir pull request

* Formatting and code clean-up

* Apply suggestions from code review

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Make retype calls less verbose

* Fix documentation dangling link

---------

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>
2023-09-11 15:35:04 +00:00
suimong
20ae8e4bdf
Implement curried dot operator (#1578)
* display icon for nickel file in vscode explorer

* implemented with DynAccess.

* added tests.

* Update grammar.lalrpop

move implementation inside CurriedOp

---------

Co-authored-by: Ben Yang <ben@ya.ng>
2023-09-06 07:43:04 +00:00
Viktor Kleen
305d1a4488
Normalize line endings in string literals during parsing (#1562)
* Remove unused `strip_indent_doc`

* Replace `\r\n` by `\n` in the string literal parser

* `mk_strchunk_literal` -> `StrChunk::normalized_literal`

* Move string normalizing into the lexer

* Remove leftover debug print
2023-09-04 15:11:32 +00:00
Yann Hamdaoui
d2316b6588
Revert to special casing the parsing of "%...%{ in multiline strings (#1566)
* Revert "[Fix] Fix bad lexing of mutline string closing delimiter (#1435)"

This reverts commit 664c397aad.

After more investigation, it turns out the original change made it very
annoying to interpolate something between double quotes, because it
conflicts with the multiline string end delimiter:

m%" "%{hello}" "%

After the reverted change, this wouldn't parse correctly anymore. One
can escape the first quote, but it's verbose and harder to read:

m%" %{"\""}%{hello}" "%

Although, we can't see any case where an interpolation sequence with a
heading quotes would have a valid meaning in the reverted commit's
model: it's always an error (applying a string to something). Thus, if
there's a sequence `"%{` in a string, there is no other meaningful
interpretation than this is a quote followed by an interpolation
sequence.

We thus decided to revert this change, although it derives a bit from
the most simple interpretation of parsing multiline string.

* Post-revert fixup: restore some comments and renamings

* Document multiline string parsing special case

* Update core/src/parser/lexer.rs

Co-authored-by: jneem <joeneeman@gmail.com>

* Fix interpolation sequence in pretty printer

---------

Co-authored-by: jneem <joeneeman@gmail.com>
2023-09-04 10:12:22 +00:00
jneem
8f2def66ff
Make an enum for different input sources (#1560)
* Make an enum for different input sources

* Fix doc link

* Review comments

* Add a comment
2023-09-01 17:26:09 +00:00
Yann Hamdaoui
b687ea3322
Get rid of uninformative generic notes (#1564)
Upon the failure of a higher-order contracts, we used to include quite
generic notes showing a plain illustration of the kind of higher-order
violation that happened. However, this text was totally generic (the
example didn't relate to the source code at all), quite verbose (around
4-5 lines of notes), and to be honest, not very helpful.

We now have good mechanisms to underline the right part of the function
contract that failed, together with customized label (e.g. "broken by the
caller"), which makes look like the original generic note aren't very
valuable.

This patch ditches those notes altogether.
2023-09-01 17:24:14 +00:00
deotime
f03213c997
(LSP) Import completion (#1561)
* Import completion

* Use `cache::normalize_path` instead of `fs::canonicalize`

* Add cached files as candidates for import autocompletion

* Rename `InputFormat::from_path_buf` to `InputFormat::from_path`

* Deduplication between server cached files and disk files

---------

Co-authored-by: deotime <89555032+DeoTimeTheGithubUser@users.noreply.github.com>
2023-09-01 16:18:41 +00:00
Yann Hamdaoui
3891689a43
Fix incomplete check of row constraints (#1558)
In an attempt to free the allocation of the constraints of a record row
unification variable that is being assigned to some record rows, the
code checking that row constraints are satisfied was removing the
constraints from the global state. Since rows are defined as linked
list, if the constraint wasn't violated on the first row, this function
would recursively call itself. But then the subsequent recursive calls
would try to get the constraints from the state again, to only find an
empty set constraints, as it has been removed just before. The function
thus wouldn't detect constraint violations happening in the tail of the
record rows.

This patch defines a subfunction which passes the initial constraints
along recursive calls, such that they aren't lost during recursion,
instead of trying to get them from the state again at each recursive
call.
2023-09-01 14:40:52 +00:00
Taeer Bar-Yam
b8e292d44b
set record arguments with -- (#1475)
* set arguments with --

Create term that merges command line arguments with program

* Move building the override term into `Program`
* instead of immediately evaluating argument values, use
`ImportResolver::add_string` to make them separate imports.
* Move CLI export into its own file
* Integrate record builder interface
* Make record field paths work correctly
* Evaluate annotations, too, for record normal form
* Make term interfaces a first class concept
* Stop printing terms on export

* Rename eval_for_doc to be less doc-specific

And expand the documentation at the same time.

* Augment FieldInterface with more data

Augment the field interface type - for freeform argument passing in the
CLI - with additional metadata, such as being optional or defined, taken
directly from the original field's metadata. Add a somehow heuristic
(but simple) `is_input` method, which decides if a field is an input and
should have a dedicated argument.

Minor reworking/renaming of other part of the CLI export code around
freeform arguments.

* Restore the path argument, add more documentation

* Use Field/FieldMetadata instead of custom types

In the CLI export function, instead of using a structure that mostly
mirror record fields, we directly use record fields to avoid code
duplication, although fields do carry a bit more information that what
we exactly need.

* Use query printer to render freeform args help

* Continue proper handling of overrides/inputs in CLI

* Fix eval_record_spine, add sources for CLI arguments

* Remove debug println, improve CLI freeform output

* Move around reporting code to error module

* Actually implement --override, add custom error messages

* Make overrides have a `force` priority

* Add some untested tests

* Small rewording of Ident documentation

* Post-rebase fixups + get rid of Merge trait

* Fix snapshot test runner for freeform args

* Fix CLI freeform snapshot tests

* Fix unused import warning

* Formatting

* Review pass, smallfixes and clippy fix

* Fix cargo doc warnings

* Fix 1.72 new clippy warnings

* Update snapshots after extra-args -> customize mode

* Move the Combine trait in its own mod

* Apply suggestions from code review

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Update core/src/term/make/builder.rs

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Remove questionable Option<> layer

* Update cli/src/export.rs

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Update cli/src/export.rs

Co-authored-by: jneem <joeneeman@gmail.com>

* Apply suggestions from code review

Co-authored-by: jneem <joeneeman@gmail.com>

* Add warning about usage of Label::default

---------

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>
Co-authored-by: jneem <joeneeman@gmail.com>
2023-09-01 09:57:30 +00:00
Yann Hamdaoui
182568a8da
flake.lock: Update (#1545)
* flake.lock: Update

Flake lock file updates:

• Updated input 'crane':
    'github:ipetkov/crane/7809d369710abb17767b624f9e72b500373580bc' (2023-08-12)
  → 'github:ipetkov/crane/ef5d11e3c2e5b3924eb0309dba2e1fea2d9062ae' (2023-08-23)
• Updated input 'flake-utils':
    'github:numtide/flake-utils/919d646de7be200f3bf08cb76ae1f09402b6f9b4' (2023-07-11)
  → 'github:numtide/flake-utils/f9e7cf818399d17d347f847525c5a5a8032e4e44' (2023-08-23)
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/d680ded26da5cf104dd2735a51e88d2d8f487b4d' (2023-08-19)
  → 'github:NixOS/nixpkgs/5690c4271f2998c304a45c91a0aeb8fb69feaea7' (2023-08-25)
• Updated input 'rust-overlay':
    'github:oxalica/rust-overlay/598b2f04ed252eb5808b108d7a10084c0c548753' (2023-08-19)
  → 'github:oxalica/rust-overlay/e90223633068a44f0fb62374e0fa360ccc987292' (2023-08-26)
• Updated input 'topiary':
    'github:tweag/topiary/e30432a29b6d3bb606cbb09b022a5ad0598810dc' (2023-07-31)
  → 'github:tweag/topiary/577fe940aa0b9dae478b463bddd1238e20f86e3a' (2023-08-24)

* Run `cargo fmt`

* Fix new clippy lints

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>
2023-08-28 11:01:43 +00:00
deotime
9e6dc8c27d
Add REPL config attribute (#1546) 2023-08-28 07:57:39 +00:00
Viktor Kleen
a153a9b618
Fix contract application order in let bindings and annotations (#1544)
* Fix contract application order in let bindings and annotations

In a let binding `let foo | A | B = ...` and in an infix contract
annotation `foo | A | B` the contracts `A` and `B` were actually being
applied in reverse order. That is, first `B`, then `A`. The contract
application order for record fields was still correct, however.

The root cause was the fact that there were two places with code that
merged separate annotations into a single annotation containing multiple
contracts. With this change, both spots call into the same code and I've
added regression tests to ensure the correct behaviour.

* `Annot` -> `Combine` in comments

* Add a snapshot test for let-binding contract order
2023-08-25 15:03:38 +00:00
Viktor Kleen
6b44916454
Overhaul and improve testing of the pretty printer (#1532)
* Move `Type` pretty printer test into `core/src/pretty.rs`

* Revamp pretty printer for `TypeF::Array`

* Start overhauling and testing the Type pretty printer

* Simplify pretty printer integration test

* Expand pretty printer integration tests to include stdlib

* continue overhauling the pretty printer

* Add tests for "let" pretty printing

* Ignore empty annotations in TypeAnnotation::attach_term

* Get almost all pretty printer tests to pass, missing `pass/strings/symbolic_strings.ncl`

* Use `Term::StrChunks` when desugaring symbolic strings

* Fix missing newlines in multiline strings

* Handle record patterns in let bindings

* `fun` pretty printing

* Update snapshot tests

* Fix pretty-printer dependent test cases

* Remove TODO comment

* Improve formatting for record fields with metadata

* Remove a redundant clone and some redundant calls to `pretty`

* Consistently use `%<stuff>` for internal values

* Address comments from code review

* Derive `Eq` for `OpPos`

* format other binary operators the same way as `&&` and `||`

* Add a comment about special unary operators

* Adjust grouping for field metadata annotations

* Simplify `Array` pretty printing code

* Document the lack of `indoc!` in `pretty_multiline_strings`
2023-08-25 08:50:32 +00:00
jneem
e848fc3e88
Initial handling of incomplete input in nls (#1541)
* WIP

* Working extraction and reparsing

* werks

* Add a test

* Remove unused linearization param

* Cleanups and docs

* Allow quoted strings to count as a single token

* Add some docs about the initial env
2023-08-24 22:15:24 +00:00
Viktor Kleen
28a5e10592
Use std:io::IsTerminal instead of atty (#1540)
The `atty` crate appears to be unmaintained and `std::io::IsTerminal`
provides the same functionality since rust 1.70.
2023-08-23 15:20:19 +00:00
jneem
63e2c1d19c
Rename symbols to idents (#1539)
* Rename symbols to idents

* Missed a couple
2023-08-22 02:30:56 +00:00
jneem
4e3bca4a3d
Usage/definition lookup table (#1535)
* WIP

WIP

WIP

* Working unit tests checkpoint

* Pass some tests

* A slight refactor and better docs

* Update core/src/destructuring.rs

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Update lsp/nls/src/linearization/mod.rs

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Update lsp/nls/src/position.rs

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Also track bindings in records

* Fix warnings

---------

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>
2023-08-21 16:48:44 +00:00
Viktor Kleen
db12730802
Refactor destructuring bindings (#1537)
* Simplify parsing of record destructuring

* Capture pattern destructuring errors as snapshot tests

* Record the destructuring contract in a label

This makes error reporting point at the destructuring instead of at a
fictitious `Dyn`.
2023-08-21 08:46:02 +00:00
jneem
a55c6488b0
Sort out idents and positions (#1531)
* Make Symbol available.

Ident is a symbol plus a position. The position isn't intended to carry
any semantics -- it's really just for diagnostics -- so it's ignored in
comparison and hashing. This is mostly useful, but sometimes
(particularly in the LSP) we do want meaningful positions. Also, a lot
of code that completely doesn't care about the positions were carrying
one around "by accident."

This PR makes Symbol publicly available, and updates many usages of
Ident to use Symbol instead. There are probably more places that
Symbol could be used.

We do not yet introduce a Symbol + meaningful position type. That will
be done in a future PR, and probably restricted to the LSP.

* two more conversions

* Update core/src/eval/operation.rs

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>

* The big rename

---------

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
2023-08-16 21:06:30 +00:00
Viktor Kleen
3df013169b
Use the original unevaluated type and contract annotation in nickel doc (#1529)
* Use the original unevaluated type and contract annotation in `nickel doc`

With this change we no longer print evaluated types or contracts in
field annotations for `nickel doc`. Ever since `nickel doc` started
evaluating terms to address #1462 contract and type annotations would be
evaluated before reaching the documentation extraction stage. This means
they would be affected by program transformations and the result would
most likely be meaningless to the user.

Incidentally, `nickel query` already correctly used the original
unevaluated type for contract annotations but not for type annotations.
If a type annotation contains a `Term` as a `TypeF::Flat` variant, it
was possible to trigger the same undesirable behaviour:

```
❯ cargo run --bin nickel -- query foo <<<'{ foo : { x | Dyn, y } = {x = 1, y = 2} | { x | Dyn, y } }'
    Finished dev [unoptimized + debuginfo] target(s) in 0.12s
     Running `target/debug/nickel query foo`
• type: let %182 = $dyn in { x | Dyn, y, }

Available fields
• x
• y
```

Fixes #1519

* Add a snapshot test against regressions
2023-08-14 13:34:14 +00:00
jneem
3b5e15109a
Add a position lookup table instead of doing traversal. (#1523)
* Add a position lookup table instead of doing traversal.

* Comments and clippy

* Update lsp/nls/src/position.rs

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>

* Review comments

* Cleanups to the disjointness pass

* cmon rustdoc

---------

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
2023-08-11 22:25:19 +00:00
Viktor Kleen
bb410b8aa3
LSP formatting without calling the topiary binary (#1526)
* Introduce nickel_lang_core::format and use it in the CLI

* Use nickel_lang_core::format for formatting in the LSP

* Update lsp/nls/README.md

* Update lsp/nls/README.md

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>

* Remove an unused import

* Remove the format feature flag for NLS

---------

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
2023-08-11 18:02:21 +00:00
Taeer Bar-Yam
701b7ed538
use get() directly on the terms HashMap because we don't care that the term hasn't had it's imports resolved (#1522) 2023-08-10 15:16:57 +00:00
Viktor Kleen
07be729e76
Use the pretty printer in the Display implementation for Type (#1518)
* Use the pretty printer in the  `Display` implementation for `Type`

We previously used a bespoke formatting algorithm for `Type`. I replaced
the analogous code for `Term`s by the pretty printer in #1262 but we
were worried about some questionable code for contract error reporting
before doing the same for types. Namely, at some point it relied on hard
coded string offsets for pointing at parts of types that were inferred
by Nickel and consequently had no `TermPos`. In #1229 we ripped out
that code and replaced it by reparsing the pretty printer output when
necessary.

Incidentally, this change also fixes some terms being truncated when
formatted. For example, previously

```
SomeUserDefinedContract "that" "takes" "many" "arguments"
```

would be printed as `SomeUserDefinedContract "that" …`. This is somewhat
useful to prevent huge screenfuls of error messages sometimes, but it
makes the `Display` implementation useless for other natural purposes.

* Revert a useless change to snapshot test inputs

* Factor out calling the pretty printer for `Display`
2023-08-10 12:56:40 +00:00
Yann Hamdaoui
8b4d2d4d80
Update strip-ansi-escape deps, remove corresponding temporary fix (#1521) 2023-08-10 11:06:48 +00:00
Yann Hamdaoui
98e84fc977
Add warning for empty query path on the CLI (#1517)
* Add warning for empty query path on the CLI

* Update cli/src/error.rs

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>

* Fix formatting after committing reviewer's suggestion

---------

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>
2023-08-09 17:00:12 +00:00
Théophane Hufschmitt
56c66c6291
Add a linebreak between contract and the documentation in the doc output (#1520)
* Add a linebreak between contract and the documentation in the doc output

The markdown output of `nickel doc` for record fields with both a
contract and a documentation currently looks like

```markdown
- `foo | Dyn`
documentation
```

which in strict markdown is equivalent to

```markdown
- `foo | Dyn` documentation
```

and isn't really nice once converted to Html.

Change that to

```markdown
- `foo | Dyn`
\
documentation
```

which renders `documentation` on a newline (still whithin the same
bullet point though)

* Accept new snapshots

---------

Co-authored-by: Viktor Kleen <viktor.kleen@tweag.io>
2023-08-09 15:24:15 +00:00
jneem
8d092fa36d
Term-based completion (#1504)
* Add terms to the linearization

* Add the reverse map

* Add find_pos

* Remove some panics

* Refactor position-finding

* Small refactor

* Initial version of term-based completion

* oops

* Follow var definitions

* Follow resolved imports

* Fix duplicates, and add some metadata

* Merges

* tests

* Review comments
2023-08-09 14:09:29 +00:00
Yann Hamdaoui
dd58ac16e1
Improve type variables name allocation when reporting type errors (#1512)
* Improve type error reporting

The implementation of the conversion from unification type to
human-readable types was a bit confused, using two name tables, not
updating correctly the set of already used variables, etc.

This commit simplifies the implementation, move free-standing functions
into `NameReg`, with the effect of correctly propagating position
information for user-written type variables and thus improving the error
reporting for the variable level mismath error.

* Do not alter state in into_typecheck_err

* Fix doc linking to private item
2023-08-09 09:04:07 +00:00
Yann Hamdaoui
ddede20f77
Fix typo in std.record.remove documentation (#1513) 2023-08-08 14:35:49 +00:00
Viktor Kleen
84da9e78af
Refactor the Nickel CLI (#1502)
* Refactor the Nickel CLI structure

* Add an `Error` enum for the CLI

* Restore the previous user-facing interface

* Make clippy happy

* Restore `nickel format` CLI behaviour

* Change command options naming

* Properly respect feature flags

* WithProgram -> ReportWithProgram

* Remove the unecessary `Files` struct for now

* ReportWithProgram -> ResultErrorExt

* Suggestions from code review
2023-08-08 11:55:03 +00:00
Yann Hamdaoui
943430e07e
[Fix] Fix contract.Sequence reversing order (#1510)
* Fix contract.Sequence reversing order

This commit started by fixing tests that didn't fire, because they
involved contracts but were never executed. On of those tests failed
after being fixed, indicating that `std.contract.Sequence` wasn't
preserving the order of applied contracts. This commit fixes this issue
as well by using `fold_left` instead of `fold_right` in the
implementation of `Sequence`.

* Formatting (stdlib)
2023-08-08 08:57:19 +00:00
Théophane Hufschmitt
5572b444b0
Disable the colors when stdout isn't a tty (#1508)
Fix https://github.com/tweag/nickel/issues/1500
2023-08-08 08:55:46 +00:00