Skip to content

Commit 44eb974

Browse files
committed
migrate rustc_hir_analysis to session diagnostic
part two files list: rustc_hir_analysis/variance/* rustc_hir_analysis/missing_cast_for_variadic_arg.rs rustc_hir_analysis/sized_unsized_cast.rs
1 parent b171953 commit 44eb974

File tree

5 files changed

+68
-28
lines changed

5 files changed

+68
-28
lines changed

compiler/rustc_hir_analysis/locales/en-US.ftl

+8
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,11 @@ hir_analysis_cannot_capture_late_bound_ty_in_anon_const =
155155
hir_analysis_cannot_capture_late_bound_const_in_anon_const =
156156
cannot capture late-bound const parameter in a constant
157157
.label = parameter defined here
158+
159+
hir_analysis_variances_of = {$variances_of}
160+
161+
hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function
162+
.suggestion = cast the value to `{$cast_ty}`
163+
.help = cast the value to `{$cast_ty}`
164+
165+
hir_analysis_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_ty}` to fat pointer `{$cast_ty}`

compiler/rustc_hir_analysis/src/errors.rs

+31
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,34 @@ pub(crate) enum CannotCaptureLateBoundInAnonConst {
399399
def_span: Span,
400400
},
401401
}
402+
403+
#[derive(Diagnostic)]
404+
#[diag(hir_analysis_variances_of)]
405+
pub(crate) struct VariancesOf {
406+
#[primary_span]
407+
pub span: Span,
408+
pub variances_of: String,
409+
}
410+
411+
#[derive(Diagnostic)]
412+
#[diag(hir_analysis_pass_to_variadic_function, code = "E0617")]
413+
pub(crate) struct PassToVariadicFunction<'tcx, 'a> {
414+
#[primary_span]
415+
pub span: Span,
416+
pub ty: Ty<'tcx>,
417+
pub cast_ty: &'a str,
418+
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
419+
pub sugg_span: Option<Span>,
420+
pub replace: String,
421+
#[help]
422+
pub help: Option<()>,
423+
}
424+
425+
#[derive(Diagnostic)]
426+
#[diag(hir_analysis_cast_thin_pointer_to_fat_pointer, code = "E0607")]
427+
pub(crate) struct CastThinPointerToFatPointer<'tcx> {
428+
#[primary_span]
429+
pub span: Span,
430+
pub expr_ty: Ty<'tcx>,
431+
pub cast_ty: String,
432+
}

compiler/rustc_hir_analysis/src/structured_errors/missing_cast_for_variadic_arg.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::structured_errors::StructuredDiagnostic;
2-
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
1+
use crate::{errors, structured_errors::StructuredDiagnostic};
2+
use rustc_errors::{DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
33
use rustc_middle::ty::{Ty, TypeVisitableExt};
44
use rustc_session::Session;
55
use rustc_span::Span;
@@ -21,27 +21,26 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx, '_> {
2121
}
2222

2323
fn diagnostic_common(&self) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
24-
let mut err = self.sess.struct_span_err_with_code(
25-
self.span,
26-
&format!("can't pass `{}` to variadic function", self.ty),
27-
self.code(),
28-
);
24+
let (sugg_span, replace, help) =
25+
if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) {
26+
(Some(self.span), format!("{} as {}", snippet, self.cast_ty), None)
27+
} else {
28+
(None, "".to_string(), Some(()))
29+
};
30+
31+
let mut err = self.sess.create_err(errors::PassToVariadicFunction {
32+
span: self.span,
33+
ty: self.ty,
34+
cast_ty: self.cast_ty,
35+
help,
36+
replace,
37+
sugg_span,
38+
});
2939

3040
if self.ty.references_error() {
3141
err.downgrade_to_delayed_bug();
3242
}
3343

34-
if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) {
35-
err.span_suggestion(
36-
self.span,
37-
&format!("cast the value to `{}`", self.cast_ty),
38-
format!("{} as {}", snippet, self.cast_ty),
39-
Applicability::MachineApplicable,
40-
);
41-
} else {
42-
err.help(&format!("cast the value to `{}`", self.cast_ty));
43-
}
44-
4544
err
4645
}
4746

compiler/rustc_hir_analysis/src/structured_errors/sized_unsized_cast.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::structured_errors::StructuredDiagnostic;
1+
use crate::{errors, structured_errors::StructuredDiagnostic};
22
use rustc_errors::{DiagnosticBuilder, DiagnosticId, ErrorGuaranteed};
33
use rustc_middle::ty::{Ty, TypeVisitableExt};
44
use rustc_session::Session;
@@ -21,14 +21,11 @@ impl<'tcx> StructuredDiagnostic<'tcx> for SizedUnsizedCast<'tcx> {
2121
}
2222

2323
fn diagnostic_common(&self) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
24-
let mut err = self.sess.struct_span_err_with_code(
25-
self.span,
26-
&format!(
27-
"cannot cast thin pointer `{}` to fat pointer `{}`",
28-
self.expr_ty, self.cast_ty
29-
),
30-
self.code(),
31-
);
24+
let mut err = self.sess.create_err(errors::CastThinPointerToFatPointer {
25+
span: self.span,
26+
expr_ty: self.expr_ty,
27+
cast_ty: self.cast_ty.to_owned(),
28+
});
3229

3330
if self.expr_ty.references_error() {
3431
err.downgrade_to_delayed_bug();
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
use rustc_middle::ty::TyCtxt;
22
use rustc_span::symbol::sym;
33

4+
use crate::errors;
5+
46
pub fn test_variance(tcx: TyCtxt<'_>) {
57
// For unit testing: check for a special "rustc_variance"
68
// attribute and report an error with various results if found.
79
for id in tcx.hir().items() {
810
if tcx.has_attr(id.owner_id.to_def_id(), sym::rustc_variance) {
911
let variances_of = tcx.variances_of(id.owner_id);
1012

11-
tcx.sess.struct_span_err(tcx.def_span(id.owner_id), format!("{variances_of:?}")).emit();
13+
tcx.sess.emit_err(errors::VariancesOf {
14+
span: tcx.def_span(id.owner_id),
15+
variances_of: format!("{variances_of:?}"),
16+
});
1217
}
1318
}
1419
}

0 commit comments

Comments
 (0)