Skip to content

Commit c3a0fd0

Browse files
committed
good old stash
1 parent 94cb442 commit c3a0fd0

File tree

10 files changed

+547
-143
lines changed

10 files changed

+547
-143
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ use rustc_errors::{
44
use rustc_middle::ty::{self, Ty, TyCtxt};
55
use rustc_span::Span;
66

7+
use crate::session_diagnostics::{
8+
AssignBorrowErr, BorrowAcrossDestructor, BorrowAcrossGeneratorYield, InteriorDropMoveErr,
9+
PathShortLive, UseMutBorrowErr,
10+
};
11+
712
impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
813
pub(crate) fn cannot_move_when_borrowed(
914
&self,
@@ -29,17 +34,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
2934
borrow_span: Span,
3035
borrow_desc: &str,
3136
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
32-
let mut err = struct_span_err!(
33-
self,
34-
span,
35-
E0503,
36-
"cannot use {} because it was mutably borrowed",
37-
desc,
38-
);
39-
40-
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
41-
err.span_label(span, format!("use of borrowed {}", borrow_desc));
42-
err
37+
self.infcx.tcx.sess.create_err(UseMutBorrowErr { desc, borrow_desc, span, borrow_span })
4338
}
4439

4540
pub(crate) fn cannot_mutably_borrow_multiply(
@@ -99,26 +94,19 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
9994
old_loan_span: Span,
10095
old_load_end_span: Option<Span>,
10196
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
102-
let mut err = struct_span_err!(
103-
self,
104-
new_loan_span,
105-
E0524,
106-
"two closures require unique access to {} at the same time",
107-
desc,
108-
);
109-
if old_loan_span == new_loan_span {
110-
err.span_label(
111-
old_loan_span,
112-
"closures are constructed here in different iterations of loop",
113-
);
97+
use crate::session_diagnostics::ClosureConstructLabel::*;
98+
let (case, diff_span) = if old_loan_span == new_loan_span {
99+
(Both { old_loan_span }, None)
114100
} else {
115-
err.span_label(old_loan_span, "first closure is constructed here");
116-
err.span_label(new_loan_span, "second closure is constructed here");
117-
}
118-
if let Some(old_load_end_span) = old_load_end_span {
119-
err.span_label(old_load_end_span, "borrow from first closure ends here");
120-
}
121-
err
101+
(First { old_loan_span }, Some(new_loan_span))
102+
};
103+
self.infcx.tcx.sess.create_err(crate::session_diagnostics::TwoClosuresUniquelyBorrowErr {
104+
desc,
105+
case,
106+
new_loan_span,
107+
old_load_end_span,
108+
diff_span,
109+
})
122110
}
123111

124112
pub(crate) fn cannot_uniquely_borrow_by_one_closure(
@@ -242,17 +230,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
242230
borrow_span: Span,
243231
desc: &str,
244232
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
245-
let mut err = struct_span_err!(
246-
self,
247-
span,
248-
E0506,
249-
"cannot assign to {} because it is borrowed",
250-
desc,
251-
);
252-
253-
err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
254-
err.span_label(span, format!("assignment to borrowed {} occurs here", desc));
255-
err
233+
self.infcx.tcx.sess.create_err(AssignBorrowErr { desc, span, borrow_span })
256234
}
257235

258236
pub(crate) fn cannot_reassign_immutable(
@@ -312,15 +290,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
312290
move_from_span: Span,
313291
container_ty: Ty<'_>,
314292
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
315-
let mut err = struct_span_err!(
316-
self,
317-
move_from_span,
318-
E0509,
319-
"cannot move out of type `{}`, which implements the `Drop` trait",
320-
container_ty,
321-
);
322-
err.span_label(move_from_span, "cannot move out of here");
323-
err
293+
self.infcx.tcx.sess.create_err(InteriorDropMoveErr { container_ty, move_from_span })
324294
}
325295

326296
pub(crate) fn cannot_act_on_moved_value(
@@ -379,34 +349,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
379349
span: Span,
380350
yield_span: Span,
381351
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
382-
let mut err = struct_span_err!(
383-
self,
384-
span,
385-
E0626,
386-
"borrow may still be in use when generator yields",
387-
);
388-
err.span_label(yield_span, "possible yield occurs here");
389-
err
352+
self.infcx.tcx.sess.create_err(BorrowAcrossGeneratorYield { span, yield_span })
390353
}
391354

392355
pub(crate) fn cannot_borrow_across_destructor(
393356
&self,
394357
borrow_span: Span,
395358
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
396-
struct_span_err!(
397-
self,
398-
borrow_span,
399-
E0713,
400-
"borrow may still be in use when destructor runs",
401-
)
359+
self.infcx.tcx.sess.create_err(BorrowAcrossDestructor { borrow_span })
402360
}
403361

404362
pub(crate) fn path_does_not_live_long_enough(
405363
&self,
406364
span: Span,
407365
path: &str,
408366
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
409-
struct_span_err!(self, span, E0597, "{} does not live long enough", path,)
367+
self.infcx.tcx.sess.create_err(PathShortLive { path, span })
410368
}
411369

412370
pub(crate) fn cannot_return_reference_to_local(

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_span::Span;
88

99
use crate::diagnostics::{DescribePlaceOpt, UseSpans};
1010
use crate::prefixes::PrefixSet;
11+
use crate::session_diagnostics::AddMoveErr;
1112
use crate::MirBorrowckCtxt;
1213

1314
// Often when desugaring a pattern match we may have many individual moves in
@@ -512,9 +513,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
512513
let binding_span = bind_to.source_info.span;
513514

514515
if j == 0 {
515-
err.span_label(binding_span, "data moved here");
516+
err.subdiagnostic(AddMoveErr::Here { binding_span });
516517
} else {
517-
err.span_label(binding_span, "...and here");
518+
err.subdiagnostic(AddMoveErr::AndHere { binding_span });
518519
}
519520

520521
if binds_to.len() == 1 {
@@ -529,10 +530,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
529530
}
530531

531532
if binds_to.len() > 1 {
532-
err.note(
533-
"move occurs because these variables have types that \
534-
don't implement the `Copy` trait",
535-
);
533+
err.subdiagnostic(AddMoveErr::MovedNotCopy);
536534
}
537535
}
538536
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// #![deny(rustc::untranslatable_diagnostic)]
2+
// #![deny(rustc::diagnostic_outside_of_impl)]
3+
14
use rustc_errors::{Applicability, Diagnostic};
25
use rustc_hir as hir;
36
use rustc_hir::intravisit::Visitor;
@@ -14,6 +17,7 @@ use rustc_span::symbol::{kw, Symbol};
1417
use rustc_span::{sym, BytePos, Span};
1518

1619
use crate::diagnostics::BorrowedContentSource;
20+
use crate::session_diagnostics::FnMutBumpFn;
1721
use crate::MirBorrowckCtxt;
1822
use rustc_const_eval::util::collect_writes::FindAssignments;
1923

@@ -964,7 +968,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
964968

965969
/// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected.
966970
fn expected_fn_found_fn_mut_call(&self, err: &mut Diagnostic, sp: Span, act: &str) {
967-
err.span_label(sp, format!("cannot {act}"));
971+
err.subdiagnostic(FnMutBumpFn::Cannot { act, sp });
968972

969973
let hir = self.infcx.tcx.hir();
970974
let closure_id = self.mir_hir_id();
@@ -1018,9 +1022,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10181022
_ => None,
10191023
};
10201024
if let Some(span) = arg {
1021-
err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
1022-
err.span_label(func.span, "expects `Fn` instead of `FnMut`");
1023-
err.span_label(closure_span, "in this closure");
1025+
err.subdiagnostic(FnMutBumpFn::AcceptFnMut { span });
1026+
err.subdiagnostic(FnMutBumpFn::AcceptFn { span: func.span });
1027+
err.subdiagnostic(FnMutBumpFn::Here { span: closure_span });
10241028
look_at_return = false;
10251029
}
10261030
}
@@ -1041,12 +1045,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10411045
kind: hir::ImplItemKind::Fn(sig, _),
10421046
..
10431047
}) => {
1044-
err.span_label(ident.span, "");
1045-
err.span_label(
1046-
sig.decl.output.span(),
1047-
"change this to return `FnMut` instead of `Fn`",
1048-
);
1049-
err.span_label(closure_span, "in this closure");
1048+
err.subdiagnostic(FnMutBumpFn::EmptyLabel { span: ident.span });
1049+
err.subdiagnostic(FnMutBumpFn::ReturnFnMut { span: sig.decl.output.span() });
1050+
err.subdiagnostic(FnMutBumpFn::Here { span: closure_span });
10501051
}
10511052
_ => {}
10521053
}

compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ impl OutlivesSuggestionBuilder {
171171
if let (Some(fr_name), Some(outlived_fr_name)) = (fr_name, outlived_fr_name)
172172
&& !matches!(outlived_fr_name.source, RegionNameSource::Static)
173173
{
174-
diag.help(&format!(
175-
"consider adding the following bound: `{fr_name}: {outlived_fr_name}`",
176-
));
174+
diag.subdiagnostic(crate::session_diagnostics::OnLifetimeBound::Add { fr_name: &fr_name, outlived_fr_name: &outlived_fr_name });
177175
}
178176
}
179177

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
710710
},
711711
_ => LifetimeReturnCategoryErr::ShortReturn {
712712
span: *span,
713-
category_desc: category.description(),
713+
category: category.description(),
714714
free_region_name: &fr_name,
715715
outlived_fr_name,
716716
},

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_middle::ty::{self, DefIdTree, RegionVid, Ty};
1010
use rustc_span::symbol::{kw, sym, Ident, Symbol};
1111
use rustc_span::{Span, DUMMY_SP};
1212

13+
use crate::session_diagnostics::RegionNameLabels;
1314
use crate::{nll::ToRegionVid, universal_regions::DefiningTy, MirBorrowckCtxt};
1415

1516
/// A name for a particular region used in emitting diagnostics. This name could be a generated
@@ -133,16 +134,17 @@ impl RegionName {
133134
RegionNameHighlight::MatchedAdtAndSegment(span),
134135
_,
135136
) => {
136-
diag.span_label(*span, format!("let's call this `{self}`"));
137+
diag.subdiagnostic(RegionNameLabels::NameRegion { span: *span, rg_name: self });
137138
}
138139
RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::Occluded(
139140
span,
140141
type_name,
141142
)) => {
142-
diag.span_label(
143-
*span,
144-
format!("lifetime `{self}` appears in the type {type_name}"),
145-
);
143+
diag.subdiagnostic(RegionNameLabels::LifetimeInType {
144+
span: *span,
145+
type_name: &type_name,
146+
rg_name: self,
147+
});
146148
}
147149
RegionNameSource::AnonRegionFromOutput(
148150
RegionNameHighlight::Occluded(span, type_name),
@@ -156,10 +158,11 @@ impl RegionName {
156158
);
157159
}
158160
RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => {
159-
diag.span_label(
160-
*span,
161-
format!("lifetime `{self}` appears in the type of `{upvar_name}`"),
162-
);
161+
diag.subdiagnostic(RegionNameLabels::LifetimeInTypeOf {
162+
span: *span,
163+
upvar_name: upvar_name.to_ident_string(),
164+
rg_name: self,
165+
});
163166
}
164167
RegionNameSource::AnonRegionFromOutput(
165168
RegionNameHighlight::CannotMatchHirTy(span, type_name),
@@ -168,13 +171,17 @@ impl RegionName {
168171
diag.span_label(*span, format!("return type{mir_description} is {type_name}"));
169172
}
170173
RegionNameSource::AnonRegionFromYieldTy(span, type_name) => {
171-
diag.span_label(*span, format!("yield type is {type_name}"));
174+
diag.subdiagnostic(RegionNameLabels::YieldTypeIsTpye {
175+
span: *span,
176+
type_name: &type_name,
177+
});
172178
}
173179
RegionNameSource::AnonRegionFromImplSignature(span, location) => {
174-
diag.span_label(
175-
*span,
176-
format!("lifetime `{self}` appears in the `impl`'s {location}"),
177-
);
180+
diag.subdiagnostic(RegionNameLabels::LifetimeInImpl {
181+
span: *span,
182+
rg_name: self,
183+
location: &location,
184+
});
178185
}
179186
RegionNameSource::Static => {}
180187
}

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use rustc_span::Span;
1212
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
1313
use rustc_trait_selection::traits::ObligationCtxt;
1414

15+
use crate::session_diagnostics::{OpaqueTyDefineErrCause, OpaqueTypeNotDefine};
16+
1517
use super::RegionInferenceContext;
1618

1719
impl<'tcx> RegionInferenceContext<'tcx> {
@@ -367,15 +369,12 @@ fn check_opaque_type_parameter_valid(
367369
let arg_is_param = match arg.unpack() {
368370
GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)),
369371
GenericArgKind::Lifetime(lt) if lt.is_static() => {
370-
tcx.sess
371-
.struct_span_err(span, "non-defining opaque type use in defining scope")
372-
.span_label(
373-
tcx.def_span(opaque_generics.param_at(i, tcx).def_id),
374-
"cannot use static lifetime; use a bound lifetime \
375-
instead or remove the lifetime parameter from the \
376-
opaque type",
377-
)
378-
.emit();
372+
tcx.sess.emit_err(OpaqueTypeNotDefine {
373+
cause: OpaqueTyDefineErrCause::UsedStaticLifetime {
374+
span: tcx.def_span(opaque_generics.param_at(i, tcx).def_id),
375+
},
376+
span,
377+
});
379378
return false;
380379
}
381380
GenericArgKind::Lifetime(lt) => {
@@ -389,17 +388,14 @@ fn check_opaque_type_parameter_valid(
389388
} else {
390389
// Prevent `fn foo() -> Foo<u32>` from being defining.
391390
let opaque_param = opaque_generics.param_at(i, tcx);
392-
tcx.sess
393-
.struct_span_err(span, "non-defining opaque type use in defining scope")
394-
.span_note(
395-
tcx.def_span(opaque_param.def_id),
396-
&format!(
397-
"used non-generic {} `{}` for generic parameter",
398-
opaque_param.kind.descr(),
399-
arg,
400-
),
401-
)
402-
.emit();
391+
tcx.sess.emit_err(OpaqueTypeNotDefine {
392+
cause: OpaqueTyDefineErrCause::NonGenericUsed {
393+
span: tcx.def_span(opaque_param.def_id),
394+
descr: &opaque_param.kind.descr(),
395+
arg: arg.to_string(),
396+
},
397+
span,
398+
});
403399
return false;
404400
}
405401
}

0 commit comments

Comments
 (0)