Slightly improved rustc error messages for invalid -C arguments
diff --git a/src/librustc/driver/config.rs b/src/librustc/driver/config.rs
index 8c1d7c8..b60676e 100644
--- a/src/librustc/driver/config.rs
+++ b/src/librustc/driver/config.rs
@@ -268,8 +268,21 @@
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 @@
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;
diff --git a/src/librustc/driver/mod.rs b/src/librustc/driver/mod.rs
index edd82b4..546469c 100644
--- a/src/librustc/driver/mod.rs
+++ b/src/librustc/driver/mod.rs
@@ -299,14 +299,10 @@
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
index 0000000..e439b27
--- /dev/null
+++ b/src/test/run-make/codegen-options-parsing/Makefile
@@ -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
index 0000000..8ae3d07
--- /dev/null
+++ b/src/test/run-make/codegen-options-parsing/dummy.rs
@@ -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() {}