Skip to content

Commit a0312c1

Browse files
committed
Auto merge of #66815 - mark-i-m:simplify-borrow_check-errors, r=Dylan-DPC
Reorganize borrow check diagnostic code Currently borrow checker diagnostics are split across many different modules in different places in the `librustc_mir` crate. This moves them all to a `diagnostics` module. This also reduces the nesting of the modules a bit (sooo much nesting). I am also thinking of moving stuff out of the `nll` module since we only have one borrow checker now (:tada:), and maybe it even makes sense to split out all of this stuff to a `librustc_borrow_check`, but those are for the future. Feel free to ping me here or on zulip and let me know what you think... cc @nikomatsakis @matthewjasper @eddyb
2 parents 1e2a738 + b998e83 commit a0312c1

File tree

13 files changed

+130
-110
lines changed

13 files changed

+130
-110
lines changed

src/librustc_mir/borrow_check/conflict_errors.rs renamed to src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
1414
use syntax_pos::Span;
1515
use syntax::source_map::DesugaringKind;
1616

17-
use super::nll::explain_borrow::BorrowExplanation;
18-
use super::nll::region_infer::{RegionName, RegionNameSource};
19-
use super::prefixes::IsPrefixOf;
20-
use super::WriteKind;
21-
use super::borrow_set::BorrowData;
22-
use super::MirBorrowckCtxt;
23-
use super::{InitializationRequiringAction, PrefixSet};
24-
use super::error_reporting::{IncludingDowncast, UseSpans};
2517
use crate::dataflow::drop_flag_effects;
2618
use crate::dataflow::indexes::{MovePathIndex, MoveOutIndex};
2719
use crate::util::borrowck_errors;
2820

21+
use crate::borrow_check::{
22+
prefixes::IsPrefixOf,
23+
WriteKind,
24+
borrow_set::BorrowData,
25+
MirBorrowckCtxt, InitializationRequiringAction, PrefixSet
26+
};
27+
28+
use super::{
29+
IncludingDowncast, UseSpans, RegionName, RegionNameSource,
30+
explain_borrow::BorrowExplanation,
31+
};
32+
2933
#[derive(Debug)]
3034
struct MoveSite {
3135
/// Index of the "move out" that we found. The `MoveData` can
@@ -46,7 +50,7 @@ enum StorageDeadOrDrop<'tcx> {
4650
}
4751

4852
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
49-
pub(super) fn report_use_of_moved_or_uninitialized(
53+
pub(in crate::borrow_check) fn report_use_of_moved_or_uninitialized(
5054
&mut self,
5155
location: Location,
5256
desired_action: InitializationRequiringAction,
@@ -269,7 +273,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
269273
}
270274
}
271275

272-
pub(super) fn report_move_out_while_borrowed(
276+
pub(in crate::borrow_check) fn report_move_out_while_borrowed(
273277
&mut self,
274278
location: Location,
275279
(place, span): (&Place<'tcx>, Span),
@@ -326,7 +330,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
326330
err.buffer(&mut self.errors_buffer);
327331
}
328332

329-
pub(super) fn report_use_while_mutably_borrowed(
333+
pub(in crate::borrow_check) fn report_use_while_mutably_borrowed(
330334
&mut self,
331335
location: Location,
332336
(place, _span): (&Place<'tcx>, Span),
@@ -368,7 +372,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
368372
err
369373
}
370374

371-
pub(super) fn report_conflicting_borrow(
375+
pub(in crate::borrow_check) fn report_conflicting_borrow(
372376
&mut self,
373377
location: Location,
374378
(place, span): (&Place<'tcx>, Span),
@@ -614,7 +618,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
614618
///
615619
/// > cannot borrow `a.u` (via `a.u.z.c`) as immutable because it is also borrowed as
616620
/// > mutable (via `a.u.s.b`) [E0502]
617-
pub(super) fn describe_place_for_conflicting_borrow(
621+
pub(in crate::borrow_check) fn describe_place_for_conflicting_borrow(
618622
&self,
619623
first_borrowed_place: &Place<'tcx>,
620624
second_borrowed_place: &Place<'tcx>,
@@ -722,7 +726,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
722726
/// short a lifetime. (But sometimes it is more useful to report
723727
/// it as a more direct conflict between the execution of a
724728
/// `Drop::drop` with an aliasing borrow.)
725-
pub(super) fn report_borrowed_value_does_not_live_long_enough(
729+
pub(in crate::borrow_check) fn report_borrowed_value_does_not_live_long_enough(
726730
&mut self,
727731
location: Location,
728732
borrow: &BorrowData<'tcx>,
@@ -1478,7 +1482,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14781482
result
14791483
}
14801484

1481-
pub(super) fn report_illegal_mutation_of_borrowed(
1485+
pub(in crate::borrow_check) fn report_illegal_mutation_of_borrowed(
14821486
&mut self,
14831487
location: Location,
14841488
(place, span): (&Place<'tcx>, Span),
@@ -1537,7 +1541,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15371541
/// assigned; `err_place` is a place providing a reason why
15381542
/// `place` is not mutable (e.g., the non-`mut` local `x` in an
15391543
/// assignment to `x.f`).
1540-
pub(super) fn report_illegal_reassignment(
1544+
pub(in crate::borrow_check) fn report_illegal_reassignment(
15411545
&mut self,
15421546
_location: Location,
15431547
(place, span): (&Place<'tcx>, Span),
@@ -2080,7 +2084,7 @@ enum AnnotatedBorrowFnSignature<'tcx> {
20802084
impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
20812085
/// Annotate the provided diagnostic with information about borrow from the fn signature that
20822086
/// helps explain.
2083-
pub(super) fn emit(
2087+
pub(in crate::borrow_check) fn emit(
20842088
&self,
20852089
cx: &mut MirBorrowckCtxt<'_, 'tcx>,
20862090
diag: &mut DiagnosticBuilder<'_>,

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs renamed to src/librustc_mir/borrow_check/diagnostics/explain_borrow.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::collections::VecDeque;
22

33
use crate::borrow_check::borrow_set::BorrowData;
4-
use crate::borrow_check::error_reporting::UseSpans;
5-
use crate::borrow_check::nll::region_infer::{Cause, RegionName};
4+
use crate::borrow_check::nll::region_infer::Cause;
65
use crate::borrow_check::nll::ConstraintDescription;
76
use crate::borrow_check::{MirBorrowckCtxt, WriteKind};
87
use rustc::mir::{
@@ -17,7 +16,7 @@ use rustc_errors::DiagnosticBuilder;
1716
use syntax_pos::Span;
1817
use syntax_pos::symbol::Symbol;
1918

20-
mod find_use;
19+
use super::{UseSpans, find_use, RegionName};
2120

2221
#[derive(Debug)]
2322
pub(in crate::borrow_check) enum BorrowExplanation {

src/librustc_mir/borrow_check/error_reporting.rs renamed to src/librustc_mir/borrow_check/diagnostics/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Borrow checker diagnostics.
2+
13
use rustc::hir;
24
use rustc::hir::def::Namespace;
35
use rustc::hir::def_id::DefId;
@@ -17,6 +19,22 @@ use super::borrow_set::BorrowData;
1719
use super::MirBorrowckCtxt;
1820
use crate::dataflow::move_paths::{InitLocation, LookupResult};
1921

22+
mod find_use;
23+
mod var_name;
24+
mod region_name;
25+
mod outlives_suggestion;
26+
27+
mod conflict_errors;
28+
mod move_errors;
29+
mod mutability_errors;
30+
mod region_errors;
31+
mod explain_borrow;
32+
33+
crate use mutability_errors::AccessKind;
34+
crate use region_name::{RegionName, RegionNameSource, RegionErrorNamingCtx};
35+
crate use region_errors::{ErrorReportingCtx, ErrorConstraintInfo};
36+
crate use outlives_suggestion::OutlivesSuggestionBuilder;
37+
2038
pub(super) struct IncludingDowncast(pub(super) bool);
2139

2240
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {

src/librustc_mir/borrow_check/move_errors.rs renamed to src/librustc_mir/borrow_check/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use syntax_pos::Span;
55

66
use crate::borrow_check::MirBorrowckCtxt;
77
use crate::borrow_check::prefixes::PrefixSet;
8-
use crate::borrow_check::error_reporting::UseSpans;
8+
use crate::borrow_check::diagnostics::UseSpans;
99
use crate::dataflow::move_paths::{
1010
IllegalMoveOrigin, IllegalMoveOriginKind,
1111
LookupResult, MoveError, MovePathIndex,

src/librustc_mir/borrow_check/mutability_errors.rs renamed to src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ use syntax_pos::Span;
88
use syntax_pos::symbol::kw;
99

1010
use crate::borrow_check::MirBorrowckCtxt;
11-
use crate::borrow_check::error_reporting::BorrowedContentSource;
11+
use crate::borrow_check::diagnostics::BorrowedContentSource;
1212
use crate::util::collect_writes::FindAssignments;
1313
use rustc_errors::Applicability;
1414

1515
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
16-
pub(super) enum AccessKind {
16+
pub(crate) enum AccessKind {
1717
MutableBorrow,
1818
Mutate,
1919
}
2020

2121
impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
22-
pub(super) fn report_mutability_error(
22+
pub(crate) fn report_mutability_error(
2323
&mut self,
2424
access_place: &Place<'tcx>,
2525
span: Span,
+4-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ use syntax_pos::symbol::Symbol;
1313

1414
use smallvec::SmallVec;
1515

16-
use crate::borrow_check::nll::region_infer::{
17-
error_reporting::{
18-
region_name::{RegionName, RegionNameSource},
19-
ErrorConstraintInfo, ErrorReportingCtx, RegionErrorNamingCtx,
20-
},
21-
RegionInferenceContext,
16+
use crate::borrow_check::nll::region_infer::RegionInferenceContext;
17+
18+
use super::{
19+
RegionName, RegionNameSource, ErrorConstraintInfo, ErrorReportingCtx, RegionErrorNamingCtx,
2220
};
2321

2422
/// The different things we could suggest.

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs renamed to src/librustc_mir/borrow_check/diagnostics/region_errors.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ use syntax::symbol::kw;
1919
use syntax_pos::Span;
2020
use syntax_pos::symbol::Symbol;
2121

22-
use self::outlives_suggestion::OutlivesSuggestionBuilder;
23-
24-
pub mod outlives_suggestion;
25-
26-
mod region_name;
27-
mod var_name;
28-
29-
crate use self::region_name::{RegionName, RegionNameSource, RegionErrorNamingCtx};
22+
use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource, RegionErrorNamingCtx};
3023

3124
impl ConstraintDescription for ConstraintCategory {
3225
fn description(&self) -> &'static str {
@@ -61,36 +54,36 @@ enum Trace {
6154
/// Various pieces of state used when reporting borrow checker errors.
6255
pub struct ErrorReportingCtx<'a, 'b, 'tcx> {
6356
/// The region inference context used for borrow chekcing this MIR body.
64-
region_infcx: &'b RegionInferenceContext<'tcx>,
57+
pub(super) region_infcx: &'b RegionInferenceContext<'tcx>,
6558

6659
/// The inference context used for type checking.
67-
infcx: &'b InferCtxt<'a, 'tcx>,
60+
pub(super) infcx: &'b InferCtxt<'a, 'tcx>,
6861

6962
/// The MIR def we are reporting errors on.
70-
mir_def_id: DefId,
63+
pub(super) mir_def_id: DefId,
7164

7265
/// The MIR body we are reporting errors on (for convenience).
73-
body: &'b Body<'tcx>,
66+
pub(super) body: &'b Body<'tcx>,
7467

7568
/// User variable names for MIR locals (where applicable).
76-
local_names: &'b IndexVec<Local, Option<Symbol>>,
69+
pub(super) local_names: &'b IndexVec<Local, Option<Symbol>>,
7770

7871
/// Any upvars for the MIR body we have kept track of during borrow checking.
79-
upvars: &'b [Upvar],
72+
pub(super) upvars: &'b [Upvar],
8073
}
8174

8275
/// Information about the various region constraints involved in a borrow checker error.
8376
#[derive(Clone, Debug)]
8477
pub struct ErrorConstraintInfo {
8578
// fr: outlived_fr
86-
fr: RegionVid,
87-
fr_is_local: bool,
88-
outlived_fr: RegionVid,
89-
outlived_fr_is_local: bool,
79+
pub(super) fr: RegionVid,
80+
pub(super) fr_is_local: bool,
81+
pub(super) outlived_fr: RegionVid,
82+
pub(super) outlived_fr_is_local: bool,
9083

9184
// Category and span for best blame constraint
92-
category: ConstraintCategory,
93-
span: Span,
85+
pub(super) category: ConstraintCategory,
86+
pub(super) span: Span,
9487
}
9588

9689
impl<'tcx> RegionInferenceContext<'tcx> {
@@ -368,7 +361,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
368361
/// ```
369362
///
370363
/// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
371-
pub(super) fn report_error<'a>(
364+
pub(in crate::borrow_check) fn report_error<'a>(
372365
&'a self,
373366
body: &Body<'tcx>,
374367
local_names: &IndexVec<Local, Option<Symbol>>,

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs renamed to src/librustc_mir/borrow_check/diagnostics/region_name.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
use std::fmt::{self, Display};
22

3-
use crate::borrow_check::nll::region_infer::{
4-
RegionInferenceContext,
5-
error_reporting::ErrorReportingCtx,
6-
};
7-
use crate::borrow_check::nll::universal_regions::DefiningTy;
8-
use crate::borrow_check::nll::ToRegionVid;
9-
use crate::borrow_check::Upvar;
103
use rustc::hir;
114
use rustc::hir::def::{Res, DefKind};
125
use rustc::hir::def_id::DefId;
@@ -21,6 +14,15 @@ use syntax::symbol::kw;
2114
use rustc_data_structures::fx::FxHashMap;
2215
use syntax_pos::{Span, symbol::Symbol, DUMMY_SP};
2316

17+
use crate::borrow_check::{
18+
nll::region_infer::RegionInferenceContext,
19+
nll::universal_regions::DefiningTy,
20+
nll::ToRegionVid,
21+
Upvar,
22+
};
23+
24+
use super::region_errors::ErrorReportingCtx;
25+
2426
/// A name for a particular region used in emitting diagnostics. This name could be a generated
2527
/// name like `'1`, a name used by the user like `'a`, or a name like `'static`.
2628
#[derive(Debug, Clone)]

src/librustc_mir/borrow_check/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,14 @@ use self::flows::Flows;
4646
use self::location::LocationTable;
4747
use self::prefixes::PrefixSet;
4848
use self::MutateMode::{JustWrite, WriteAndRead};
49-
use self::mutability_errors::AccessKind;
49+
use self::diagnostics::AccessKind;
5050

5151
use self::path_utils::*;
5252

5353
crate mod borrow_set;
54-
mod error_reporting;
54+
mod diagnostics;
5555
mod flows;
5656
mod location;
57-
mod conflict_errors;
58-
mod move_errors;
59-
mod mutability_errors;
6057
mod path_utils;
6158
crate mod place_ext;
6259
crate mod places_conflict;

src/librustc_mir/borrow_check/nll/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ use crate::util as mir_util;
3232
use crate::util::pretty;
3333

3434
mod constraint_generation;
35-
pub mod explain_borrow;
3635
mod facts;
3736
mod invalidation;
38-
crate mod region_infer;
3937
mod renumber;
40-
crate mod type_check;
41-
mod universal_regions;
4238

43-
mod constraints;
4439
mod member_constraints;
4540

41+
crate mod constraints;
42+
crate mod universal_regions;
43+
crate mod type_check;
44+
crate mod region_infer;
45+
4646
use self::facts::AllFacts;
4747
use self::region_infer::RegionInferenceContext;
4848
use self::universal_regions::UniversalRegions;

0 commit comments

Comments
 (0)