Skip to content

Commit 1160e60

Browse files
committed
Move ConstraintCategory to rustc::mir
Allows us to use the category of outlive requirements inside a closure when reporting free region errors caused by its closure bounds.
1 parent ac841e7 commit 1160e60

File tree

12 files changed

+86
-62
lines changed

12 files changed

+86
-62
lines changed

src/librustc/ich/impls_mir.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,23 @@ impl_stable_hash_for!(struct mir::ClosureRegionRequirements<'tcx> {
550550
impl_stable_hash_for!(struct mir::ClosureOutlivesRequirement<'tcx> {
551551
subject,
552552
outlived_free_region,
553-
blame_span
553+
blame_span,
554+
category
555+
});
556+
557+
impl_stable_hash_for!(enum mir::ConstraintCategory {
558+
Return,
559+
TypeAnnotation,
560+
Cast,
561+
ClosureBounds,
562+
CallArgument,
563+
CopyBound,
564+
SizedBound,
565+
Assignment,
566+
OpaqueType,
567+
Boring,
568+
BoringNoLocation,
569+
Internal,
554570
});
555571

556572
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for mir::ClosureOutlivesSubject<'gcx> {

src/librustc/mir/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,6 +2646,43 @@ pub struct ClosureOutlivesRequirement<'tcx> {
26462646
pub blame_span: Span,
26472647
}
26482648

2649+
/// Outlives constraints can be categorized to determine whether and why they
2650+
/// are interesting (for error reporting). Order of variants indicates sort
2651+
/// order of the category, thereby influencing diagnostic output.
2652+
///
2653+
/// See also [rustc_mir::borrow_check::nll::constraints]
2654+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
2655+
pub enum ConstraintCategory {
2656+
Return,
2657+
TypeAnnotation,
2658+
Cast,
2659+
2660+
/// A constraint that came from checking the body of a closure.
2661+
///
2662+
/// We try to get the category that the closure used when reporting this.
2663+
ClosureBounds,
2664+
CallArgument,
2665+
CopyBound,
2666+
SizedBound,
2667+
Assignment,
2668+
OpaqueType,
2669+
2670+
/// A "boring" constraint (caused by the given location) is one that
2671+
/// the user probably doesn't want to see described in diagnostics,
2672+
/// because it is kind of an artifact of the type system setup.
2673+
/// Example: `x = Foo { field: y }` technically creates
2674+
/// intermediate regions representing the "type of `Foo { field: y
2675+
/// }`", and data flows from `y` into those variables, but they
2676+
/// are not very interesting. The assignment into `x` on the other
2677+
/// hand might be.
2678+
Boring,
2679+
// Boring and applicable everywhere.
2680+
BoringNoLocation,
2681+
2682+
/// A constraint that doesn't correspond to anything the user sees.
2683+
Internal,
2684+
}
2685+
26492686
/// The subject of a ClosureOutlivesRequirement -- that is, the thing
26502687
/// that must outlive some region.
26512688
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]

src/librustc_mir/borrow_check/nll/constraints/graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
// except according to those terms.
1010

1111
use borrow_check::nll::type_check::Locations;
12-
use borrow_check::nll::constraints::{ConstraintCategory, ConstraintIndex};
12+
use borrow_check::nll::constraints::ConstraintIndex;
1313
use borrow_check::nll::constraints::{ConstraintSet, OutlivesConstraint};
14+
use rustc::mir::ConstraintCategory;
1415
use rustc::ty::RegionVid;
1516
use rustc_data_structures::graph;
1617
use rustc_data_structures::indexed_vec::IndexVec;

src/librustc_mir/borrow_check/nll/constraints/mod.rs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use rustc::mir::ConstraintCategory;
1112
use rustc::ty::RegionVid;
1213
use rustc_data_structures::graph::scc::Sccs;
1314
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
@@ -23,42 +24,6 @@ crate struct ConstraintSet {
2324
constraints: IndexVec<ConstraintIndex, OutlivesConstraint>,
2425
}
2526

26-
/// Constraints can be categorized to determine whether and why they are
27-
/// interesting. Order of variants indicates sort order of the category,
28-
/// thereby influencing diagnostic output.
29-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
30-
pub enum ConstraintCategory {
31-
Return,
32-
TypeAnnotation,
33-
Cast,
34-
CallArgument,
35-
36-
/// A constraint that came from checking the body of a closure.
37-
///
38-
/// Ideally we would give an explanation that points to the relevant part
39-
/// of the closure's body.
40-
ClosureBounds,
41-
CopyBound,
42-
SizedBound,
43-
Assignment,
44-
OpaqueType,
45-
46-
/// A "boring" constraint (caused by the given location) is one that
47-
/// the user probably doesn't want to see described in diagnostics,
48-
/// because it is kind of an artifact of the type system setup.
49-
/// Example: `x = Foo { field: y }` technically creates
50-
/// intermediate regions representing the "type of `Foo { field: y
51-
/// }`", and data flows from `y` into those variables, but they
52-
/// are not very interesting. The assignment into `x` on the other
53-
/// hand might be.
54-
Boring,
55-
// Boring and applicable everywhere.
56-
BoringNoLocation,
57-
58-
/// A constraint that doesn't correspond to anything the user sees.
59-
Internal,
60-
}
61-
6227
impl ConstraintSet {
6328
crate fn push(&mut self, constraint: OutlivesConstraint) {
6429
debug!(

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use borrow_check::nll::constraints::{OutlivesConstraint, ConstraintCategory};
11+
use borrow_check::nll::constraints::{OutlivesConstraint};
1212
use borrow_check::nll::region_infer::RegionInferenceContext;
1313
use rustc::hir::def_id::DefId;
1414
use rustc::infer::error_reporting::nice_region_error::NiceRegionError;
1515
use rustc::infer::InferCtxt;
16-
use rustc::mir::{Location, Mir};
16+
use rustc::mir::{ConstraintCategory, Location, Mir};
1717
use rustc::ty::{self, RegionVid};
1818
use rustc_data_structures::indexed_vec::IndexVec;
1919
use rustc_errors::{Diagnostic, DiagnosticBuilder};
@@ -28,22 +28,26 @@ mod var_name;
2828

2929
use self::region_name::RegionName;
3030

31-
impl fmt::Display for ConstraintCategory {
32-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31+
trait ConstraintDescription {
32+
fn description(&self) -> &'static str;
33+
}
34+
35+
impl ConstraintDescription for ConstraintCategory {
36+
fn description(&self) -> &'static str {
3337
// Must end with a space. Allows for empty names to be provided.
3438
match self {
35-
ConstraintCategory::Assignment => write!(f, "assignment "),
36-
ConstraintCategory::Return => write!(f, "returning this value "),
37-
ConstraintCategory::Cast => write!(f, "cast "),
38-
ConstraintCategory::CallArgument => write!(f, "argument "),
39-
ConstraintCategory::TypeAnnotation => write!(f, "type annotation "),
40-
ConstraintCategory::ClosureBounds => write!(f, "closure body "),
41-
ConstraintCategory::SizedBound => write!(f, "proving this value is `Sized` "),
42-
ConstraintCategory::CopyBound => write!(f, "copying this value "),
43-
ConstraintCategory::OpaqueType => write!(f, "opaque type "),
39+
ConstraintCategory::Assignment => "assignment ",
40+
ConstraintCategory::Return => "returning this value ",
41+
ConstraintCategory::Cast => "cast ",
42+
ConstraintCategory::CallArgument => "argument ",
43+
ConstraintCategory::TypeAnnotation => "type annotation ",
44+
ConstraintCategory::ClosureBounds => "closure body ",
45+
ConstraintCategory::SizedBound => "proving this value is `Sized` ",
46+
ConstraintCategory::CopyBound => "copying this value ",
47+
ConstraintCategory::OpaqueType => "opaque type ",
4448
ConstraintCategory::Boring
4549
| ConstraintCategory::BoringNoLocation
46-
| ConstraintCategory::Internal => write!(f, ""),
50+
| ConstraintCategory::Internal => "",
4751
}
4852
}
4953
}
@@ -358,7 +362,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
358362
_ => {
359363
diag.span_label(span, format!(
360364
"{}requires that `{}` must outlive `{}`",
361-
category, fr_name, outlived_fr_name,
365+
category.description(), fr_name, outlived_fr_name,
362366
));
363367
},
364368
}

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc::infer::canonical::QueryRegionConstraint;
1919
use rustc::infer::region_constraints::{GenericKind, VarInfos, VerifyBound};
2020
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin, RegionVariableOrigin};
2121
use rustc::mir::{
22-
ClosureOutlivesRequirement, ClosureOutlivesSubject, ClosureRegionRequirements, Local, Location,
23-
Mir,
22+
ClosureOutlivesRequirement, ClosureOutlivesSubject, ClosureRegionRequirements,
23+
ConstraintCategory, Local, Location, Mir,
2424
};
2525
use rustc::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable};
2626
use rustc::util::common;

src/librustc_mir/borrow_check/nll/type_check/constraint_conversion.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use borrow_check::nll::constraints::{ConstraintCategory, ConstraintSet, OutlivesConstraint};
11+
use borrow_check::nll::constraints::{ConstraintSet, OutlivesConstraint};
1212
use borrow_check::nll::region_infer::TypeTest;
1313
use borrow_check::nll::type_check::Locations;
1414
use borrow_check::nll::universal_regions::UniversalRegions;
@@ -17,6 +17,7 @@ use rustc::infer::outlives::env::RegionBoundPairs;
1717
use rustc::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
1818
use rustc::infer::region_constraints::{GenericKind, VerifyBound};
1919
use rustc::infer::{self, SubregionOrigin};
20+
use rustc::mir::ConstraintCategory;
2021
use rustc::ty::subst::UnpackedKind;
2122
use rustc::ty::{self, TyCtxt};
2223
use syntax_pos::DUMMY_SP;

src/librustc_mir/borrow_check/nll/type_check/free_region_relations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use borrow_check::nll::type_check::constraint_conversion;
1212
use borrow_check::nll::type_check::{Locations, MirTypeckRegionConstraints};
1313
use borrow_check::nll::universal_regions::UniversalRegions;
1414
use borrow_check::nll::ToRegionVid;
15-
use borrow_check::nll::constraints::ConstraintCategory;
1615
use rustc::infer::canonical::QueryRegionConstraint;
1716
use rustc::infer::outlives::free_region_map::FreeRegionRelations;
1817
use rustc::infer::region_constraints::GenericKind;
1918
use rustc::infer::InferCtxt;
19+
use rustc::mir::ConstraintCategory;
2020
use rustc::traits::query::outlives_bounds::{self, OutlivesBound};
2121
use rustc::traits::query::type_op::{self, TypeOp};
2222
use rustc::ty::{self, RegionVid, Ty};

src/librustc_mir/borrow_check/nll/type_check/input_output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc::ty::Ty;
2424
use rustc_data_structures::indexed_vec::Idx;
2525
use syntax_pos::Span;
2626

27-
use super::{ConstraintCategory, Locations, TypeChecker};
27+
use super::{Locations, TypeChecker};
2828

2929
impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
3030
pub(super) fn equate_inputs_and_outputs(

src/librustc_mir/borrow_check/nll/type_check/liveness/trace.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use borrow_check::location::LocationTable;
12-
use borrow_check::nll::constraints::ConstraintCategory;
1312
use borrow_check::nll::region_infer::values::{self, PointIndex, RegionValueElements};
1413
use borrow_check::nll::type_check::liveness::liveness_map::{LiveVar, NllLivenessMap};
1514
use borrow_check::nll::type_check::liveness::local_use_map::LocalUseMap;
@@ -19,7 +18,7 @@ use dataflow::move_paths::indexes::MovePathIndex;
1918
use dataflow::move_paths::MoveData;
2019
use dataflow::{FlowAtLocation, FlowsAtLocation, MaybeInitializedPlaces};
2120
use rustc::infer::canonical::QueryRegionConstraint;
22-
use rustc::mir::{BasicBlock, Local, Location, Mir};
21+
use rustc::mir::{BasicBlock, ConstraintCategory, Local, Location, Mir};
2322
use rustc::traits::query::dropck_outlives::DropckOutlivesResult;
2423
use rustc::traits::query::type_op::outlives::DropckOutlives;
2524
use rustc::traits::query::type_op::TypeOp;

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use borrow_check::borrow_set::BorrowSet;
1515
use borrow_check::location::LocationTable;
16-
use borrow_check::nll::constraints::{ConstraintCategory, ConstraintSet, OutlivesConstraint};
16+
use borrow_check::nll::constraints::{ConstraintSet, OutlivesConstraint};
1717
use borrow_check::nll::facts::AllFacts;
1818
use borrow_check::nll::region_infer::values::LivenessValues;
1919
use borrow_check::nll::region_infer::values::PlaceholderIndices;

src/librustc_mir/borrow_check/nll/type_check/relate_tys.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use borrow_check::nll::constraints::{ConstraintCategory, OutlivesConstraint};
11+
use borrow_check::nll::constraints::OutlivesConstraint;
1212
use borrow_check::nll::type_check::{BorrowCheckContext, Locations};
1313
use rustc::infer::canonical::{Canonical, CanonicalVarInfos};
1414
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
15+
use rustc::mir::ConstraintCategory;
1516
use rustc::traits::query::Fallible;
1617
use rustc::ty::fold::{TypeFoldable, TypeVisitor};
1718
use rustc::ty::relate::{self, Relate, RelateResult, TypeRelation};

0 commit comments

Comments
 (0)