Skip to content

Replace try! with ?. #36041

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 13 commits into from
Sep 14, 2016
Merged
2 changes: 1 addition & 1 deletion src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use util::{exe, mtime, libdir, add_lib_path};
/// * The error itself
///
/// This is currently used judiciously throughout the build system rather than
/// using a `Result` with `try!`, but this may change on day...
/// using a `Result` with `try!`, but this may change one day...
macro_rules! t {
($e:expr) => (match $e {
Ok(e) => e,
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/num/bignum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,9 @@ macro_rules! define_bignum {
let sz = if self.size < 1 {1} else {self.size};
let digitlen = mem::size_of::<$ty>() * 2;

try!(write!(f, "{:#x}", self.base[sz-1]));
write!(f, "{:#x}", self.base[sz-1])?;
for &v in self.base[..sz-1].iter().rev() {
try!(write!(f, "_{:01$x}", v, digitlen));
write!(f, "_{:01$x}", v, digitlen)?;
}
::result::Result::Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1756,9 +1756,9 @@ impl<'a> State<'a> {
self.commasep(Inconsistent, &elts[ddpos..], |s, p| s.print_pat(&p))?;
}
} else {
try!(self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p)));
self.commasep(Inconsistent, &elts[..], |s, p| s.print_pat(&p))?;
}
try!(self.pclose());
self.pclose()?;
}
PatKind::Path(None, ref path) => {
self.print_path(path, true, 0)?;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/higher_ranked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
debug!("higher_ranked_match: skol_map={:?}", skol_map);

// Equate types now that bound regions have been replaced.
try!(self.equate(a_is_expected).relate(&a_match, &b_match));
self.equate(a_is_expected).relate(&a_match, &b_match)?;

// Map each skolemized region to a vector of other regions that it
// must be equated with. (Note that this vector may include other
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<Li
let p = p.as_ref();
let q = q.as_ref();
if q.exists() {
try!(fs::remove_file(&q));
fs::remove_file(&q)?;
}

match fs::hard_link(p, q) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_back/target/aarch64_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use target::{Target, TargetOptions, TargetResult};
use super::apple_ios_base::{opts, Arch};

pub fn target() -> TargetResult {
let base = try!(opts(Arch::Arm64));
let base = opts(Arch::Arm64)?;
Ok(Target {
llvm_target: "arm64-apple-ios".to_string(),
target_endian: "little".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_back/target/apple_ios_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn build_pre_link_args(arch: Arch) -> Result<Vec<String>, String> {

let arch_name = arch.to_string();

let sdk_root = try!(get_sdk_root(sdk_name));
let sdk_root = get_sdk_root(sdk_name)?;

Ok(vec!["-arch".to_string(), arch_name.to_string(),
"-Wl,-syslibroot".to_string(), sdk_root])
Expand All @@ -85,7 +85,7 @@ fn target_cpu(arch: Arch) -> String {
}

pub fn opts(arch: Arch) -> Result<TargetOptions, String> {
let pre_link_args = try!(build_pre_link_args(arch));
let pre_link_args = build_pre_link_args(arch)?;
Ok(TargetOptions {
cpu: target_cpu(arch),
dynamic_linking: false,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_back/target/armv7_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use target::{Target, TargetOptions, TargetResult};
use super::apple_ios_base::{opts, Arch};

pub fn target() -> TargetResult {
let base = try!(opts(Arch::Armv7));
let base = opts(Arch::Armv7)?;
Ok(Target {
llvm_target: "armv7-apple-ios".to_string(),
target_endian: "little".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_back/target/armv7s_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use target::{Target, TargetOptions, TargetResult};
use super::apple_ios_base::{opts, Arch};

pub fn target() -> TargetResult {
let base = try!(opts(Arch::Armv7s));
let base = opts(Arch::Armv7s)?;
Ok(Target {
llvm_target: "armv7s-apple-ios".to_string(),
target_endian: "little".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_back/target/i386_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use target::{Target, TargetOptions, TargetResult};
use super::apple_ios_base::{opts, Arch};

pub fn target() -> TargetResult {
let base = try!(opts(Arch::I386));
let base = opts(Arch::I386)?;
Ok(Target {
llvm_target: "i386-apple-ios".to_string(),
target_endian: "little".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_back/target/i586_pc_windows_msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use target::TargetResult;

pub fn target() -> TargetResult {
let mut base = try!(super::i686_pc_windows_msvc::target());
let mut base = super::i686_pc_windows_msvc::target()?;
base.options.cpu = "pentium".to_string();
base.llvm_target = "i586-pc-windows-msvc".to_string();
Ok(base)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_back/target/i586_unknown_linux_gnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use target::TargetResult;

pub fn target() -> TargetResult {
let mut base = try!(super::i686_unknown_linux_gnu::target());
let mut base = super::i686_unknown_linux_gnu::target()?;
base.options.cpu = "pentium".to_string();
base.llvm_target = "i586-unknown-linux-gnu".to_string();
Ok(base)
Expand Down
16 changes: 8 additions & 8 deletions src/librustc_back/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ macro_rules! supported_targets {
match target {
$(
$triple => {
let mut t = try!($module::target());
let mut t = $module::target()?;
t.options.is_builtin = true;

// round-trip through the JSON parser to ensure at
// run-time that the parser works correctly
t = try!(Target::from_json(t.to_json()));
t = Target::from_json(t.to_json())?;
debug!("Got builtin target: {:?}", t);
Ok(t)
},
Expand Down Expand Up @@ -442,12 +442,12 @@ impl Target {
};

let mut base = Target {
llvm_target: try!(get_req_field("llvm-target")),
target_endian: try!(get_req_field("target-endian")),
target_pointer_width: try!(get_req_field("target-pointer-width")),
data_layout: try!(get_req_field("data-layout")),
arch: try!(get_req_field("arch")),
target_os: try!(get_req_field("os")),
llvm_target: get_req_field("llvm-target")?,
target_endian: get_req_field("target-endian")?,
target_pointer_width: get_req_field("target-pointer-width")?,
data_layout: get_req_field("data-layout")?,
arch: get_req_field("arch")?,
target_os: get_req_field("os")?,
target_env: get_opt_field("env", ""),
target_vendor: get_opt_field("vendor", "unknown"),
options: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_back/target/x86_64_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use target::{Target, TargetOptions, TargetResult};
use super::apple_ios_base::{opts, Arch};

pub fn target() -> TargetResult {
let base = try!(opts(Arch::X86_64));
let base = opts(Arch::X86_64)?;
Ok(Target {
llvm_target: "x86_64-apple-ios".to_string(),
target_endian: "little".to_string(),
Expand Down
41 changes: 19 additions & 22 deletions src/librustc_const_eval/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}
let pat = match expr.node {
hir::ExprTup(ref exprs) =>
PatKind::Tuple(try!(exprs.iter()
.map(|expr| const_expr_to_pat(tcx, &expr, pat_id, span))
.collect()), None),
PatKind::Tuple(exprs.iter()
.map(|expr| const_expr_to_pat(tcx, &expr, pat_id, span))
.collect::<Result<_, _>>()?, None),

hir::ExprCall(ref callee, ref args) => {
let def = tcx.expect_def(callee.id);
Expand All @@ -297,34 +297,31 @@ pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
})),
_ => bug!()
};
let pats = try!(args.iter()
.map(|expr| const_expr_to_pat(tcx, &**expr,
pat_id, span))
.collect());
let pats = args.iter()
.map(|expr| const_expr_to_pat(tcx, &**expr, pat_id, span))
.collect::<Result<_, _>>()?;
PatKind::TupleStruct(path, pats, None)
}

hir::ExprStruct(ref path, ref fields, None) => {
let field_pats =
try!(fields.iter()
.map(|field| Ok(codemap::Spanned {
span: syntax_pos::DUMMY_SP,
node: hir::FieldPat {
name: field.name.node,
pat: try!(const_expr_to_pat(tcx, &field.expr,
pat_id, span)),
is_shorthand: false,
},
}))
.collect());
fields.iter()
.map(|field| Ok(codemap::Spanned {
span: syntax_pos::DUMMY_SP,
node: hir::FieldPat {
name: field.name.node,
pat: const_expr_to_pat(tcx, &field.expr, pat_id, span)?,
is_shorthand: false,
},
}))
.collect::<Result<_, _>>()?;
PatKind::Struct(path.clone(), field_pats, false)
}

hir::ExprVec(ref exprs) => {
let pats = try!(exprs.iter()
.map(|expr| const_expr_to_pat(tcx, &expr,
pat_id, span))
.collect());
let pats = exprs.iter()
.map(|expr| const_expr_to_pat(tcx, &expr, pat_id, span))
.collect::<Result<_, _>>()?;
PatKind::Vec(pats, None, hir::HirVec::new())
}

Expand Down
34 changes: 17 additions & 17 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,45 +882,45 @@ impl Destination {
match style {
Style::FileNameStyle | Style::LineAndColumn => {}
Style::LineNumber => {
try!(self.start_attr(term::Attr::Bold));
self.start_attr(term::Attr::Bold)?;
if cfg!(windows) {
try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_CYAN)));
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_CYAN))?;
} else {
try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_BLUE)));
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_BLUE))?;
}
}
Style::ErrorCode => {
try!(self.start_attr(term::Attr::Bold));
try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_MAGENTA)));
self.start_attr(term::Attr::Bold)?;
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_MAGENTA))?;
}
Style::Quotation => {}
Style::OldSchoolNote => {
try!(self.start_attr(term::Attr::Bold));
try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_GREEN)));
self.start_attr(term::Attr::Bold)?;
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_GREEN))?;
}
Style::OldSchoolNoteText | Style::HeaderMsg => {
try!(self.start_attr(term::Attr::Bold));
self.start_attr(term::Attr::Bold)?;
if cfg!(windows) {
try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_WHITE)));
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_WHITE))?;
}
}
Style::UnderlinePrimary | Style::LabelPrimary => {
try!(self.start_attr(term::Attr::Bold));
try!(self.start_attr(term::Attr::ForegroundColor(lvl.color())));
self.start_attr(term::Attr::Bold)?;
self.start_attr(term::Attr::ForegroundColor(lvl.color()))?;
}
Style::UnderlineSecondary |
Style::LabelSecondary => {
try!(self.start_attr(term::Attr::Bold));
self.start_attr(term::Attr::Bold)?;
if cfg!(windows) {
try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_CYAN)));
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_CYAN))?;
} else {
try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_BLUE)));
self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_BLUE))?;
}
}
Style::NoStyle => {}
Style::Level(l) => {
try!(self.start_attr(term::Attr::Bold));
try!(self.start_attr(term::Attr::ForegroundColor(l.color())));
self.start_attr(term::Attr::Bold)?;
self.start_attr(term::Attr::ForegroundColor(l.color()))?;
}
}
Ok(())
Expand Down Expand Up @@ -960,4 +960,4 @@ impl Write for Destination {
Raw(ref mut w) => w.flush(),
}
}
}
}
4 changes: 2 additions & 2 deletions src/librustc_incremental/persist/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ impl<'a, 'tcx> HashContext<'a, 'tcx> {

// Load up the hashes for the def-ids from this crate.
let mut decoder = Decoder::new(data, 0);
let svh_in_hashes_file = try!(Svh::decode(&mut decoder));
let svh_in_hashes_file = Svh::decode(&mut decoder)?;

if svh_in_hashes_file != expected_svh {
// We should not be able to get here. If we do, then
// `fs::find_metadata_hashes_for()` has messed up.
bug!("mismatch between SVH in crate and SVH in incr. comp. hashes")
}

let serialized_hashes = try!(SerializedMetadataHashes::decode(&mut decoder));
let serialized_hashes = SerializedMetadataHashes::decode(&mut decoder)?;
for serialized_hash in serialized_hashes.hashes {
// the hashes are stored with just a def-index, which is
// always relative to the old crate; convert that to use
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_incremental/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ pub fn decode_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
{
// Decode the list of work_products
let mut work_product_decoder = Decoder::new(work_products_data, 0);
let work_products = try!(<Vec<SerializedWorkProduct>>::decode(&mut work_product_decoder));
let work_products = <Vec<SerializedWorkProduct>>::decode(&mut work_product_decoder)?;

// Deserialize the directory and dep-graph.
let mut dep_graph_decoder = Decoder::new(dep_graph_data, 0);
let prev_commandline_args_hash = try!(u64::decode(&mut dep_graph_decoder));
let prev_commandline_args_hash = u64::decode(&mut dep_graph_decoder)?;

if prev_commandline_args_hash != tcx.sess.opts.dep_tracking_hash() {
// We can't reuse the cache, purge it.
Expand All @@ -142,8 +142,8 @@ pub fn decode_dep_graph<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
return Ok(());
}

let directory = try!(DefIdDirectory::decode(&mut dep_graph_decoder));
let serialized_dep_graph = try!(SerializedDepGraph::decode(&mut dep_graph_decoder));
let directory = DefIdDirectory::decode(&mut dep_graph_decoder)?;
let serialized_dep_graph = SerializedDepGraph::decode(&mut dep_graph_decoder)?;

// Retrace the paths in the directory to find their current location (if any).
let retraced = directory.retrace(tcx);
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_incremental/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn encode_dep_graph(preds: &Predecessors,
-> io::Result<()> {
// First encode the commandline arguments hash
let tcx = builder.tcx();
try!(tcx.sess.opts.dep_tracking_hash().encode(encoder));
tcx.sess.opts.dep_tracking_hash().encode(encoder)?;

// Create a flat list of (Input, WorkProduct) edges for
// serialization.
Expand Down Expand Up @@ -149,8 +149,8 @@ pub fn encode_dep_graph(preds: &Predecessors,
debug!("graph = {:#?}", graph);

// Encode the directory and then the graph data.
try!(builder.directory().encode(encoder));
try!(graph.encode(encoder));
builder.directory().encode(encoder)?;
graph.encode(encoder)?;

Ok(())
}
Expand Down Expand Up @@ -222,8 +222,8 @@ pub fn encode_metadata_hashes(tcx: TyCtxt,
}

// Encode everything.
try!(svh.encode(encoder));
try!(serialized_hashes.encode(encoder));
svh.encode(encoder)?;
serialized_hashes.encode(encoder)?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ fn get_metadata_section_imp(target: &Target, flavor: CrateFlavor, filename: &Pat
None => Err(format!("failed to read rlib metadata: '{}'",
filename.display())),
Some(blob) => {
try!(verify_decompressed_encoding_version(&blob, filename));
verify_decompressed_encoding_version(&blob, filename)?;
Ok(blob)
}
};
Expand Down Expand Up @@ -858,7 +858,7 @@ fn get_metadata_section_imp(target: &Target, flavor: CrateFlavor, filename: &Pat
match flate::inflate_bytes(bytes) {
Ok(inflated) => {
let blob = MetadataVec(inflated);
try!(verify_decompressed_encoding_version(&blob, filename));
verify_decompressed_encoding_version(&blob, filename)?;
return Ok(blob);
}
Err(_) => {}
Expand Down
Loading