Skip to content

add overflow-checks field to profiles #3908

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
Apr 18, 2017
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
3 changes: 3 additions & 0 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub struct Profile {
pub rustdoc_args: Option<Vec<String>>,
pub debuginfo: Option<u32>,
pub debug_assertions: bool,
pub overflow_checks: bool,
#[serde(skip_serializing)]
pub rpath: bool,
pub test: bool,
Expand Down Expand Up @@ -528,6 +529,7 @@ impl Profile {
Profile {
debuginfo: Some(2),
debug_assertions: true,
overflow_checks: true,
..Profile::default()
}
}
Expand Down Expand Up @@ -594,6 +596,7 @@ impl Default for Profile {
rustdoc_args: None,
debuginfo: None,
debug_assertions: false,
overflow_checks: false,
rpath: false,
test: false,
doc: false,
Expand Down
29 changes: 23 additions & 6 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ fn build_base_args(cx: &mut Context,
crate_types: &[&str]) {
let Profile {
ref opt_level, lto, codegen_units, ref rustc_args, debuginfo,
debug_assertions, rpath, test, doc: _doc, run_custom_build,
ref panic, rustdoc_args: _, check,
debug_assertions, overflow_checks, rpath, test, doc: _doc,
run_custom_build, ref panic, rustdoc_args: _, check,
} = *unit.profile;
assert!(!run_custom_build);

Expand Down Expand Up @@ -678,10 +678,27 @@ fn build_base_args(cx: &mut Context,
cmd.args(args);
}

if debug_assertions && opt_level != "0" {
cmd.args(&["-C", "debug-assertions=on"]);
} else if !debug_assertions && opt_level == "0" {
cmd.args(&["-C", "debug-assertions=off"]);
// -C overflow-checks is implied by the setting of -C debug-assertions,
// so we only need to provide -C overflow-checks if it differs from
// the value of -C debug-assertions we would provide.
if opt_level != "0" {
if debug_assertions {
cmd.args(&["-C", "debug-assertions=on"]);
if !overflow_checks {
cmd.args(&["-C", "overflow-checks=off"]);
}
} else if overflow_checks {
cmd.args(&["-C", "overflow-checks=on"]);
}
} else {
if !debug_assertions {
cmd.args(&["-C", "debug-assertions=off"]);
if overflow_checks {
cmd.args(&["-C", "overflow-checks=on"]);
}
} else if !overflow_checks {
cmd.args(&["-C", "overflow-checks=off"]);
}
}

if test && unit.target.harness() {
Expand Down
5 changes: 4 additions & 1 deletion src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ pub struct TomlProfile {
debug_assertions: Option<bool>,
rpath: Option<bool>,
panic: Option<String>,
#[serde(rename = "overflow-checks")]
overflow_checks: Option<bool>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -1469,7 +1471,7 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
fn merge(profile: Profile, toml: Option<&TomlProfile>) -> Profile {
let &TomlProfile {
ref opt_level, lto, codegen_units, ref debug, debug_assertions, rpath,
ref panic
ref panic, ref overflow_checks,
} = match toml {
Some(toml) => toml,
None => return profile,
Expand All @@ -1488,6 +1490,7 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
rustdoc_args: None,
debuginfo: debug.unwrap_or(profile.debuginfo),
debug_assertions: debug_assertions.unwrap_or(profile.debug_assertions),
overflow_checks: overflow_checks.unwrap_or(profile.overflow_checks),
rpath: rpath.unwrap_or(profile.rpath),
test: profile.test,
doc: profile.doc,
Expand Down
5 changes: 5 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,7 @@ fn compiler_json_error_format() {
"debug_assertions": true,
"debuginfo": 2,
"opt_level": "0",
"overflow_checks": true,
"test": false
},
"features": [],
Expand Down Expand Up @@ -2574,6 +2575,7 @@ fn compiler_json_error_format() {
"debug_assertions": true,
"debuginfo": 2,
"opt_level": "0",
"overflow_checks": true,
"test": false
},
"features": [],
Expand All @@ -2593,6 +2595,7 @@ fn compiler_json_error_format() {
"debug_assertions": true,
"debuginfo": 2,
"opt_level": "0",
"overflow_checks": true,
"test": false
},
"features": [],
Expand Down Expand Up @@ -2620,6 +2623,7 @@ fn compiler_json_error_format() {
"debug_assertions": true,
"debuginfo": 2,
"opt_level": "0",
"overflow_checks": true,
"test": false
},
"features": [],
Expand Down Expand Up @@ -2679,6 +2683,7 @@ fn message_format_json_forward_stderr() {
"debug_assertions":true,
"debuginfo":2,
"opt_level":"0",
"overflow_checks": true,
"test":false
},
"features":[],
Expand Down
35 changes: 35 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,41 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
"));
}

#[test]
fn cargo_test_overflow_checks() {
if !is_nightly() {
return;
}
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.5.0"
authors = []

[[bin]]
name = "foo"

[profile.release]
overflow-checks = true
"#)
.file("src/foo.rs", r#"
use std::panic;
pub fn main() {
let r = panic::catch_unwind(|| {
[1, i32::max_value()].iter().sum::<i32>();
});
assert!(r.is_err());
}"#);

assert_that(p.cargo_process("build").arg("--release"),
execs().with_status(0));
assert_that(&p.release_bin("foo"), existing_file());

assert_that(process(&p.release_bin("foo")),
execs().with_status(0).with_stdout(""));
}

#[test]
fn cargo_test_verbose() {
let p = project("foo")
Expand Down