Mercurial > public > mercurial-scm > hg
annotate rust/hg-core/src/revset.rs @ 49000:dd6b67d5c256 stable
rust: fix unsound `OwningDirstateMap`
As per the previous patch, `OwningDirstateMap` is unsound. Self-referential
structs are difficult to implement correctly in Rust since the compiler is
free to move structs around as much as it wants to. They are also very rarely
needed in practice, so the state-of-the-art on how they should be done within
the Rust rules is still a bit new.
The crate `ouroboros` is an attempt at providing a safe way (in the Rust sense)
of declaring self-referential structs. It is getting a lot attention and was
improved very quickly when soundness issues were found in the past: rather than
relying on our own (limited) review circle, we might as well use the de-facto
common crate to fix this problem. This will give us a much better chance of
finding issues should any new ones be discovered as well as the benefit of
fewer `unsafe` APIs of our own.
I was starting to think about how I would present a safe API to the old struct
but soon realized that the callback-based approach was already done in
`ouroboros`, along with a lot more care towards refusing incorrect structs.
In short: we don't return a mutable reference to the `DirstateMap` anymore, we
expect users of its API to pass a `FnOnce` that takes the map as an argument.
This allows our `OwningDirstateMap` to control the input and output lifetimes
of the code that modifies it to prevent such issues.
Changing to `ouroboros` meant changing every API with it, but it is relatively
low churn in the end. It correctly identified the example buggy modification of
`copy_map_insert` outlined in the previous patch as violating the borrow rules.
Differential Revision: https://phab.mercurial-scm.org/D12429
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Tue, 05 Apr 2022 10:55:28 +0200 |
parents | 3da7bf75fdb2 |
children | e98fd81bb151 |
rev | line source |
---|---|
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
1 //! The revset query language |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
2 //! |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
3 //! <https://www.mercurial-scm.org/repo/hg/help/revsets> |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
4 |
46725
df247f58ecee
rhg: Fall back to Python for unsupported revset syntax
Simon Sapin <simon.sapin@octobus.net>
parents:
46433
diff
changeset
|
5 use crate::errors::HgError; |
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
6 use crate::repo::Repo; |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
7 use crate::revlog::revlog::{Revlog, RevlogError}; |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
8 use crate::revlog::NodePrefix; |
46821
e8ae91b1a63d
rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46725
diff
changeset
|
9 use crate::revlog::{Revision, NULL_REVISION, WORKING_DIRECTORY_HEX}; |
e8ae91b1a63d
rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46725
diff
changeset
|
10 use crate::Node; |
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
11 |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
12 /// Resolve a query string into a single revision. |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
13 /// |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
14 /// Only some of the revset language is implemented yet. |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
15 pub fn resolve_single( |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
16 input: &str, |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
17 repo: &Repo, |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
18 ) -> Result<Revision, RevlogError> { |
47959
21d25e9ee58e
rust: Keep lazily-initialized Changelog and Manifest log on the Repo object
Simon Sapin <simon.sapin@octobus.net>
parents:
46821
diff
changeset
|
19 let changelog = repo.changelog()?; |
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
20 |
48078
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
21 match input { |
48079
3da7bf75fdb2
rust-revset: support explicit `.` revision
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48078
diff
changeset
|
22 "." => { |
3da7bf75fdb2
rust-revset: support explicit `.` revision
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48078
diff
changeset
|
23 let p1 = repo.dirstate_parents()?.p1; |
3da7bf75fdb2
rust-revset: support explicit `.` revision
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48078
diff
changeset
|
24 return Ok(changelog.revlog.rev_from_node(p1.into())?); |
3da7bf75fdb2
rust-revset: support explicit `.` revision
Rapha?l Gom?s <rgomes@octobus.net>
parents:
48078
diff
changeset
|
25 } |
48078
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
26 "null" => return Ok(NULL_REVISION), |
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
27 _ => {} |
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
28 } |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
29 |
48078
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
30 match resolve_rev_number_or_hex_prefix(input, &changelog.revlog) { |
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
31 Err(RevlogError::InvalidRevision) => { |
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
32 // TODO: support for the rest of the language here. |
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
33 let msg = format!("cannot parse revset '{}'", input); |
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
34 Err(HgError::unsupported(msg).into()) |
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
35 } |
ddde80830aea
rust-revset: add separate match logic for shortcuts
Rapha?l Gom?s <rgomes@octobus.net>
parents:
47968
diff
changeset
|
36 result => return result, |
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
37 } |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
38 } |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
39 |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
40 /// Resolve the small subset of the language suitable for revlogs other than |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
41 /// the changelog, such as in `hg debugdata --manifest` CLI argument. |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
42 /// |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
43 /// * A non-negative decimal integer for a revision number, or |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
44 /// * An hexadecimal string, for the unique node ID that starts with this |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
45 /// prefix |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
46 pub fn resolve_rev_number_or_hex_prefix( |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
47 input: &str, |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
48 revlog: &Revlog, |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
49 ) -> Result<Revision, RevlogError> { |
47962
8c29af0f6d6e
rhg: Align with Python on some revset parsing corner cases
Simon Sapin <simon.sapin@octobus.net>
parents:
47959
diff
changeset
|
50 // The Python equivalent of this is part of `revsymbol` in |
8c29af0f6d6e
rhg: Align with Python on some revset parsing corner cases
Simon Sapin <simon.sapin@octobus.net>
parents:
47959
diff
changeset
|
51 // `mercurial/scmutil.py` |
8c29af0f6d6e
rhg: Align with Python on some revset parsing corner cases
Simon Sapin <simon.sapin@octobus.net>
parents:
47959
diff
changeset
|
52 |
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
53 if let Ok(integer) = input.parse::<i32>() { |
47962
8c29af0f6d6e
rhg: Align with Python on some revset parsing corner cases
Simon Sapin <simon.sapin@octobus.net>
parents:
47959
diff
changeset
|
54 if integer.to_string() == input |
8c29af0f6d6e
rhg: Align with Python on some revset parsing corner cases
Simon Sapin <simon.sapin@octobus.net>
parents:
47959
diff
changeset
|
55 && integer >= 0 |
8c29af0f6d6e
rhg: Align with Python on some revset parsing corner cases
Simon Sapin <simon.sapin@octobus.net>
parents:
47959
diff
changeset
|
56 && revlog.has_rev(integer) |
8c29af0f6d6e
rhg: Align with Python on some revset parsing corner cases
Simon Sapin <simon.sapin@octobus.net>
parents:
47959
diff
changeset
|
57 { |
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
58 return Ok(integer); |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
59 } |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
60 } |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
61 if let Ok(prefix) = NodePrefix::from_hex(input) { |
46821
e8ae91b1a63d
rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46725
diff
changeset
|
62 if prefix.is_prefix_of(&Node::from_hex(WORKING_DIRECTORY_HEX).unwrap()) |
e8ae91b1a63d
rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46725
diff
changeset
|
63 { |
e8ae91b1a63d
rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46725
diff
changeset
|
64 return Err(RevlogError::WDirUnsupported); |
e8ae91b1a63d
rhg: raise wdir specific error for `hg debugdata`
Pulkit Goyal <7895pulkit@gmail.com>
parents:
46725
diff
changeset
|
65 } |
47968
6f579618ea7b
rust: Rename the `Revlog::get_node_rev` method to `rev_from_node`
Simon Sapin <simon.sapin@octobus.net>
parents:
47962
diff
changeset
|
66 return revlog.rev_from_node(prefix); |
46433
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
67 } |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
68 Err(RevlogError::InvalidRevision) |
4b381dbbf8b7
rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents:
diff
changeset
|
69 } |