Skip to content

Commit d10497b

Browse files
authored
Rollup merge of #98415 - compiler-errors:rustc-borrowck-session-diagnostic-1, r=davidtwco
Migrate some `rustc_borrowck` diagnostics to `SessionDiagnostic` Self-explanatory r? ```@davidtwco```
2 parents 66c83ff + 98af1bf commit d10497b

20 files changed

+122
-43
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3595,6 +3595,7 @@ dependencies = [
35953595
"rustc_index",
35963596
"rustc_infer",
35973597
"rustc_lexer",
3598+
"rustc_macros",
35983599
"rustc_middle",
35993600
"rustc_mir_dataflow",
36003601
"rustc_serialize",

compiler/rustc_borrowck/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rustc_hir = { path = "../rustc_hir" }
1919
rustc_index = { path = "../rustc_index" }
2020
rustc_infer = { path = "../rustc_infer" }
2121
rustc_lexer = { path = "../rustc_lexer" }
22+
rustc_macros = { path = "../rustc_macros" }
2223
rustc_middle = { path = "../rustc_middle" }
2324
rustc_const_eval = { path = "../rustc_const_eval" }
2425
rustc_mir_dataflow = { path = "../rustc_mir_dataflow" }

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use std::fmt;
1919
use std::rc::Rc;
2020

2121
use crate::region_infer::values::RegionElement;
22+
use crate::session_diagnostics::HigherRankedErrorCause;
23+
use crate::session_diagnostics::HigherRankedLifetimeError;
24+
use crate::session_diagnostics::HigherRankedSubtypeError;
2225
use crate::MirBorrowckCtxt;
2326

2427
#[derive(Clone)]
@@ -69,7 +72,7 @@ impl<'tcx> UniverseInfo<'tcx> {
6972
// up in the existing UI tests. Consider investigating this
7073
// some more.
7174
mbcx.buffer_error(
72-
mbcx.infcx.tcx.sess.struct_span_err(cause.span, "higher-ranked subtype error"),
75+
mbcx.infcx.tcx.sess.create_err(HigherRankedSubtypeError { span: cause.span }),
7376
);
7477
}
7578
}
@@ -216,9 +219,12 @@ impl<'tcx> TypeOpInfo<'tcx> for PredicateQuery<'tcx> {
216219
tcx: TyCtxt<'tcx>,
217220
span: Span,
218221
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
219-
let mut err = tcx.sess.struct_span_err(span, "higher-ranked lifetime error");
220-
err.note(&format!("could not prove {}", self.canonical_query.value.value.predicate));
221-
err
222+
tcx.sess.create_err(HigherRankedLifetimeError {
223+
cause: Some(HigherRankedErrorCause::CouldNotProve {
224+
predicate: self.canonical_query.value.value.predicate.to_string(),
225+
}),
226+
span,
227+
})
222228
}
223229

224230
fn base_universe(&self) -> ty::UniverseIndex {
@@ -263,9 +269,12 @@ where
263269
tcx: TyCtxt<'tcx>,
264270
span: Span,
265271
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
266-
let mut err = tcx.sess.struct_span_err(span, "higher-ranked lifetime error");
267-
err.note(&format!("could not normalize `{}`", self.canonical_query.value.value.value));
268-
err
272+
tcx.sess.create_err(HigherRankedLifetimeError {
273+
cause: Some(HigherRankedErrorCause::CouldNotNormalize {
274+
value: self.canonical_query.value.value.value.to_string(),
275+
}),
276+
span,
277+
})
269278
}
270279

271280
fn base_universe(&self) -> ty::UniverseIndex {
@@ -326,7 +335,7 @@ impl<'tcx> TypeOpInfo<'tcx> for AscribeUserTypeQuery<'tcx> {
326335
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
327336
// FIXME: This error message isn't great, but it doesn't show up in the existing UI tests,
328337
// and is only the fallback when the nice error fails. Consider improving this some more.
329-
tcx.sess.struct_span_err(span, "higher-ranked lifetime error")
338+
tcx.sess.create_err(HigherRankedLifetimeError { cause: None, span })
330339
}
331340

332341
fn base_universe(&self) -> ty::UniverseIndex {
@@ -366,7 +375,7 @@ impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
366375
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
367376
// FIXME: This error message isn't great, but it doesn't show up in the existing UI tests,
368377
// and is only the fallback when the nice error fails. Consider improving this some more.
369-
tcx.sess.struct_span_err(span, "higher-ranked lifetime error for opaque type!")
378+
tcx.sess.create_err(HigherRankedLifetimeError { cause: None, span })
370379
}
371380

372381
fn base_universe(&self) -> ty::UniverseIndex {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_span::symbol::Ident;
2424
use rustc_span::Span;
2525

2626
use crate::borrowck_errors;
27+
use crate::session_diagnostics::GenericDoesNotLiveLongEnough;
2728

2829
use super::{OutlivesSuggestionBuilder, RegionName};
2930
use crate::region_infer::BlameConstraint;
@@ -196,9 +197,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
196197
// to report it; we could probably handle it by
197198
// iterating over the universal regions and reporting
198199
// an error that multiple bounds are required.
199-
self.buffer_error(self.infcx.tcx.sess.struct_span_err(
200-
type_test_span,
201-
&format!("`{}` does not live long enough", type_test.generic_kind),
200+
self.buffer_error(self.infcx.tcx.sess.create_err(
201+
GenericDoesNotLiveLongEnough {
202+
kind: type_test.generic_kind.to_string(),
203+
span: type_test_span,
204+
},
202205
));
203206
}
204207
}

compiler/rustc_borrowck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ mod places_conflict;
7676
mod prefixes;
7777
mod region_infer;
7878
mod renumber;
79+
mod session_diagnostics;
7980
mod type_check;
8081
mod universal_regions;
8182
mod used_muts;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
2+
use rustc_middle::ty::Ty;
3+
use rustc_span::Span;
4+
5+
#[derive(SessionDiagnostic)]
6+
#[error(borrowck::move_unsized, code = "E0161")]
7+
pub(crate) struct MoveUnsized<'tcx> {
8+
pub ty: Ty<'tcx>,
9+
#[primary_span]
10+
#[label]
11+
pub span: Span,
12+
}
13+
14+
#[derive(SessionDiagnostic)]
15+
#[error(borrowck::higher_ranked_lifetime_error)]
16+
pub(crate) struct HigherRankedLifetimeError {
17+
#[subdiagnostic]
18+
pub cause: Option<HigherRankedErrorCause>,
19+
#[primary_span]
20+
pub span: Span,
21+
}
22+
23+
#[derive(SessionSubdiagnostic)]
24+
pub(crate) enum HigherRankedErrorCause {
25+
#[note(borrowck::could_not_prove)]
26+
CouldNotProve { predicate: String },
27+
#[note(borrowck::could_not_normalize)]
28+
CouldNotNormalize { value: String },
29+
}
30+
31+
#[derive(SessionDiagnostic)]
32+
#[error(borrowck::higher_ranked_subtype_error)]
33+
pub(crate) struct HigherRankedSubtypeError {
34+
#[primary_span]
35+
pub span: Span,
36+
}
37+
38+
#[derive(SessionDiagnostic)]
39+
#[error(borrowck::generic_does_not_live_long_enough)]
40+
pub(crate) struct GenericDoesNotLiveLongEnough {
41+
pub kind: String,
42+
#[primary_span]
43+
pub span: Span,
44+
}

compiler/rustc_borrowck/src/type_check/mod.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use hir::OpaqueTyOrigin;
99
use rustc_data_structures::frozen::Frozen;
1010
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1111
use rustc_data_structures::vec_map::VecMap;
12-
use rustc_errors::struct_span_err;
1312
use rustc_hir as hir;
1413
use rustc_hir::def::DefKind;
1514
use rustc_hir::def_id::LocalDefId;
@@ -48,6 +47,7 @@ use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
4847
use rustc_mir_dataflow::move_paths::MoveData;
4948
use rustc_mir_dataflow::ResultsCursor;
5049

50+
use crate::session_diagnostics::MoveUnsized;
5151
use crate::{
5252
borrow_set::BorrowSet,
5353
constraints::{OutlivesConstraint, OutlivesConstraintSet},
@@ -1780,19 +1780,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
17801780
// slot or local, so to find all unsized rvalues it is enough
17811781
// to check all temps, return slots and locals.
17821782
if self.reported_errors.replace((ty, span)).is_none() {
1783-
let mut diag = struct_span_err!(
1784-
self.tcx().sess,
1785-
span,
1786-
E0161,
1787-
"cannot move a value of type {0}: the size of {0} \
1788-
cannot be statically determined",
1789-
ty
1790-
);
1791-
17921783
// While this is located in `nll::typeck` this error is not
17931784
// an NLL error, it's a required check to prevent creation
17941785
// of unsized rvalues in a call expression.
1795-
diag.emit();
1786+
self.tcx().sess.emit_err(MoveUnsized { ty, span });
17961787
}
17971788
}
17981789
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
borrowck-move-unsized =
2+
cannot move a value of type `{$ty}`
3+
.label = the size of `{$ty}` cannot be statically determined
4+
5+
borrowck-higher-ranked-lifetime-error =
6+
higher-ranked lifetime error
7+
8+
borrowck-could-not-prove =
9+
could not prove `{$predicate}`
10+
11+
borrowck-could-not-normalize =
12+
could not normalize `{$value}`
13+
14+
borrowck-higher-ranked-subtype-error =
15+
higher-ranked subtype error
16+
17+
generic-does-not-live-long-enough =
18+
`{$kind}` does not live long enough

compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fluent_messages! {
3535
privacy => "../locales/en-US/privacy.ftl",
3636
typeck => "../locales/en-US/typeck.ftl",
3737
builtin_macros => "../locales/en-US/builtin_macros.ftl",
38+
borrowck => "../locales/en-US/borrowck.ftl",
3839
}
3940

4041
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

compiler/rustc_errors/src/emitter.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,19 @@ pub trait Emitter {
281281
let message = bundle.get_message(&identifier).expect("missing diagnostic in fluent bundle");
282282
let value = match attr {
283283
Some(attr) => {
284-
message.get_attribute(attr).expect("missing attribute in fluent message").value()
284+
if let Some(attr) = message.get_attribute(attr) {
285+
attr.value()
286+
} else {
287+
panic!("missing attribute `{attr}` in fluent message `{identifier}`")
288+
}
289+
}
290+
None => {
291+
if let Some(value) = message.value() {
292+
value
293+
} else {
294+
panic!("missing value in fluent message `{identifier}`")
295+
}
285296
}
286-
None => message.value().expect("missing value in fluent message"),
287297
};
288298

289299
let mut err = vec![];

src/test/ui/dst/dst-index.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0161]: cannot move a value of type str: the size of str cannot be statically determined
1+
error[E0161]: cannot move a value of type `str`
22
--> $DIR/dst-index.rs:31:5
33
|
44
LL | S[0];
5-
| ^^^^
5+
| ^^^^ the size of `str` cannot be statically determined
66

7-
error[E0161]: cannot move a value of type dyn Debug: the size of dyn Debug cannot be statically determined
7+
error[E0161]: cannot move a value of type `dyn Debug`
88
--> $DIR/dst-index.rs:34:5
99
|
1010
LL | T[0];
11-
| ^^^^
11+
| ^^^^ the size of `dyn Debug` cannot be statically determined
1212

1313
error[E0507]: cannot move out of index of `S`
1414
--> $DIR/dst-index.rs:31:5

src/test/ui/error-codes/E0161.base.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0161]: cannot move a value of type dyn Bar: the size of dyn Bar cannot be statically determined
1+
error[E0161]: cannot move a value of type `dyn Bar`
22
--> $DIR/E0161.rs:16:5
33
|
44
LL | x.f();
5-
| ^^^^^
5+
| ^^^^^ the size of `dyn Bar` cannot be statically determined
66

77
error: aborting due to previous error
88

src/test/ui/higher-rank-trait-bounds/issue-59311.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error: higher-ranked lifetime error
44
LL | v.t(|| {});
55
| ^^^^^^^^^^
66
|
7-
= note: could not prove [closure@$DIR/issue-59311.rs:17:9: 17:14] well-formed
7+
= note: could not prove `[closure@$DIR/issue-59311.rs:17:9: 17:14] well-formed`
88

99
error: higher-ranked lifetime error
1010
--> $DIR/issue-59311.rs:17:9
1111
|
1212
LL | v.t(|| {});
1313
| ^^^^^
1414
|
15-
= note: could not prove for<'a> &'a V: 'static
15+
= note: could not prove `for<'a> &'a V: 'static`
1616

1717
error: aborting due to 2 previous errors
1818

src/test/ui/lifetimes/re-empty-in-error.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: higher-ranked lifetime error
44
LL | foo(&10);
55
| ^^^^^^^^
66
|
7-
= note: could not prove for<'b, 'r> &'b (): 'r
7+
= note: could not prove `for<'b, 'r> &'b (): 'r`
88

99
error: aborting due to previous error
1010

src/test/ui/mir/issue-67947.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct Bug {
22
A: [(); { *"" }.len()],
3-
//~^ ERROR: cannot move a value of type str
3+
//~^ ERROR: cannot move a value of type `str`
44
//~| ERROR: cannot move out of a shared reference
55
}
66

src/test/ui/mir/issue-67947.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0161]: cannot move a value of type str: the size of str cannot be statically determined
1+
error[E0161]: cannot move a value of type `str`
22
--> $DIR/issue-67947.rs:2:13
33
|
44
LL | A: [(); { *"" }.len()],
5-
| ^^^^^^^
5+
| ^^^^^^^ the size of `str` cannot be statically determined
66

77
error[E0507]: cannot move out of a shared reference
88
--> $DIR/issue-67947.rs:2:15

src/test/ui/object-safety/object-safety-by-value-self-use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait Baz {
1212
}
1313

1414
fn use_bar(t: Box<dyn Bar>) {
15-
t.bar() //~ ERROR cannot move a value of type dyn Bar
15+
t.bar() //~ ERROR cannot move a value of type `dyn Bar`
1616
}
1717

1818
fn main() { }

src/test/ui/object-safety/object-safety-by-value-self-use.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0161]: cannot move a value of type dyn Bar: the size of dyn Bar cannot be statically determined
1+
error[E0161]: cannot move a value of type `dyn Bar`
22
--> $DIR/object-safety-by-value-self-use.rs:15:5
33
|
44
LL | t.bar()
5-
| ^^^^^^^
5+
| ^^^^^^^ the size of `dyn Bar` cannot be statically determined
66

77
error: aborting due to previous error
88

src/test/ui/unsized/return-unsized-from-trait-method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait Foo {
77
fn foo(f: Option<&dyn Foo>) {
88
if let Some(f) = f {
99
let _ = f.foo();
10-
//~^ ERROR cannot move a value of type [u8]: the size of [u8] cannot be statically determined
10+
//~^ ERROR cannot move a value of type `[u8]`
1111
}
1212
}
1313

src/test/ui/unsized/return-unsized-from-trait-method.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0161]: cannot move a value of type [u8]: the size of [u8] cannot be statically determined
1+
error[E0161]: cannot move a value of type `[u8]`
22
--> $DIR/return-unsized-from-trait-method.rs:9:17
33
|
44
LL | let _ = f.foo();
5-
| ^^^^^^^
5+
| ^^^^^^^ the size of `[u8]` cannot be statically determined
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)