Skip to content

Commit 3fae390

Browse files
committed
Use IntoDiagnosticArg where it makes sense
1 parent 74f9973 commit 3fae390

File tree

5 files changed

+61
-41
lines changed

5 files changed

+61
-41
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ infer_source_kind_closure_return =
6363
6464
# generator_kind may need to be translated
6565
infer_need_type_info_in_generator =
66-
type inside {$generator_kind} must be known in this context
66+
type inside {$generator_kind ->
67+
[async_block] `async` block
68+
[async_closure] `async` closure
69+
[async_fn] `async fn` body
70+
*[generator] generator
71+
} must be known in this context
6772
6873
6974
infer_subtype = ...so that the {$requirement ->

compiler/rustc_infer/src/errors.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use rustc_hir::FnRetTy;
33
use rustc_macros::SessionDiagnostic;
44
use rustc_span::{BytePos, Span};
55

6+
use crate::infer::error_reporting::{
7+
need_type_info::{GeneratorKindAsDiagArg, UnderspecifiedArgKind},
8+
ObligationCauseAsDiagArg,
9+
};
10+
611
#[derive(SessionDiagnostic)]
712
#[diag(infer::opaque_hidden_type)]
813
pub struct OpaqueHiddenTypeDiag {
@@ -73,7 +78,7 @@ pub struct AmbigousReturn<'a> {
7378
pub struct NeedTypeInfoInGenerator<'a> {
7479
#[primary_span]
7580
pub span: Span,
76-
pub generator_kind: &'static str,
81+
pub generator_kind: GeneratorKindAsDiagArg,
7782
#[subdiagnostic]
7883
pub bad_label: InferenceBadError<'a>,
7984
}
@@ -85,7 +90,7 @@ pub struct InferenceBadError<'a> {
8590
#[primary_span]
8691
pub span: Span,
8792
pub bad_kind: &'static str,
88-
pub prefix_kind: &'static str,
93+
pub prefix_kind: UnderspecifiedArgKind,
8994
pub has_parent: bool,
9095
pub prefix: &'a str,
9196
pub parent_prefix: &'a str,
@@ -107,7 +112,7 @@ pub enum SourceKindSubdiag<'a> {
107112
type_name: String,
108113
kind: &'static str,
109114
x_kind: &'static str,
110-
prefix_kind: &'static str,
115+
prefix_kind: UnderspecifiedArgKind,
111116
prefix: &'a str,
112117
arg_name: String,
113118
},
@@ -199,7 +204,7 @@ pub enum RegionOriginNote<'a> {
199204
},
200205
WithRequirement {
201206
span: Span,
202-
requirement: &'static str,
207+
requirement: ObligationCauseAsDiagArg<'a>,
203208
expected_found: Option<(DiagnosticStyledString, DiagnosticStyledString)>,
204209
},
205210
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use crate::traits::{
5858
};
5959

6060
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
61-
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed};
61+
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg};
6262
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString, MultiSpan};
6363
use rustc_hir as hir;
6464
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -78,7 +78,7 @@ use std::{cmp, fmt, iter};
7878

7979
mod note;
8080

81-
mod need_type_info;
81+
pub(crate) mod need_type_info;
8282
pub use need_type_info::TypeAnnotationNeeded;
8383

8484
pub mod nice_region_error;
@@ -2811,7 +2811,6 @@ pub enum FailureCode {
28112811
pub trait ObligationCauseExt<'tcx> {
28122812
fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode;
28132813
fn as_requirement_str(&self) -> &'static str;
2814-
fn as_requirement_localised(&self) -> &'static str;
28152814
}
28162815

28172816
impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
@@ -2880,10 +2879,15 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
28802879
_ => "types are compatible",
28812880
}
28822881
}
2882+
}
2883+
2884+
/// Newtype to allow implementing IntoDiagnosticArg
2885+
pub struct ObligationCauseAsDiagArg<'tcx>(pub ObligationCause<'tcx>);
28832886

2884-
fn as_requirement_localised(&self) -> &'static str {
2887+
impl IntoDiagnosticArg for ObligationCauseAsDiagArg<'_> {
2888+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
28852889
use crate::traits::ObligationCauseCode::*;
2886-
match self.code() {
2890+
let kind = match self.0.code() {
28872891
CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => "method_compat",
28882892
CompareImplItemObligation { kind: ty::AssocKind::Type, .. } => "type_compat",
28892893
CompareImplItemObligation { kind: ty::AssocKind::Const, .. } => "const_compat",
@@ -2896,6 +2900,8 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
28962900
MethodReceiver => "method_correct_type",
28972901
_ => "other",
28982902
}
2903+
.into();
2904+
rustc_errors::DiagnosticArgValue::Str(kind)
28992905
}
29002906
}
29012907

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::errors::{
44
};
55
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
66
use crate::infer::InferCtxt;
7-
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
7+
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, IntoDiagnosticArg};
88
use rustc_hir as hir;
99
use rustc_hir::def::Res;
1010
use rustc_hir::def::{CtorOf, DefKind, Namespace};
@@ -65,6 +65,7 @@ pub struct InferenceDiagnosticsParentData {
6565
name: String,
6666
}
6767

68+
#[derive(Clone)]
6869
pub enum UnderspecifiedArgKind {
6970
Type { prefix: Cow<'static, str> },
7071
Const { is_parameter: bool },
@@ -101,7 +102,7 @@ impl InferenceDiagnosticsData {
101102
InferenceBadError {
102103
span,
103104
bad_kind,
104-
prefix_kind: self.kind.prefix_kind(),
105+
prefix_kind: self.kind.clone(),
105106
prefix: self.kind.try_get_prefix().unwrap_or_default(),
106107
name: self.name.clone(),
107108
has_parent,
@@ -130,14 +131,18 @@ impl InferenceDiagnosticsParentData {
130131
}
131132
}
132133

133-
impl UnderspecifiedArgKind {
134-
fn prefix_kind(&self) -> &'static str {
135-
match self {
134+
impl IntoDiagnosticArg for UnderspecifiedArgKind {
135+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
136+
let kind = match self {
136137
Self::Type { .. } => "type",
137138
Self::Const { is_parameter: true } => "const_with_param",
138139
Self::Const { is_parameter: false } => "const",
139-
}
140+
};
141+
rustc_errors::DiagnosticArgValue::Str(kind.into())
140142
}
143+
}
144+
145+
impl UnderspecifiedArgKind {
141146
fn try_get_prefix(&self) -> Option<&str> {
142147
match self {
143148
Self::Type { prefix } => Some(prefix.as_ref()),
@@ -405,7 +410,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
405410
span: insert_span,
406411
name: pattern_name.map(|name| name.to_string()).unwrap_or_else(String::new),
407412
x_kind: arg_data.where_x_is_kind(ty),
408-
prefix_kind: arg_data.kind.prefix_kind(),
413+
prefix_kind: arg_data.kind.clone(),
409414
prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
410415
arg_name: arg_data.name,
411416
kind: if pattern_name.is_some() { "with_pattern" } else { "other" },
@@ -417,7 +422,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
417422
span: insert_span,
418423
name: String::new(),
419424
x_kind: arg_data.where_x_is_kind(ty),
420-
prefix_kind: arg_data.kind.prefix_kind(),
425+
prefix_kind: arg_data.kind.clone(),
421426
prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
422427
arg_name: arg_data.name,
423428
kind: "closure",
@@ -568,12 +573,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
568573
NeedTypeInfoInGenerator {
569574
bad_label: data.make_bad_error(span),
570575
span,
571-
generator_kind: kind.descr(),
576+
generator_kind: GeneratorKindAsDiagArg(kind),
572577
}
573578
.into_diagnostic(&self.tcx.sess.parse_sess)
574579
}
575580
}
576581

582+
pub struct GeneratorKindAsDiagArg(pub hir::GeneratorKind);
583+
584+
impl IntoDiagnosticArg for GeneratorKindAsDiagArg {
585+
fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
586+
let kind = match self.0 {
587+
hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) => "async_block",
588+
hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Closure) => "async_closure",
589+
hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Fn) => "async_fn",
590+
hir::GeneratorKind::Gen => "generator",
591+
};
592+
rustc_errors::DiagnosticArgValue::Str(kind.into())
593+
}
594+
}
595+
577596
#[derive(Debug)]
578597
struct InferSource<'tcx> {
579598
span: Span,

compiler/rustc_infer/src/infer/error_reporting/note.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,26 @@
11
use crate::errors::RegionOriginNote;
2-
use crate::infer::error_reporting::{note_and_explain_region, ObligationCauseExt};
2+
use crate::infer::error_reporting::note_and_explain_region;
33
use crate::infer::{self, InferCtxt, SubregionOrigin};
44
use rustc_errors::{
5-
fluent, struct_span_err, AddSubdiagnostic, Diagnostic, DiagnosticBuilder, DiagnosticMessage,
6-
ErrorGuaranteed,
5+
fluent, struct_span_err, AddSubdiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
76
};
87
use rustc_middle::traits::ObligationCauseCode;
98
use rustc_middle::ty::error::TypeError;
109
use rustc_middle::ty::{self, Region};
1110

11+
use super::ObligationCauseAsDiagArg;
12+
1213
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
1314
pub(super) fn note_region_origin(&self, err: &mut Diagnostic, origin: &SubregionOrigin<'tcx>) {
14-
let mut label_or_note = |span, msg: DiagnosticMessage| {
15-
let sub_count = err.children.iter().filter(|d| d.span.is_dummy()).count();
16-
let expanded_sub_count = err.children.iter().filter(|d| !d.span.is_dummy()).count();
17-
let span_is_primary = err.span.primary_spans().iter().all(|&sp| sp == span);
18-
if span_is_primary && sub_count == 0 && expanded_sub_count == 0 {
19-
err.span_label(span, msg);
20-
} else if span_is_primary && expanded_sub_count == 0 {
21-
err.note(msg);
22-
} else {
23-
err.span_note(span, msg);
24-
}
25-
};
2615
match *origin {
2716
infer::Subtype(ref trace) => RegionOriginNote::WithRequirement {
2817
span: trace.cause.span,
29-
requirement: trace.cause.as_requirement_localised(),
18+
requirement: ObligationCauseAsDiagArg(trace.cause.clone()),
3019
expected_found: self.values_str(trace.values),
3120
}
3221
.add_to_diagnostic(err),
33-
infer::Reborrow(span) => {
34-
label_or_note(span, fluent::infer::reborrow);
35-
RegionOriginNote::Plain { span, msg: fluent::infer::reborrow }
36-
.add_to_diagnostic(err)
37-
}
22+
infer::Reborrow(span) => RegionOriginNote::Plain { span, msg: fluent::infer::reborrow }
23+
.add_to_diagnostic(err),
3824
infer::ReborrowUpvar(span, ref upvar_id) => {
3925
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
4026
RegionOriginNote::WithName {
@@ -46,7 +32,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
4632
.add_to_diagnostic(err);
4733
}
4834
infer::RelateObjectBound(span) => {
49-
label_or_note(span, fluent::infer::relate_object_bound);
5035
RegionOriginNote::Plain { span, msg: fluent::infer::relate_object_bound }
5136
.add_to_diagnostic(err);
5237
}

0 commit comments

Comments
 (0)