changeset 45620:426294d06ddc

rust: move rustfmt.toml to repo root so it can be used by `hg fix` `hg fix` runs the formatters from the repo root so it doesn't pick up the `rustfmt.toml` configs we had in each the `hg-core`, `hg-cpython`, and `rhg` packages, which resulted in warnings about `async fn` not existing in Rust 2015. This patch moves the `rustfmt.toml` file to the root so `hg fix` will use it. By putting the `rustfmt.toml` file in a higher-level directory, it also applies to the `chg` and `hgcli` packages. That makes `test-check-rust-format.t` fail, so this patch also applies the new formatting rules to those packages. Differential Revision: https://phab.mercurial-scm.org/D9142
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 01 Oct 2020 09:09:35 -0700
parents e8078af6af30
children 646a676f5365
files contrib/examples/fix.hgrc rust/chg/src/clientext.rs rust/chg/src/locator.rs rust/chg/src/message.rs rust/chg/src/procutil.rs rust/chg/src/runcommand.rs rust/chg/src/uihandler.rs rust/hg-core/rustfmt.toml rust/hg-cpython/rustfmt.toml rust/hgcli/build.rs rust/hgcli/src/main.rs rust/rhg/rustfmt.toml rustfmt.toml tests/test-check-code.t
diffstat 14 files changed, 113 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/examples/fix.hgrc	Wed Sep 30 18:07:21 2020 +0530
+++ b/contrib/examples/fix.hgrc	Thu Oct 01 09:09:35 2020 -0700
@@ -3,7 +3,7 @@
 clang-format:pattern = set:(**.c or **.cc or **.h) and not "include:contrib/clang-format-ignorelist"
 
 rustfmt:command = rustfmt +nightly
-rustfmt:pattern = set:**.rs
+rustfmt:pattern = set:"**.rs" - "mercurial/thirdparty/**"
 
 black:command = black --config=black.toml -
 black:pattern = set:**.py - mercurial/thirdparty/**
--- a/rust/chg/src/clientext.rs	Wed Sep 30 18:07:21 2020 +0530
+++ b/rust/chg/src/clientext.rs	Thu Oct 01 09:09:35 2020 -0700
@@ -43,11 +43,20 @@
         stdout: &impl AsRawFd,
         stderr: &impl AsRawFd,
     ) -> io::Result<()> {
-        attachio::attach_io(self.client.borrow_protocol_mut(), stdin, stdout, stderr).await
+        attachio::attach_io(
+            self.client.borrow_protocol_mut(),
+            stdin,
+            stdout,
+            stderr,
+        )
+        .await
     }
 
     /// Changes the working directory of the server.
-    pub async fn set_current_dir(&mut self, dir: impl AsRef<Path>) -> io::Result<()> {
+    pub async fn set_current_dir(
+        &mut self,
+        dir: impl AsRef<Path>,
+    ) -> io::Result<()> {
         let dir_bytes = dir.as_ref().as_os_str().as_bytes().to_owned();
         self.client
             .borrow_protocol_mut()
@@ -67,7 +76,10 @@
     }
 
     /// Changes the process title of the server.
-    pub async fn set_process_name(&mut self, name: impl AsRef<OsStr>) -> io::Result<()> {
+    pub async fn set_process_name(
+        &mut self,
+        name: impl AsRef<OsStr>,
+    ) -> io::Result<()> {
         let name_bytes = name.as_ref().as_bytes().to_owned();
         self.client
             .borrow_protocol_mut()
--- a/rust/chg/src/locator.rs	Wed Sep 30 18:07:21 2020 +0530
+++ b/rust/chg/src/locator.rs	Thu Oct 01 09:09:35 2020 -0700
@@ -71,8 +71,12 @@
     }
 
     /// Specifies the arguments to be passed to the server at start.
-    pub fn set_early_args(&mut self, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
-        self.hg_early_args = args.into_iter().map(|a| a.as_ref().to_owned()).collect();
+    pub fn set_early_args(
+        &mut self,
+        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
+    ) {
+        self.hg_early_args =
+            args.into_iter().map(|a| a.as_ref().to_owned()).collect();
     }
 
     /// Connects to the server.
@@ -104,7 +108,10 @@
     /// Runs instructions received from the server.
     ///
     /// Returns true if the client should try connecting to the other server.
-    fn run_instructions(&mut self, instructions: &[Instruction]) -> io::Result<bool> {
+    fn run_instructions(
+        &mut self,
+        instructions: &[Instruction],
+    ) -> io::Result<bool> {
         let mut reconnect = false;
         for inst in instructions {
             debug!("instruction: {:?}", inst);
@@ -123,7 +130,10 @@
                             "insecure redirect instruction from server: {}",
                             path.display()
                         );
-                        return Err(io::Error::new(io::ErrorKind::InvalidData, msg));
+                        return Err(io::Error::new(
+                            io::ErrorKind::InvalidData,
+                            msg,
+                        ));
                     }
                     self.redirect_sock_path = Some(path.to_owned());
                     reconnect = true;
@@ -134,7 +144,10 @@
                             "insecure unlink instruction from server: {}",
                             path.display()
                         );
-                        return Err(io::Error::new(io::ErrorKind::InvalidData, msg));
+                        return Err(io::Error::new(
+                            io::ErrorKind::InvalidData,
+                            msg,
+                        ));
                     }
                     fs::remove_file(path).unwrap_or(()); // may race
                 }
@@ -319,7 +332,10 @@
     P: AsRef<Path>,
 {
     let a = fs::symlink_metadata(path.as_ref())?;
-    if a.is_dir() && a.uid() == procutil::get_effective_uid() && (a.mode() & 0o777) == 0o700 {
+    if a.is_dir()
+        && a.uid() == procutil::get_effective_uid()
+        && (a.mode() & 0o777) == 0o700
+    {
         Ok(path)
     } else {
         Err(io::Error::new(io::ErrorKind::Other, "insecure directory"))
@@ -344,7 +360,9 @@
 }
 
 /// Collects arguments which need to be passed to the server at start.
-pub fn collect_early_args(args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> Vec<OsString> {
+pub fn collect_early_args(
+    args: impl IntoIterator<Item = impl AsRef<OsStr>>,
+) -> Vec<OsString> {
     let mut args_iter = args.into_iter();
     let mut early_args = Vec::new();
     while let Some(arg) = args_iter.next() {
--- a/rust/chg/src/message.rs	Wed Sep 30 18:07:21 2020 +0530
+++ b/rust/chg/src/message.rs	Thu Oct 01 09:09:35 2020 -0700
@@ -32,11 +32,16 @@
 }
 
 /// Parses "S" channel request into command type and spec.
-pub fn parse_command_spec(data: Bytes) -> io::Result<(CommandType, CommandSpec)> {
+pub fn parse_command_spec(
+    data: Bytes,
+) -> io::Result<(CommandType, CommandSpec)> {
     let mut split = data.split(|&c| c == b'\0');
-    let ctype = parse_command_type(split.next().ok_or(new_parse_error("missing type"))?)?;
+    let ctype = parse_command_type(
+        split.next().ok_or(new_parse_error("missing type"))?,
+    )?;
     let command = split.next().ok_or(new_parse_error("missing command"))?;
-    let current_dir = split.next().ok_or(new_parse_error("missing current dir"))?;
+    let current_dir =
+        split.next().ok_or(new_parse_error("missing current dir"))?;
 
     let mut envs = Vec::new();
     for l in split {
@@ -89,14 +94,21 @@
             (b"exit", Some(arg)) => decode_latin1(arg)
                 .parse()
                 .map(Instruction::Exit)
-                .map_err(|_| new_parse_error(format!("invalid exit code: {:?}", arg)))?,
+                .map_err(|_| {
+                    new_parse_error(format!("invalid exit code: {:?}", arg))
+                })?,
             (b"reconnect", None) => Instruction::Reconnect,
             (b"redirect", Some(arg)) => {
                 Instruction::Redirect(OsStr::from_bytes(arg).to_owned().into())
             }
-            (b"unlink", Some(arg)) => Instruction::Unlink(OsStr::from_bytes(arg).to_owned().into()),
+            (b"unlink", Some(arg)) => {
+                Instruction::Unlink(OsStr::from_bytes(arg).to_owned().into())
+            }
             _ => {
-                return Err(new_parse_error(format!("unknown command: {:?}", l)));
+                return Err(new_parse_error(format!(
+                    "unknown command: {:?}",
+                    l
+                )));
             }
         };
         instructions.push(inst);
@@ -118,7 +130,8 @@
 ) -> Bytes {
     let mut vars_iter = vars.into_iter();
     if let Some((k, v)) = vars_iter.next() {
-        let mut dst = BytesMut::with_capacity(INITIAL_PACKED_ENV_VARS_CAPACITY);
+        let mut dst =
+            BytesMut::with_capacity(INITIAL_PACKED_ENV_VARS_CAPACITY);
         pack_env_into(&mut dst, k.as_ref(), v.as_ref());
         for (k, v) in vars_iter {
             dst.reserve(1);
@@ -145,7 +158,9 @@
     s.as_ref().iter().map(|&c| c as char).collect()
 }
 
-fn new_parse_error(error: impl Into<Box<dyn error::Error + Send + Sync>>) -> io::Error {
+fn new_parse_error(
+    error: impl Into<Box<dyn error::Error + Send + Sync>>,
+) -> io::Error {
     io::Error::new(io::ErrorKind::InvalidData, error)
 }
 
@@ -183,17 +198,24 @@
     fn parse_command_spec_too_short() {
         assert!(parse_command_spec(Bytes::from_static(b"")).is_err());
         assert!(parse_command_spec(Bytes::from_static(b"pager")).is_err());
-        assert!(parse_command_spec(Bytes::from_static(b"pager\0less")).is_err());
+        assert!(
+            parse_command_spec(Bytes::from_static(b"pager\0less")).is_err()
+        );
     }
 
     #[test]
     fn parse_command_spec_malformed_env() {
-        assert!(parse_command_spec(Bytes::from_static(b"pager\0less\0/tmp\0HOME")).is_err());
+        assert!(parse_command_spec(Bytes::from_static(
+            b"pager\0less\0/tmp\0HOME"
+        ))
+        .is_err());
     }
 
     #[test]
     fn parse_command_spec_unknown_type() {
-        assert!(parse_command_spec(Bytes::from_static(b"paper\0less")).is_err());
+        assert!(
+            parse_command_spec(Bytes::from_static(b"paper\0less")).is_err()
+        );
     }
 
     #[test]
--- a/rust/chg/src/procutil.rs	Wed Sep 30 18:07:21 2020 +0530
+++ b/rust/chg/src/procutil.rs	Thu Oct 01 09:09:35 2020 -0700
@@ -44,7 +44,8 @@
     if flags < 0 {
         return Err(io::Error::last_os_error());
     }
-    let r = unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) };
+    let r =
+        unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) };
     if r < 0 {
         return Err(io::Error::last_os_error());
     }
@@ -69,7 +70,10 @@
 ///
 /// This touches global states, and thus synchronized as a one-time
 /// initialization function.
-pub fn setup_signal_handler_once(pid: u32, pgid: Option<u32>) -> io::Result<()> {
+pub fn setup_signal_handler_once(
+    pid: u32,
+    pgid: Option<u32>,
+) -> io::Result<()> {
     let pid_signed = pid as i32;
     let pgid_signed = pgid.map(|n| n as i32).unwrap_or(0);
     let mut r = 0;
--- a/rust/chg/src/runcommand.rs	Wed Sep 30 18:07:21 2020 +0530
+++ b/rust/chg/src/runcommand.rs	Thu Oct 01 09:09:35 2020 -0700
@@ -36,7 +36,8 @@
             ChannelMessage::Data(..) => {
                 // just ignores data sent to optional channel
             }
-            ChannelMessage::InputRequest(..) | ChannelMessage::LineRequest(..) => {
+            ChannelMessage::InputRequest(..)
+            | ChannelMessage::LineRequest(..) => {
                 return Err(io::Error::new(
                     io::ErrorKind::InvalidData,
                     "unsupported request",
@@ -49,7 +50,8 @@
                         // server spins new command loop while pager request is
                         // in progress, which can be terminated by "" command.
                         let pin = handler.spawn_pager(&cmd_spec).await?;
-                        attachio::attach_io(proto, &io::stdin(), &pin, &pin).await?;
+                        attachio::attach_io(proto, &io::stdin(), &pin, &pin)
+                            .await?;
                         proto.send_command("").await?; // terminator
                     }
                     CommandType::System => {
--- a/rust/chg/src/uihandler.rs	Wed Sep 30 18:07:21 2020 +0530
+++ b/rust/chg/src/uihandler.rs	Thu Oct 01 09:09:35 2020 -0700
@@ -22,7 +22,10 @@
     /// Handles pager command request.
     ///
     /// Returns the pipe to be attached to the server if the pager is spawned.
-    async fn spawn_pager(&mut self, spec: &CommandSpec) -> io::Result<Self::PagerStdin>;
+    async fn spawn_pager(
+        &mut self,
+        spec: &CommandSpec,
+    ) -> io::Result<Self::PagerStdin>;
 
     /// Handles system command request.
     ///
@@ -53,8 +56,12 @@
 impl SystemHandler for ChgUiHandler {
     type PagerStdin = ChildStdin;
 
-    async fn spawn_pager(&mut self, spec: &CommandSpec) -> io::Result<Self::PagerStdin> {
-        let mut pager = new_shell_command(&spec).stdin(Stdio::piped()).spawn()?;
+    async fn spawn_pager(
+        &mut self,
+        spec: &CommandSpec,
+    ) -> io::Result<Self::PagerStdin> {
+        let mut pager =
+            new_shell_command(&spec).stdin(Stdio::piped()).spawn()?;
         let pin = pager.stdin.take().unwrap();
         procutil::set_blocking_fd(pin.as_raw_fd())?;
         // TODO: if pager exits, notify the server with SIGPIPE immediately.
--- a/rust/hg-core/rustfmt.toml	Wed Sep 30 18:07:21 2020 +0530
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-max_width = 79
-wrap_comments = true
-error_on_line_overflow = true
--- a/rust/hg-cpython/rustfmt.toml	Wed Sep 30 18:07:21 2020 +0530
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-max_width = 79
-wrap_comments = true
-error_on_line_overflow = true
--- a/rust/hgcli/build.rs	Wed Sep 30 18:07:21 2020 +0530
+++ b/rust/hgcli/build.rs	Thu Oct 01 09:09:35 2020 -0700
@@ -5,7 +5,9 @@
 /*! Build script to integrate PyOxidizer. */
 
 fn main() {
-    if let Ok(config_rs) = std::env::var("DEP_PYTHONXY_DEFAULT_PYTHON_CONFIG_RS") {
+    if let Ok(config_rs) =
+        std::env::var("DEP_PYTHONXY_DEFAULT_PYTHON_CONFIG_RS")
+    {
         println!(
             "cargo:rustc-env=PYOXIDIZER_DEFAULT_PYTHON_CONFIG_RS={}",
             config_rs
--- a/rust/hgcli/src/main.rs	Wed Sep 30 18:07:21 2020 +0530
+++ b/rust/hgcli/src/main.rs	Thu Oct 01 09:09:35 2020 -0700
@@ -9,21 +9,22 @@
 include!(env!("PYOXIDIZER_DEFAULT_PYTHON_CONFIG_RS"));
 
 fn main() {
-    // The following code is in a block so the MainPythonInterpreter is destroyed in an
-    // orderly manner, before process exit.
+    // The following code is in a block so the MainPythonInterpreter is
+    // destroyed in an orderly manner, before process exit.
     let code = {
-        // Load the default Python configuration as derived by the PyOxidizer config
-        // file used at build time.
+        // Load the default Python configuration as derived by the PyOxidizer
+        // config file used at build time.
         let config = default_python_config();
 
-        // Construct a new Python interpreter using that config, handling any errors
-        // from construction.
+        // Construct a new Python interpreter using that config, handling any
+        // errors from construction.
         match MainPythonInterpreter::new(config) {
             Ok(mut interp) => {
-                // And run it using the default run configuration as specified by the
-                // configuration. If an uncaught Python exception is raised, handle it.
-                // This includes the special SystemExit, which is a request to terminate the
-                // process.
+                // And run it using the default run configuration as specified
+                // by the configuration. If an uncaught Python
+                // exception is raised, handle it.
+                // This includes the special SystemExit, which is a request to
+                // terminate the process.
                 interp.run_as_main()
             }
             Err(msg) => {
--- a/rust/rhg/rustfmt.toml	Wed Sep 30 18:07:21 2020 +0530
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-max_width = 79
-wrap_comments = true
-error_on_line_overflow = true
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rustfmt.toml	Thu Oct 01 09:09:35 2020 -0700
@@ -0,0 +1,4 @@
+edition = "2018"
+max_width = 79
+wrap_comments = true
+error_on_line_overflow = true
--- a/tests/test-check-code.t	Wed Sep 30 18:07:21 2020 +0530
+++ b/tests/test-check-code.t	Thu Oct 01 09:09:35 2020 -0700
@@ -69,6 +69,7 @@
   hg
   hgeditor
   hgweb.cgi
+  rustfmt.toml
   setup.py
 
 Prevent adding modules which could be shadowed by ancient .so/.dylib.