Skip to content

Commit ddfea0d

Browse files
committed
review comment
1 parent 20c622e commit ddfea0d

File tree

6 files changed

+26
-31
lines changed

6 files changed

+26
-31
lines changed

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ use rustc_infer::infer::TyCtxtInferExt;
99
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
1010
use rustc_middle::mir::{self, CallSource};
1111
use rustc_middle::ty::print::with_no_trimmed_paths;
12-
use rustc_middle::ty::TraitRef;
13-
use rustc_middle::ty::{suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, Param, Ty};
14-
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
12+
use rustc_middle::ty::{
13+
suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef,
14+
Param, TraitRef, Ty, TyParam,
15+
};
1516
use rustc_middle::util::{call_kind, CallDesugaringKind, CallKind};
1617
use rustc_session::parse::feature_err;
1718
use rustc_span::symbol::sym;
@@ -129,10 +130,9 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
129130
tcx,
130131
generics,
131132
err,
132-
&param_ty.name.as_str(),
133+
TyParam(param_ty.name.as_str(), None),
133134
&constraint,
134135
None,
135-
None,
136136
);
137137
}
138138
}

compiler/rustc_hir_analysis/src/astconv/errors.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use crate::errors::{
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_errors::{pluralize, struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
88
use rustc_hir as hir;
9-
use rustc_hir::def_id::{DefId, LocalDefId};
9+
use rustc_hir::def_id::DefId;
1010
use rustc_infer::traits::FulfillmentError;
11-
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty, TyCtxt};
11+
use rustc_middle::ty::{self, suggest_constraining_type_param, Ty, TyCtxt, TyParam};
1212
use rustc_session::parse::feature_err;
1313
use rustc_span::edit_distance::find_best_match_for_name;
1414
use rustc_span::symbol::{sym, Ident};
@@ -100,8 +100,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
100100
pub(crate) fn complain_about_assoc_type_not_found<I>(
101101
&self,
102102
all_candidates: impl Fn() -> I,
103-
ty_param_name: &str,
104-
ty_param_def_id: Option<LocalDefId>,
103+
ty_param: TyParam<'_>,
105104
assoc_name: Ident,
106105
span: Span,
107106
) -> ErrorGuaranteed
@@ -118,7 +117,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
118117
E0220,
119118
"associated type `{}` not found for `{}`",
120119
assoc_name,
121-
ty_param_name
120+
ty_param.0,
122121
);
123122

124123
if is_dummy {
@@ -200,7 +199,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
200199
),
201200
);
202201
let hir = self.tcx().hir();
203-
if let Some(def_id) = ty_param_def_id
202+
if let Some(def_id) = ty_param.1
203+
&& let Some(def_id) = def_id.as_local()
204204
&& let parent = hir.get_parent_item(hir.local_def_id_to_hir_id(def_id))
205205
&& let Some(generics) = hir.get_generics(parent.def_id)
206206
{
@@ -228,10 +228,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
228228
self.tcx(),
229229
generics,
230230
&mut err,
231-
&ty_param_name,
231+
ty_param,
232232
&trait_name,
233233
None,
234-
None,
235234
)
236235
&& suggested_name != assoc_name.name
237236
{
@@ -263,7 +262,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
263262

264263
err.span_suggestion(
265264
assoc_name.span,
266-
format!("`{ty_param_name}` has the following associated type"),
265+
format!("`{}` has the following associated type", ty_param.0),
267266
all_candidate_names.first().unwrap().to_string(),
268267
applicability,
269268
);

compiler/rustc_hir_analysis/src/astconv/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
2929
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
3030
use rustc_infer::traits::ObligationCause;
3131
use rustc_middle::middle::stability::AllowUnstable;
32-
use rustc_middle::ty::GenericParamDefKind;
3332
use rustc_middle::ty::{
34-
self, Const, GenericArgKind, GenericArgsRef, IsSuggestable, Ty, TyCtxt, TypeVisitableExt,
33+
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, IsSuggestable, Ty, TyCtxt,
34+
TyParam, TypeVisitableExt,
3535
};
3636
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
3737
use rustc_span::edit_distance::find_best_match_for_name;
@@ -1096,8 +1096,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10961096
(None, None) => {
10971097
let reported = self.complain_about_assoc_type_not_found(
10981098
all_candidates,
1099-
&ty_param_name.to_string(),
1100-
ty_param_def_id,
1099+
TyParam(&ty_param_name.to_string(), ty_param_def_id.map(|id| id.to_def_id())),
11011100
assoc_name,
11021101
span,
11031102
);

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::error::ExpectedFound;
88
use rustc_middle::ty::print::Printer;
99
use rustc_middle::{
1010
traits::ObligationCause,
11-
ty::{self, error::TypeError, print::FmtPrinter, suggest_constraining_type_param, Ty},
11+
ty::{self, error::TypeError, print::FmtPrinter, suggest_constraining_type_param, Ty, TyParam},
1212
};
1313
use rustc_span::{def_id::DefId, sym, BytePos, Span, Symbol};
1414

@@ -139,9 +139,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
139139
tcx,
140140
generics,
141141
diag,
142-
&proj.self_ty().to_string(),
142+
TyParam(&proj.self_ty().to_string(), None),
143143
&path,
144-
None,
145144
matching_span,
146145
);
147146
} else {
@@ -153,9 +152,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
153152
tcx,
154153
generics,
155154
diag,
156-
&proj.self_ty().to_string(),
155+
TyParam(&proj.self_ty().to_string(), None),
157156
&path,
158-
None,
159157
matching_span,
160158
);
161159
}

compiler/rustc_middle/src/ty/diagnostics.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ impl<'tcx> IntoDiagnosticArg for Ty<'tcx> {
2525
}
2626
}
2727

28+
pub struct TyParam<'a>(pub &'a str, pub Option<DefId>);
29+
2830
impl<'tcx> Ty<'tcx> {
2931
/// Similar to `Ty::is_primitive`, but also considers inferred numeric values to be primitive.
3032
pub fn is_primitive_ty(self) -> bool {
@@ -216,16 +218,15 @@ pub fn suggest_constraining_type_param(
216218
tcx: TyCtxt<'_>,
217219
generics: &hir::Generics<'_>,
218220
err: &mut Diagnostic,
219-
param_name: &str,
221+
ty_param: TyParam<'_>,
220222
constraint: &str,
221-
def_id: Option<DefId>,
222223
span_to_replace: Option<Span>,
223224
) -> bool {
224225
suggest_constraining_type_params(
225226
tcx,
226227
generics,
227228
err,
228-
[(param_name, constraint, def_id)].into_iter(),
229+
[(ty_param.0, constraint, ty_param.1)].into_iter(),
229230
span_to_replace,
230231
)
231232
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_middle::hir::map;
3131
use rustc_middle::ty::error::TypeError::{self, Sorts};
3232
use rustc_middle::ty::{
3333
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, GenericArgs,
34-
InferTy, IsSuggestable, ToPredicate, Ty, TyCtxt, TypeAndMut, TypeFoldable, TypeFolder,
34+
InferTy, IsSuggestable, ToPredicate, Ty, TyCtxt, TyParam, TypeAndMut, TypeFoldable, TypeFolder,
3535
TypeSuperFoldable, TypeVisitableExt, TypeckResults,
3636
};
3737
use rustc_span::def_id::LocalDefId;
@@ -649,9 +649,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
649649
self.tcx,
650650
generics,
651651
&mut err,
652-
&param_name,
652+
TyParam(&param_name, Some(trait_pred.def_id())),
653653
&constraint,
654-
Some(trait_pred.def_id()),
655654
None,
656655
) {
657656
return;
@@ -1102,9 +1101,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11021101
self.tcx,
11031102
generics,
11041103
err,
1105-
param.name.as_str(),
1104+
TyParam(param.name.as_str(), Some(clone_trait)),
11061105
"Clone",
1107-
Some(clone_trait),
11081106
None,
11091107
);
11101108
}

0 commit comments

Comments
 (0)