Skip to content

Commit a13c9f6

Browse files
committed
convert from an UnlessNll flag to a SuppressRegionErrors flag
Hopefully this will help clarify the behavior in the various borrowck modes
1 parent 0b4791e commit a13c9f6

File tree

6 files changed

+55
-40
lines changed

6 files changed

+55
-40
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
//! ported to this system, and which relies on string concatenation at the
5656
//! time of error detection.
5757
58-
use infer::{self, UnlessNll};
58+
use infer::{self, SuppressRegionErrors};
5959
use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs};
6060
use super::region_constraints::GenericKind;
6161
use super::lexical_region_resolve::RegionResolutionError;
@@ -68,7 +68,6 @@ use middle::region;
6868
use traits::{ObligationCause, ObligationCauseCode};
6969
use ty::{self, subst::Subst, Region, Ty, TyCtxt, TypeFoldable, TyKind};
7070
use ty::error::TypeError;
71-
use session::config::BorrowckMode;
7271
use syntax::ast::DUMMY_NODE_ID;
7372
use syntax_pos::{Pos, Span};
7473
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
@@ -298,20 +297,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
298297
&self,
299298
region_scope_tree: &region::ScopeTree,
300299
errors: &Vec<RegionResolutionError<'tcx>>,
301-
unless_nll: UnlessNll,
300+
suppress: SuppressRegionErrors,
302301
) {
303-
debug!("report_region_errors(): {} errors to start", errors.len());
304-
305-
// If the errors will later be reported by NLL, choose wether to display them or not based
306-
// on the borrowck mode
307-
if unless_nll.0 {
308-
match self.tcx.borrowck_mode() {
309-
// If we're on AST or Migrate mode, report AST region errors
310-
BorrowckMode::Ast | BorrowckMode::Migrate => {},
311-
// If we're on MIR or Compare mode, don't report AST region errors as they should
312-
// be reported by NLL
313-
BorrowckMode::Compare | BorrowckMode::Mir => return,
314-
}
302+
debug!("report_region_errors(): {} errors to start, suppress = {:?}", errors.len(), suppress);
303+
304+
if suppress.suppressed() {
305+
return;
315306
}
316307

317308
// try to pre-process the errors, which will group some of them

src/librustc/infer/mod.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use middle::free_region::RegionRelations;
2424
use middle::lang_items;
2525
use middle::region;
2626
use rustc_data_structures::unify as ut;
27+
use session::config::BorrowckMode;
2728
use std::cell::{Cell, Ref, RefCell, RefMut};
2829
use std::collections::BTreeMap;
2930
use std::fmt;
@@ -80,15 +81,37 @@ pub type Bound<T> = Option<T>;
8081
pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
8182
pub type FixupResult<T> = Result<T, FixupError>; // "fixup result"
8283

83-
/// A flag that is given when running region resolution: if true, it
84-
/// indicates that we should not report the region errors to the user
85-
/// if NLL is enabled, since NLL will also detect them (and do a
86-
/// better job of it).
87-
///
88-
/// Currently, NLL only runs on HIR bodies, so you should use `false`
89-
/// unless you are region-checking a `hir::Body` (basically, a fn or
90-
/// expression).
91-
pub struct UnlessNll(pub bool);
84+
/// A flag that is used to suppress region errors. This is normally
85+
/// false, but sometimes -- when we are doing region checks that the
86+
/// NLL borrow checker will also do -- it might be set to true.
87+
#[derive(Copy, Clone, Default, Debug)]
88+
pub struct SuppressRegionErrors {
89+
suppressed: bool
90+
}
91+
92+
impl SuppressRegionErrors {
93+
pub fn suppressed(self) -> bool {
94+
self.suppressed
95+
}
96+
97+
/// Indicates that the MIR borrowck will repeat these region
98+
/// checks, so we should ignore errors if NLL is (unconditionally)
99+
/// enabled.
100+
pub fn when_nll_is_enabled(tcx: TyCtxt<'_, '_, '_>) -> Self {
101+
match tcx.borrowck_mode() {
102+
// If we're on AST or Migrate mode, report AST region errors
103+
BorrowckMode::Ast | BorrowckMode::Migrate => SuppressRegionErrors {
104+
suppressed: false
105+
},
106+
107+
// If we're on MIR or Compare mode, don't report AST region errors as they should
108+
// be reported by NLL
109+
BorrowckMode::Compare | BorrowckMode::Mir => SuppressRegionErrors {
110+
suppressed: true
111+
},
112+
}
113+
}
114+
}
92115

93116
pub struct InferCtxt<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
94117
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
@@ -1049,7 +1072,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10491072
region_context: DefId,
10501073
region_map: &region::ScopeTree,
10511074
outlives_env: &OutlivesEnvironment<'tcx>,
1052-
unless_nll: UnlessNll,
1075+
suppress: SuppressRegionErrors,
10531076
) {
10541077
assert!(
10551078
self.is_tainted_by_errors() || self.region_obligations.borrow().is_empty(),
@@ -1081,7 +1104,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10811104
// this infcx was in use. This is totally hokey but
10821105
// otherwise we have a hard time separating legit region
10831106
// errors from silly ones.
1084-
self.report_region_errors(region_map, &errors, unless_nll);
1107+
self.report_region_errors(region_map, &errors, suppress);
10851108
}
10861109
}
10871110

src/librustc/traits/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use self::ObligationCauseCode::*;
2020
use chalk_engine;
2121
use hir;
2222
use hir::def_id::DefId;
23-
use infer::UnlessNll;
23+
use infer::SuppressRegionErrors;
2424
use infer::outlives::env::OutlivesEnvironment;
2525
use middle::region;
2626
use mir::interpret::ConstEvalErr;
@@ -720,7 +720,7 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
720720
region_context,
721721
&region_scope_tree,
722722
&outlives_env,
723-
UnlessNll(false),
723+
SuppressRegionErrors::default(),
724724
);
725725

726726
let predicates = match infcx.fully_resolve(&predicates) {

src/librustc_typeck/check/dropck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use check::regionck::RegionCtxt;
1212

1313
use hir::def_id::DefId;
14-
use rustc::infer::{self, InferOk, UnlessNll};
14+
use rustc::infer::{self, InferOk, SuppressRegionErrors};
1515
use rustc::infer::outlives::env::OutlivesEnvironment;
1616
use rustc::middle::region;
1717
use rustc::ty::subst::{Subst, Substs, UnpackedKind};
@@ -128,7 +128,7 @@ fn ensure_drop_params_and_item_params_correspond<'a, 'tcx>(
128128
// conservative. -nmatsakis
129129
let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty());
130130

131-
infcx.resolve_regions_and_report_errors(drop_impl_did, &region_scope_tree, &outlives_env, UnlessNll(false));
131+
infcx.resolve_regions_and_report_errors(drop_impl_did, &region_scope_tree, &outlives_env, SuppressRegionErrors::default());
132132
Ok(())
133133
})
134134
}

src/librustc_typeck/check/regionck.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ use middle::mem_categorization as mc;
8888
use middle::mem_categorization::Categorization;
8989
use middle::region;
9090
use rustc::hir::def_id::DefId;
91-
use rustc::infer::{self, RegionObligation, UnlessNll};
9291
use rustc::infer::outlives::env::OutlivesEnvironment;
92+
use rustc::infer::{self, RegionObligation, SuppressRegionErrors};
9393
use rustc::ty::adjustment;
9494
use rustc::ty::subst::Substs;
9595
use rustc::ty::{self, Ty};
@@ -140,7 +140,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
140140
rcx.visit_body(body);
141141
rcx.visit_region_obligations(id);
142142
}
143-
rcx.resolve_regions_and_report_errors(UnlessNll(true));
143+
rcx.resolve_regions_and_report_errors(SuppressRegionErrors::when_nll_is_enabled(self.tcx));
144144

145145
assert!(self.tables.borrow().free_region_map.is_empty());
146146
self.tables.borrow_mut().free_region_map = rcx.outlives_environment.into_free_region_map();
@@ -162,7 +162,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
162162
.add_implied_bounds(self, wf_tys, item_id, span);
163163
rcx.outlives_environment.save_implied_bounds(item_id);
164164
rcx.visit_region_obligations(item_id);
165-
rcx.resolve_regions_and_report_errors(UnlessNll(false));
165+
rcx.resolve_regions_and_report_errors(SuppressRegionErrors::default());
166166
}
167167

168168
/// Region check a function body. Not invoked on closures, but
@@ -190,7 +190,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
190190
rcx.visit_fn_body(fn_id, body, self.tcx.hir.span(fn_id));
191191
}
192192

193-
rcx.resolve_regions_and_report_errors(UnlessNll(true));
193+
rcx.resolve_regions_and_report_errors(SuppressRegionErrors::when_nll_is_enabled(self.tcx));
194194

195195
// In this mode, we also copy the free-region-map into the
196196
// tables of the enclosing fcx. In the other regionck modes
@@ -314,7 +314,7 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
314314
id: ast::NodeId, // the id of the fn itself
315315
body: &'gcx hir::Body,
316316
span: Span,
317-
) {
317+
) {
318318
// When we enter a function, we can derive
319319
debug!("visit_fn_body(id={})", id);
320320

@@ -355,7 +355,8 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
355355
body_id.node_id,
356356
span,
357357
);
358-
self.outlives_environment.save_implied_bounds(body_id.node_id);
358+
self.outlives_environment
359+
.save_implied_bounds(body_id.node_id);
359360
self.link_fn_args(
360361
region::Scope {
361362
id: body.value.hir_id.local_id,
@@ -392,7 +393,7 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
392393
self.select_all_obligations_or_error();
393394
}
394395

395-
fn resolve_regions_and_report_errors(&self, unless_nll: UnlessNll) {
396+
fn resolve_regions_and_report_errors(&self, suppress: SuppressRegionErrors) {
396397
self.infcx.process_registered_region_obligations(
397398
self.outlives_environment.region_bound_pairs_map(),
398399
self.implicit_region_bound,
@@ -403,7 +404,7 @@ impl<'a, 'gcx, 'tcx> RegionCtxt<'a, 'gcx, 'tcx> {
403404
self.subject_def_id,
404405
&self.region_scope_tree,
405406
&self.outlives_environment,
406-
unless_nll,
407+
suppress,
407408
);
408409
}
409410

src/librustc_typeck/coherence/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! Check properties that are required by built-in traits and set
1212
//! up data structures required by type-checking/codegen.
1313
14-
use rustc::infer::UnlessNll;
14+
use rustc::infer::SuppressRegionErrors;
1515
use rustc::infer::outlives::env::OutlivesEnvironment;
1616
use rustc::middle::region;
1717
use rustc::middle::lang_items::UnsizeTraitLangItem;
@@ -397,7 +397,7 @@ pub fn coerce_unsized_info<'a, 'gcx>(gcx: TyCtxt<'a, 'gcx, 'gcx>,
397397
impl_did,
398398
&region_scope_tree,
399399
&outlives_env,
400-
UnlessNll(false),
400+
SuppressRegionErrors::default(),
401401
);
402402

403403
CoerceUnsizedInfo {

0 commit comments

Comments
 (0)