From de563082c8aeed2931f063a87ebcdcb2836817d1 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 12 Dec 2017 08:39:20 -0500 Subject: [PATCH] use `Option` instead of `bool` Also allows us to replace `or_false` with `?`. No functional change --- .../nice_region_error/different_lifetimes.rs | 18 ++++++++++-------- .../error_reporting/nice_region_error/mod.rs | 16 ++++++++-------- .../nice_region_error/named_anon_conflict.rs | 13 +++++++------ .../error_reporting/nice_region_error/util.rs | 12 ------------ 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs b/src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs index 5061a2ba2774..d4ea899dc747 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs @@ -10,8 +10,10 @@ //! Error Reporting for Anonymous Region Lifetime Errors //! where both the regions are anonymous. + use infer::error_reporting::nice_region_error::NiceRegionError; use infer::error_reporting::nice_region_error::util::AnonymousArgInfo; +use util::common::ErrorReported; impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { /// Print the error message for lifetime errors when both the concerned regions are anonymous. @@ -50,21 +52,21 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { /// ```` /// /// It will later be extended to trait objects. - pub(super) fn try_report_anon_anon_conflict(&self) -> bool { + pub(super) fn try_report_anon_anon_conflict(&self) -> Option { let NiceRegionError { span, sub, sup, .. } = *self; // Determine whether the sub and sup consist of both anonymous (elided) regions. - let anon_reg_sup = or_false!(self.is_suitable_region(sup)); + let anon_reg_sup = self.is_suitable_region(sup)?; - let anon_reg_sub = or_false!(self.is_suitable_region(sub)); + let anon_reg_sub = self.is_suitable_region(sub)?; let scope_def_id_sup = anon_reg_sup.def_id; let bregion_sup = anon_reg_sup.boundregion; let scope_def_id_sub = anon_reg_sub.def_id; let bregion_sub = anon_reg_sub.boundregion; - let ty_sup = or_false!(self.find_anon_type(sup, &bregion_sup)); + let ty_sup = self.find_anon_type(sup, &bregion_sup)?; - let ty_sub = or_false!(self.find_anon_type(sub, &bregion_sub)); + let ty_sub = self.find_anon_type(sub, &bregion_sub)?; debug!( "try_report_anon_anon_conflict: found_arg1={:?} sup={:?} br1={:?}", @@ -84,10 +86,10 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { let AnonymousArgInfo { arg: anon_arg_sup, .. - } = or_false!(self.find_arg_with_region(sup, sup)); + } = self.find_arg_with_region(sup, sup)?; let AnonymousArgInfo { arg: anon_arg_sub, .. - } = or_false!(self.find_arg_with_region(sub, sub)); + } = self.find_arg_with_region(sub, sub)?; let sup_is_ret_type = self.is_return_type_anon(scope_def_id_sup, bregion_sup, ty_fndecl_sup); @@ -157,6 +159,6 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { .span_label(span_2, format!("")) .span_label(span, span_label) .emit(); - return true; + return Some(ErrorReported); } } diff --git a/src/librustc/infer/error_reporting/nice_region_error/mod.rs b/src/librustc/infer/error_reporting/nice_region_error/mod.rs index 46283041fac4..71e6090ab9e3 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/mod.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/mod.rs @@ -13,13 +13,12 @@ use infer::lexical_region_resolve::RegionResolutionError; use infer::lexical_region_resolve::RegionResolutionError::*; use syntax::codemap::Span; use ty::{self, TyCtxt}; +use util::common::ErrorReported; -#[macro_use] -mod util; - -mod find_anon_type; mod different_lifetimes; +mod find_anon_type; mod named_anon_conflict; +mod util; impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> { pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool { @@ -31,9 +30,9 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> { if let Some(tables) = self.in_progress_tables { let tables = tables.borrow(); - NiceRegionError::new(self.tcx, span, sub, sup, Some(&tables)).try_report() + NiceRegionError::new(self.tcx, span, sub, sup, Some(&tables)).try_report().is_some() } else { - NiceRegionError::new(self.tcx, span, sub, sup, None).try_report() + NiceRegionError::new(self.tcx, span, sub, sup, None).try_report().is_some() } } } @@ -57,7 +56,8 @@ impl<'cx, 'gcx, 'tcx> NiceRegionError<'cx, 'gcx, 'tcx> { Self { tcx, span, sub, sup, tables } } - pub fn try_report(&self) -> bool { - self.try_report_anon_anon_conflict() || self.try_report_named_anon_conflict() + pub fn try_report(&self) -> Option { + self.try_report_anon_anon_conflict() + .or_else(|| self.try_report_named_anon_conflict()) } } diff --git a/src/librustc/infer/error_reporting/nice_region_error/named_anon_conflict.rs b/src/librustc/infer/error_reporting/nice_region_error/named_anon_conflict.rs index 87c3da2ee16a..9d0ddfd4be04 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/named_anon_conflict.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/named_anon_conflict.rs @@ -12,11 +12,12 @@ //! where one region is named and the other is anonymous. use infer::error_reporting::nice_region_error::NiceRegionError; use ty; +use util::common::ErrorReported; impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { /// When given a `ConcreteFailure` for a function with arguments containing a named region and /// an anonymous region, emit an descriptive diagnostic error. - pub(super) fn try_report_named_anon_conflict(&self) -> bool { + pub(super) fn try_report_named_anon_conflict(&self) -> Option { let NiceRegionError { span, sub, sup, .. } = *self; debug!( @@ -51,7 +52,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { self.is_suitable_region(sub).unwrap(), ) } else { - return false; // inapplicable + return None; // inapplicable }; debug!("try_report_named_anon_conflict: named = {:?}", named); @@ -77,20 +78,20 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { _ => { /* not an anonymous region */ debug!("try_report_named_anon_conflict: not an anonymous region"); - return false; + return None; } } if is_impl_item { debug!("try_report_named_anon_conflict: impl item, bail out"); - return false; + return None; } if let Some((_, fndecl)) = self.find_anon_type(anon, &br) { if self.is_return_type_anon(scope_def_id, br, fndecl).is_some() || self.is_self_anon(is_first, scope_def_id) { - return false; + return None; } } @@ -115,6 +116,6 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { ) .span_label(span, format!("lifetime `{}` required", named)) .emit(); - return true; + return Some(ErrorReported); } } diff --git a/src/librustc/infer/error_reporting/nice_region_error/util.rs b/src/librustc/infer/error_reporting/nice_region_error/util.rs index 9a9b8fbbb949..f8b6f7d0afa9 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/util.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/util.rs @@ -17,18 +17,6 @@ use hir::def_id::DefId; use hir::map as hir_map; use syntax_pos::Span; -macro_rules! or_false { - ($v:expr) => { - match $v { - Some(v) => v, - None => { - debug!("or_false failed: {}", stringify!($v)); - return false; - } - } - } -} - // The struct contains the information about the anonymous region // we are searching for. #[derive(Debug)]