Skip to content

Commit 3391ddd

Browse files
committed
Slightly improved rustc error messages for invalid -C arguments
1 parent bc0b612 commit 3391ddd

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

src/librustc/driver/config.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,21 @@ macro_rules! cgoptions(
268268

269269
pub type CodegenSetter = fn(&mut CodegenOptions, v: Option<&str>) -> bool;
270270
pub const CG_OPTIONS: &'static [(&'static str, CodegenSetter,
271-
&'static str)] =
272-
&[ $( (stringify!($opt), cgsetters::$opt, $desc) ),* ];
271+
Option<&'static str>, &'static str)] =
272+
&[ $( (stringify!($opt), cgsetters::$opt, cg_type_descs::$parse, $desc) ),* ];
273+
274+
#[allow(non_upper_case_globals)]
275+
mod cg_type_descs {
276+
pub const parse_bool: Option<&'static str> = None;
277+
pub const parse_opt_bool: Option<&'static str> = None;
278+
pub const parse_string: Option<&'static str> = Some("a string");
279+
pub const parse_opt_string: Option<&'static str> = Some("a string");
280+
pub const parse_list: Option<&'static str> = Some("a space-separated list of strings");
281+
pub const parse_opt_list: Option<&'static str> = Some("a space-separated list of strings");
282+
pub const parse_uint: Option<&'static str> = Some("a number");
283+
pub const parse_passes: Option<&'static str> =
284+
Some("a space-separated list of passes, or `all`");
285+
}
273286

274287
mod cgsetters {
275288
use super::{CodegenOptions, Passes, SomePasses, AllPasses};
@@ -421,19 +434,25 @@ pub fn build_codegen_options(matches: &getopts::Matches) -> CodegenOptions
421434
let value = iter.next();
422435
let option_to_lookup = key.replace("-", "_");
423436
let mut found = false;
424-
for &(candidate, setter, _) in CG_OPTIONS.iter() {
437+
for &(candidate, setter, opt_type_desc, _) in CG_OPTIONS.iter() {
425438
if option_to_lookup.as_slice() != candidate { continue }
426439
if !setter(&mut cg, value) {
427-
match value {
428-
Some(..) => {
440+
match (value, opt_type_desc) {
441+
(Some(..), None) => {
429442
early_error(format!("codegen option `{}` takes no \
430443
value", key).as_slice())
431444
}
432-
None => {
445+
(None, Some(type_desc)) => {
433446
early_error(format!("codegen option `{0}` requires \
434-
a value (-C {0}=<value>)",
435-
key).as_slice())
447+
{1} (-C {0}=<value>)",
448+
key, type_desc).as_slice())
449+
}
450+
(Some(value), Some(type_desc)) => {
451+
early_error(format!("incorrect value `{}` for codegen \
452+
option `{}` - {} was expected",
453+
value, key, type_desc).as_slice())
436454
}
455+
(None, None) => unreachable!()
437456
}
438457
}
439458
found = true;

src/librustc/driver/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,10 @@ fn describe_debug_flags() {
299299

300300
fn describe_codegen_flags() {
301301
println!("\nAvailable codegen options:\n");
302-
let mut cg = config::basic_codegen_options();
303-
for &(name, parser, desc) in config::CG_OPTIONS.iter() {
304-
// we invoke the parser function on `None` to see if this option needs
305-
// an argument or not.
306-
let (width, extra) = if parser(&mut cg, None) {
307-
(25, "")
308-
} else {
309-
(21, "=val")
302+
for &(name, _, opt_type_desc, desc) in config::CG_OPTIONS.iter() {
303+
let (width, extra) = match opt_type_desc {
304+
Some(..) => (21, "=val"),
305+
None => (25, "")
310306
};
311307
println!(" -C {:>width$s}{} -- {}", name.replace("_", "-"),
312308
extra, desc, width=width);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-include ../tools.mk
2+
3+
all:
4+
#Option taking a number
5+
$(RUSTC) -C codegen-units dummy.rs 2>&1 | \
6+
grep 'codegen option `codegen-units` requires a number'
7+
$(RUSTC) -C codegen-units= dummy.rs 2>&1 | \
8+
grep 'incorrect value `` for codegen option `codegen-units` - a number was expected'
9+
$(RUSTC) -C codegen-units=foo dummy.rs 2>&1 | \
10+
grep 'incorrect value `foo` for codegen option `codegen-units` - a number was expected'
11+
$(RUSTC) -C codegen-units=1 dummy.rs
12+
#Option taking a string
13+
$(RUSTC) -C extra-filename dummy.rs 2>&1 | \
14+
grep 'codegen option `extra-filename` requires a string'
15+
$(RUSTC) -C extra-filename= dummy.rs 2>&1
16+
$(RUSTC) -C extra-filename=foo dummy.rs 2>&1
17+
#Option taking no argument
18+
$(RUSTC) -C lto= dummy.rs 2>&1 | \
19+
grep 'codegen option `lto` takes no value'
20+
$(RUSTC) -C lto=1 dummy.rs 2>&1 | \
21+
grep 'codegen option `lto` takes no value'
22+
$(RUSTC) -C lto=foo dummy.rs 2>&1 | \
23+
grep 'codegen option `lto` takes no value'
24+
$(RUSTC) -C lto dummy.rs
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {}

0 commit comments

Comments
 (0)