about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <nnethercote@mozilla.com>2020-04-02 15:47:11 +1100
committerNicholas Nethercote <nnethercote@mozilla.com>2020-04-19 19:53:54 +1000
commit94b93d3c1c91a78bc50fee44415fc2e9e1be3acd (patch)
tree042d8996218282c4d1a54252132f5849ae127619
parent2109464cc5b0553c1016dd7acee0951c99796fae (diff)
downloadrust-94b93d3c1c91a78bc50fee44415fc2e9e1be3acd.tar.gz
rust-94b93d3c1c91a78bc50fee44415fc2e9e1be3acd.zip
Allow all boolean options to take values.
They now all accept yes/no/y/n/on/off values. (Previously only some of
them did.)

This commit also makes `parse_bool` and `parse_opt_bool` more concise
and readable, and adds some helpful comments to some functions.
-rw-r--r--src/librustc_session/options.rs50
1 files changed, 22 insertions, 28 deletions
diff --git a/src/librustc_session/options.rs b/src/librustc_session/options.rs
index b759887cf5c..525dc7fe0d5 100644
--- a/src/librustc_session/options.rs
+++ b/src/librustc_session/options.rs
@@ -236,9 +236,8 @@ macro_rules! options {
 
     #[allow(non_upper_case_globals, dead_code)]
     mod $mod_desc {
-        pub const parse_bool: Option<&str> = None;
-        pub const parse_opt_bool: Option<&str> =
-            Some("one of: `y`, `yes`, `on`, `n`, `no`, or `off`");
+        pub const parse_bool: Option<&str> = Some("one of: `y`, `yes`, `on`, `n`, `no`, or `off`");
+        pub const parse_opt_bool: Option<&str> = parse_bool;
         pub const parse_string: Option<&str> = Some("a string");
         pub const parse_string_push: Option<&str> = Some("a string");
         pub const parse_pathbuf_push: Option<&str> = Some("a path");
@@ -310,52 +309,45 @@ macro_rules! options {
             }
         )*
 
-        /// Set a flag to true. Note that it cannot set the flag to false, so
-        /// using this parser in combination with a flag that defaults to true
-        /// is useless; the flag will always be true.
+        /// Use this for any boolean option that has a static default.
         fn parse_bool(slot: &mut bool, v: Option<&str>) -> bool {
             match v {
-                Some(..) => false,
-                None => { *slot = true; true }
+                Some("y") | Some("yes") | Some("on") | None => { *slot = true; true }
+                Some("n") | Some("no") | Some("off") => { *slot = false; true }
+                _ => false,
             }
         }
 
+        /// Use this for any boolean option that lacks a static default. (The
+        /// actions taken when such an option is not specified will depend on
+        /// other factors, such as other options, or target options.)
         fn parse_opt_bool(slot: &mut Option<bool>, v: Option<&str>) -> bool {
             match v {
-                Some(s) => {
-                    match s {
-                        "n" | "no" | "off" => {
-                            *slot = Some(false);
-                        }
-                        "y" | "yes" | "on" => {
-                            *slot = Some(true);
-                        }
-                        _ => { return false; }
-                    }
-
-                    true
-                },
-                None => { *slot = Some(true); true }
+                Some("y") | Some("yes") | Some("on") | None => { *slot = Some(true); true }
+                Some("n") | Some("no") | Some("off") => { *slot = Some(false); true }
+                _ => false,
             }
         }
 
-        fn parse_opt_string(slot: &mut Option<String>, v: Option<&str>) -> bool {
+        /// Use this for any string option that has a static default.
+        fn parse_string(slot: &mut String, v: Option<&str>) -> bool {
             match v {
-                Some(s) => { *slot = Some(s.to_string()); true },
+                Some(s) => { *slot = s.to_string(); true },
                 None => false,
             }
         }
 
-        fn parse_opt_pathbuf(slot: &mut Option<PathBuf>, v: Option<&str>) -> bool {
+        /// Use this for any string option that lacks a static default.
+        fn parse_opt_string(slot: &mut Option<String>, v: Option<&str>) -> bool {
             match v {
-                Some(s) => { *slot = Some(PathBuf::from(s)); true },
+                Some(s) => { *slot = Some(s.to_string()); true },
                 None => false,
             }
         }
 
-        fn parse_string(slot: &mut String, v: Option<&str>) -> bool {
+        fn parse_opt_pathbuf(slot: &mut Option<PathBuf>, v: Option<&str>) -> bool {
             match v {
-                Some(s) => { *slot = s.to_string(); true },
+                Some(s) => { *slot = Some(PathBuf::from(s)); true },
                 None => false,
             }
         }
@@ -417,6 +409,7 @@ macro_rules! options {
             }
         }
 
+        /// Use this for any uint option that has a static default.
         fn parse_uint(slot: &mut usize, v: Option<&str>) -> bool {
             match v.and_then(|s| s.parse().ok()) {
                 Some(i) => { *slot = i; true },
@@ -424,6 +417,7 @@ macro_rules! options {
             }
         }
 
+        /// Use this for any uint option that lacks a static default.
         fn parse_opt_uint(slot: &mut Option<usize>, v: Option<&str>) -> bool {
             match v {
                 Some(s) => { *slot = s.parse().ok(); slot.is_some() }