Skip to content

Commit 886eae6

Browse files
authored
Rollup merge of rust-lang#114548 - fee1-dead-contrib:migrate-to-trans, r=davidtwco
Migrate a trait selection error to use diagnostic translation
2 parents d57f1fa + 1ca4bc9 commit 886eae6

File tree

5 files changed

+79
-41
lines changed

5 files changed

+79
-41
lines changed

compiler/rustc_middle/src/ty/closure.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::fmt::Write;
77

88
use crate::query::Providers;
99
use rustc_data_structures::fx::FxIndexMap;
10+
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
1011
use rustc_hir::def_id::{DefId, LocalDefId};
1112
use rustc_hir::{self as hir, LangItem};
1213
use rustc_span::def_id::LocalDefIdMap;
@@ -89,10 +90,18 @@ pub enum ClosureKind {
8990
FnOnce,
9091
}
9192

92-
impl<'tcx> ClosureKind {
93+
impl ClosureKind {
9394
/// This is the initial value used when doing upvar inference.
9495
pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn;
9596

97+
pub const fn as_str(self) -> &'static str {
98+
match self {
99+
ClosureKind::Fn => "Fn",
100+
ClosureKind::FnMut => "FnMut",
101+
ClosureKind::FnOnce => "FnOnce",
102+
}
103+
}
104+
96105
/// Returns `true` if a type that impls this closure kind
97106
/// must also implement `other`.
98107
pub fn extends(self, other: ty::ClosureKind) -> bool {
@@ -115,7 +124,7 @@ impl<'tcx> ClosureKind {
115124

116125
/// Returns the representative scalar type for this closure kind.
117126
/// See `Ty::to_opt_closure_kind` for more details.
118-
pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
127+
pub fn to_ty<'tcx>(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
119128
match self {
120129
ClosureKind::Fn => tcx.types.i8,
121130
ClosureKind::FnMut => tcx.types.i16,
@@ -124,6 +133,12 @@ impl<'tcx> ClosureKind {
124133
}
125134
}
126135

136+
impl IntoDiagnosticArg for ClosureKind {
137+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
138+
DiagnosticArgValue::Str(self.as_str().into())
139+
}
140+
}
141+
127142
/// A composite describing a `Place` that is captured by a closure.
128143
#[derive(PartialEq, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
129144
#[derive(TypeFoldable, TypeVisitable)]

compiler/rustc_middle/src/ty/print/pretty.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2870,11 +2870,7 @@ define_print_and_forward_display! {
28702870
}
28712871

28722872
ty::ClosureKind {
2873-
match *self {
2874-
ty::ClosureKind::Fn => p!("Fn"),
2875-
ty::ClosureKind::FnMut => p!("FnMut"),
2876-
ty::ClosureKind::FnOnce => p!("FnOnce"),
2877-
}
2873+
p!(write("{}", self.as_str()))
28782874
}
28792875

28802876
ty::Predicate<'tcx> {

compiler/rustc_trait_selection/messages.ftl

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ trait_selection_adjust_signature_remove_borrow = consider adjusting the signatur
88
*[other] arguments
99
}
1010
11+
trait_selection_closure_fn_mut_label = closure is `FnMut` because it mutates the variable `{$place}` here
12+
13+
trait_selection_closure_fn_once_label = closure is `FnOnce` because it moves the variable `{$place}` out of its environment
14+
15+
trait_selection_closure_kind_mismatch = expected a closure that implements the `{$expected}` trait, but this closure only implements `{$found}`
16+
.label = this closure implements `{$found}`, not `{$expected}`
17+
18+
trait_selection_closure_kind_requirement = the requirement to implement `{$expected}` derives from here
19+
1120
trait_selection_dump_vtable_entries = vtable entries for `{$trait_ref}`: {$entries}
1221
1322
trait_selection_empty_on_clause_in_rustc_on_unimplemented = empty `on`-clause in `#[rustc_on_unimplemented]`

compiler/rustc_trait_selection/src/errors.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::{
44
SubdiagnosticMessage,
55
};
66
use rustc_macros::Diagnostic;
7-
use rustc_middle::ty::{self, PolyTraitRef, Ty};
7+
use rustc_middle::ty::{self, ClosureKind, PolyTraitRef, Ty};
88
use rustc_span::{Span, Symbol};
99

1010
#[derive(Diagnostic)]
@@ -131,3 +131,37 @@ impl AddToDiagnostic for AdjustSignatureBorrow {
131131
}
132132
}
133133
}
134+
135+
#[derive(Diagnostic)]
136+
#[diag(trait_selection_closure_kind_mismatch, code = "E0525")]
137+
pub struct ClosureKindMismatch {
138+
#[primary_span]
139+
#[label]
140+
pub closure_span: Span,
141+
pub expected: ClosureKind,
142+
pub found: ClosureKind,
143+
#[label(trait_selection_closure_kind_requirement)]
144+
pub cause_span: Span,
145+
146+
#[subdiagnostic]
147+
pub fn_once_label: Option<ClosureFnOnceLabel>,
148+
149+
#[subdiagnostic]
150+
pub fn_mut_label: Option<ClosureFnMutLabel>,
151+
}
152+
153+
#[derive(Subdiagnostic)]
154+
#[label(trait_selection_closure_fn_once_label)]
155+
pub struct ClosureFnOnceLabel {
156+
#[primary_span]
157+
pub span: Span,
158+
pub place: String,
159+
}
160+
161+
#[derive(Subdiagnostic)]
162+
#[label(trait_selection_closure_fn_mut_label)]
163+
pub struct ClosureFnMutLabel {
164+
#[primary_span]
165+
pub span: Span,
166+
pub place: String,
167+
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+17-33
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::{
77
ObligationCauseCode, ObligationCtxt, OutputTypeParameterMismatch, Overflow,
88
PredicateObligation, SelectionError, TraitNotObjectSafe,
99
};
10+
use crate::errors::{ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch};
1011
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
1112
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1213
use crate::infer::{self, InferCtxt};
@@ -3142,55 +3143,38 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31423143
kind: ty::ClosureKind,
31433144
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
31443145
let closure_span = self.tcx.def_span(closure_def_id);
3145-
let mut err = struct_span_err!(
3146-
self.tcx.sess,
3147-
closure_span,
3148-
E0525,
3149-
"expected a closure that implements the `{}` trait, \
3150-
but this closure only implements `{}`",
3151-
kind,
3152-
found_kind
3153-
);
31543146

3155-
err.span_label(
3147+
let mut err = ClosureKindMismatch {
31563148
closure_span,
3157-
format!("this closure implements `{found_kind}`, not `{kind}`"),
3158-
);
3159-
err.span_label(
3160-
obligation.cause.span,
3161-
format!("the requirement to implement `{kind}` derives from here"),
3162-
);
3149+
expected: kind,
3150+
found: found_kind,
3151+
cause_span: obligation.cause.span,
3152+
fn_once_label: None,
3153+
fn_mut_label: None,
3154+
};
31633155

31643156
// Additional context information explaining why the closure only implements
31653157
// a particular trait.
31663158
if let Some(typeck_results) = &self.typeck_results {
31673159
let hir_id = self.tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
31683160
match (found_kind, typeck_results.closure_kind_origins().get(hir_id)) {
31693161
(ty::ClosureKind::FnOnce, Some((span, place))) => {
3170-
err.span_label(
3171-
*span,
3172-
format!(
3173-
"closure is `FnOnce` because it moves the \
3174-
variable `{}` out of its environment",
3175-
ty::place_to_string_for_capture(self.tcx, place)
3176-
),
3177-
);
3162+
err.fn_once_label = Some(ClosureFnOnceLabel {
3163+
span: *span,
3164+
place: ty::place_to_string_for_capture(self.tcx, &place),
3165+
})
31783166
}
31793167
(ty::ClosureKind::FnMut, Some((span, place))) => {
3180-
err.span_label(
3181-
*span,
3182-
format!(
3183-
"closure is `FnMut` because it mutates the \
3184-
variable `{}` here",
3185-
ty::place_to_string_for_capture(self.tcx, place)
3186-
),
3187-
);
3168+
err.fn_mut_label = Some(ClosureFnMutLabel {
3169+
span: *span,
3170+
place: ty::place_to_string_for_capture(self.tcx, &place),
3171+
})
31883172
}
31893173
_ => {}
31903174
}
31913175
}
31923176

3193-
err
3177+
self.tcx.sess.create_err(err)
31943178
}
31953179

31963180
fn report_type_parameter_mismatch_cyclic_type_error(

0 commit comments

Comments
 (0)