]> git.lizzy.rs Git - rust.git/commitdiff
Slightly improved rustc error messages for invalid -C arguments
authorinrustwetrust <inrustwetrust@users.noreply.github.com>
Sat, 15 Nov 2014 13:51:22 +0000 (14:51 +0100)
committerinrustwetrust <inrustwetrust@users.noreply.github.com>
Sat, 15 Nov 2014 13:51:22 +0000 (14:51 +0100)
src/librustc/driver/config.rs
src/librustc/driver/mod.rs
src/test/run-make/codegen-options-parsing/Makefile [new file with mode: 0644]
src/test/run-make/codegen-options-parsing/dummy.rs [new file with mode: 0644]

index 8c1d7c839acd004473fc1cb713cfd3ee9bab54cf..b60676eb4c3e3a5a5524ff2a46aad8edf4d72c9a 100644 (file)
@@ -268,8 +268,21 @@ pub fn basic_codegen_options() -> CodegenOptions {
 
     pub type CodegenSetter = fn(&mut CodegenOptions, v: Option<&str>) -> bool;
     pub const CG_OPTIONS: &'static [(&'static str, CodegenSetter,
-                                      &'static str)] =
-        &[ $( (stringify!($opt), cgsetters::$opt, $desc) ),* ];
+                                     Option<&'static str>, &'static str)] =
+        &[ $( (stringify!($opt), cgsetters::$opt, cg_type_descs::$parse, $desc) ),* ];
+
+    #[allow(non_upper_case_globals)]
+    mod cg_type_descs {
+        pub const parse_bool: Option<&'static str> = None;
+        pub const parse_opt_bool: Option<&'static str> = None;
+        pub const parse_string: Option<&'static str> = Some("a string");
+        pub const parse_opt_string: Option<&'static str> = Some("a string");
+        pub const parse_list: Option<&'static str> = Some("a space-separated list of strings");
+        pub const parse_opt_list: Option<&'static str> = Some("a space-separated list of strings");
+        pub const parse_uint: Option<&'static str> = Some("a number");
+        pub const parse_passes: Option<&'static str> =
+            Some("a space-separated list of passes, or `all`");
+    }
 
     mod cgsetters {
         use super::{CodegenOptions, Passes, SomePasses, AllPasses};
@@ -421,19 +434,25 @@ pub fn build_codegen_options(matches: &getopts::Matches) -> CodegenOptions
         let value = iter.next();
         let option_to_lookup = key.replace("-", "_");
         let mut found = false;
-        for &(candidate, setter, _) in CG_OPTIONS.iter() {
+        for &(candidate, setter, opt_type_desc, _) in CG_OPTIONS.iter() {
             if option_to_lookup.as_slice() != candidate { continue }
             if !setter(&mut cg, value) {
-                match value {
-                    Some(..) => {
+                match (value, opt_type_desc) {
+                    (Some(..), None) => {
                         early_error(format!("codegen option `{}` takes no \
                                              value", key).as_slice())
                     }
-                    None => {
+                    (None, Some(type_desc)) => {
                         early_error(format!("codegen option `{0}` requires \
-                                             a value (-C {0}=<value>)",
-                                            key).as_slice())
+                                             {1} (-C {0}=<value>)",
+                                            key, type_desc).as_slice())
+                    }
+                    (Some(value), Some(type_desc)) => {
+                        early_error(format!("incorrect value `{}` for codegen \
+                                             option `{}` - {} was expected",
+                                             value, key, type_desc).as_slice())
                     }
+                    (None, None) => unreachable!()
                 }
             }
             found = true;
index edd82b42876aafed6f28f3b768ac331b8e74c8d1..546469c3c0e5d4735377a795ec8b92470383c798 100644 (file)
@@ -299,14 +299,10 @@ fn describe_debug_flags() {
 
 fn describe_codegen_flags() {
     println!("\nAvailable codegen options:\n");
-    let mut cg = config::basic_codegen_options();
-    for &(name, parser, desc) in config::CG_OPTIONS.iter() {
-        // we invoke the parser function on `None` to see if this option needs
-        // an argument or not.
-        let (width, extra) = if parser(&mut cg, None) {
-            (25, "")
-        } else {
-            (21, "=val")
+    for &(name, _, opt_type_desc, desc) in config::CG_OPTIONS.iter() {
+        let (width, extra) = match opt_type_desc {
+            Some(..) => (21, "=val"),
+            None => (25, "")
         };
         println!("    -C {:>width$s}{} -- {}", name.replace("_", "-"),
                  extra, desc, width=width);
diff --git a/src/test/run-make/codegen-options-parsing/Makefile b/src/test/run-make/codegen-options-parsing/Makefile
new file mode 100644 (file)
index 0000000..e439b27
--- /dev/null
@@ -0,0 +1,24 @@
+-include ../tools.mk
+
+all:
+       #Option taking a number
+       $(RUSTC) -C codegen-units dummy.rs 2>&1 | \
+               grep 'codegen option `codegen-units` requires a number'
+       $(RUSTC) -C codegen-units= dummy.rs 2>&1 | \
+               grep 'incorrect value `` for codegen option `codegen-units` - a number was expected'
+       $(RUSTC) -C codegen-units=foo dummy.rs 2>&1 | \
+               grep 'incorrect value `foo` for codegen option `codegen-units` - a number was expected'
+       $(RUSTC) -C codegen-units=1 dummy.rs
+       #Option taking a string
+       $(RUSTC) -C extra-filename dummy.rs 2>&1 | \
+               grep 'codegen option `extra-filename` requires a string'
+       $(RUSTC) -C extra-filename= dummy.rs 2>&1
+       $(RUSTC) -C extra-filename=foo dummy.rs 2>&1
+       #Option taking no argument
+       $(RUSTC) -C lto= dummy.rs 2>&1 | \
+               grep 'codegen option `lto` takes no value'
+       $(RUSTC) -C lto=1 dummy.rs 2>&1 | \
+               grep 'codegen option `lto` takes no value'
+       $(RUSTC) -C lto=foo dummy.rs 2>&1 | \
+               grep 'codegen option `lto` takes no value'
+       $(RUSTC) -C lto dummy.rs
diff --git a/src/test/run-make/codegen-options-parsing/dummy.rs b/src/test/run-make/codegen-options-parsing/dummy.rs
new file mode 100644 (file)
index 0000000..8ae3d07
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {}