Skip to content

Slightly improved rustc error messages for invalid -C arguments #18979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/librustc/driver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,21 @@ macro_rules! cgoptions(

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};
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 4 additions & 8 deletions src/librustc/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions src/test/run-make/codegen-options-parsing/Makefile
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions src/test/run-make/codegen-options-parsing/dummy.rs
Original file line number Diff line number Diff line change
@@ -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() {}