Skip to content

Commit 1c8544b

Browse files
committed
Porting my first trait_selection diagnostics
1 parent dd01122 commit 1c8544b

File tree

7 files changed

+61
-19
lines changed

7 files changed

+61
-19
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dump_vtable_entries = vtable entries for `{$trait_ref}`: {$entries}
2+
3+
unable_to_construct_constant_value_unevaluated_constant = unable to construct a constant value for the unevaluated constant {$unevaluated}
4+
5+
reached_recursion_limit_auto_deref = reached the recursion limit while auto-dereferencing `{$ty}`
6+
deref_recursion_limit_reached = deref recursion limit reached
7+
deref_recursion_limit_reached_help = consider increasing the recursion limit by adding a `#![recursion_limit = "{$suggested_limit}"]` attribute to your crate (`{$crate_name}`)

compiler/rustc_error_messages/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fluent_messages! {
4141
parser => "../locales/en-US/parser.ftl",
4242
passes => "../locales/en-US/passes.ftl",
4343
privacy => "../locales/en-US/privacy.ftl",
44+
trait_selection => "../locales/en-US/trait_selection.ftl",
4445
typeck => "../locales/en-US/typeck.ftl",
4546
}
4647

compiler/rustc_trait_selection/src/autoderef.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::traits::query::evaluate_obligation::InferCtxtExt;
22
use crate::traits::{self, TraitEngine};
3-
use rustc_errors::struct_span_err;
3+
use crate::errors::ReachedRecursionLimitDeref;
44
use rustc_hir as hir;
55
use rustc_infer::infer::InferCtxt;
66
use rustc_middle::ty::{self, TraitRef, Ty, TyCtxt};
@@ -222,19 +222,10 @@ pub fn report_autoderef_recursion_limit_error<'tcx>(tcx: TyCtxt<'tcx>, span: Spa
222222
Limit(0) => Limit(2),
223223
limit => limit * 2,
224224
};
225-
struct_span_err!(
226-
tcx.sess,
225+
tcx.sess.emit_err(ReachedRecursionLimitDeref {
227226
span,
228-
E0055,
229-
"reached the recursion limit while auto-dereferencing `{:?}`",
230-
ty
231-
)
232-
.span_label(span, "deref recursion limit reached")
233-
.help(&format!(
234-
"consider increasing the recursion limit by adding a \
235-
`#![recursion_limit = \"{}\"]` attribute to your crate (`{}`)",
236-
suggested_limit,
237-
tcx.crate_name(LOCAL_CRATE),
238-
))
239-
.emit();
227+
ty: format!("{:?}", ty),
228+
suggested_limit: suggested_limit.to_string(),
229+
crate_name: tcx.crate_name(LOCAL_CRATE).to_string(),
230+
});
240231
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use rustc_span::Span;
2+
use rustc_macros::SessionDiagnostic;
3+
4+
#[derive(SessionDiagnostic)]
5+
#[error(trait_selection::dump_vtable_entries)]
6+
pub struct DumpVTableEntries {
7+
#[primary_span]
8+
pub span: Span,
9+
pub trait_ref: String,
10+
pub entries: String,
11+
}
12+
13+
#[derive(SessionDiagnostic)]
14+
#[error(trait_selection::unable_to_construct_constant_value_unevaluated_constant)]
15+
pub struct UnableToConstructConstantValueUnevaluatedConstant {
16+
#[primary_span]
17+
pub span: Span,
18+
pub unevaluated: String,
19+
}
20+
21+
#[derive(SessionDiagnostic)]
22+
#[help(trait_selection::deref_recursion_limit_reached_help)]
23+
#[error(trait_selection::reached_recursion_limit_auto_deref, code = "E0055")]
24+
pub struct ReachedRecursionLimitDeref {
25+
#[primary_span]
26+
#[label(trait_selection::deref_recursion_limit_reached)]
27+
pub span: Span,
28+
pub ty: String,
29+
pub suggested_limit: String,
30+
pub crate_name: String,
31+
}

compiler/rustc_trait_selection/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#![feature(if_let_guard)]
2222
#![feature(never_type)]
2323
#![recursion_limit = "512"] // For rustdoc
24+
#![deny(rustc::untranslatable_diagnostic)]
25+
#![deny(rustc::diagnostic_outside_of_impl)]
2426

2527
#[macro_use]
2628
extern crate rustc_macros;
@@ -35,5 +37,6 @@ extern crate rustc_middle;
3537
extern crate smallvec;
3638

3739
pub mod autoderef;
40+
pub mod errors;
3841
pub mod infer;
3942
pub mod traits;

compiler/rustc_trait_selection/src/traits/auto_trait.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use super::*;
55

6+
use crate::errors::UnableToConstructConstantValueUnevaluatedConstant;
67
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
78
use crate::infer::InferCtxt;
89
use crate::traits::project::ProjectAndUnifyResult;
@@ -830,8 +831,12 @@ impl<'tcx> AutoTraitFinder<'tcx> {
830831
Ok(None) => {
831832
let tcx = self.tcx;
832833
let def_id = unevaluated.def.did;
833-
let reported = tcx.sess.struct_span_err(tcx.def_span(def_id), &format!("unable to construct a constant value for the unevaluated constant {:?}", unevaluated)).emit();
834-
834+
let reported = tcx.sess.emit_err(
835+
UnableToConstructConstantValueUnevaluatedConstant {
836+
span: tcx.def_span(def_id),
837+
unevaluated: format!("{:?}", unevaluated),
838+
},
839+
);
835840
Err(ErrorHandled::Reported(reported))
836841
}
837842
Err(err) => Err(err),

compiler/rustc_trait_selection/src/traits/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod structural_match;
2222
mod util;
2323
pub mod wf;
2424

25+
use crate::errors::DumpVTableEntries;
2526
use crate::infer::outlives::env::OutlivesEnvironment;
2627
use crate::infer::{InferCtxt, TyCtxtInferExt};
2728
use crate::traits::error_reporting::InferCtxtExt as _;
@@ -748,8 +749,11 @@ fn dump_vtable_entries<'tcx>(
748749
trait_ref: ty::PolyTraitRef<'tcx>,
749750
entries: &[VtblEntry<'tcx>],
750751
) {
751-
let msg = format!("vtable entries for `{}`: {:#?}", trait_ref, entries);
752-
tcx.sess.struct_span_err(sp, &msg).emit();
752+
tcx.sess.emit_err(DumpVTableEntries {
753+
span: sp,
754+
trait_ref: trait_ref.to_string(),
755+
entries: format!("{:#?}", entries),
756+
});
753757
}
754758

755759
fn own_existential_vtable_entries<'tcx>(

0 commit comments

Comments
 (0)