Skip to content

Commit a772589

Browse files
(Mostly) revert "Account for type param from other item in note_and_explain"
This mostly reverts commit 7449478. It also removes an `opt_param_at` that really is unnecessary given our ICE policy for malformed intrinsics.
1 parent 294f0e5 commit a772589

File tree

3 files changed

+45
-93
lines changed

3 files changed

+45
-93
lines changed

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,8 @@ pub fn check_intrinsic_type(
162162
) {
163163
let generics = tcx.generics_of(intrinsic_id);
164164
let param = |n| {
165-
if let Some(&ty::GenericParamDef {
166-
name, kind: ty::GenericParamDefKind::Type { .. }, ..
167-
}) = generics.opt_param_at(n as usize, tcx)
165+
if let &ty::GenericParamDef { name, kind: ty::GenericParamDefKind::Type { .. }, .. } =
166+
generics.param_at(n as usize, tcx)
168167
{
169168
Ty::new_param(tcx, n, name)
170169
} else {

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

+43-62
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
5454
}
5555
(ty::Param(expected), ty::Param(found)) => {
5656
let generics = tcx.generics_of(body_owner_def_id);
57-
if let Some(param) = generics.opt_type_param(expected, tcx) {
58-
let e_span = tcx.def_span(param.def_id);
59-
if !sp.contains(e_span) {
60-
diag.span_label(e_span, "expected type parameter");
61-
}
57+
let e_span = tcx.def_span(generics.type_param(expected, tcx).def_id);
58+
if !sp.contains(e_span) {
59+
diag.span_label(e_span, "expected type parameter");
6260
}
63-
if let Some(param) = generics.opt_type_param(found, tcx) {
64-
let f_span = tcx.def_span(param.def_id);
65-
if !sp.contains(f_span) {
66-
diag.span_label(f_span, "found type parameter");
67-
}
61+
let f_span = tcx.def_span(generics.type_param(found, tcx).def_id);
62+
if !sp.contains(f_span) {
63+
diag.span_label(f_span, "found type parameter");
6864
}
6965
diag.note(
7066
"a type parameter was expected, but a different one was found; \
@@ -87,29 +83,22 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
8783
| (ty::Alias(ty::Projection, proj), ty::Param(p))
8884
if !tcx.is_impl_trait_in_trait(proj.def_id) =>
8985
{
90-
let parent = tcx
91-
.generics_of(body_owner_def_id)
92-
.opt_type_param(p, tcx)
93-
.and_then(|param| {
94-
let p_def_id = param.def_id;
95-
let p_span = tcx.def_span(p_def_id);
96-
let expected = match (values.expected.kind(), values.found.kind()) {
97-
(ty::Param(_), _) => "expected ",
98-
(_, ty::Param(_)) => "found ",
99-
_ => "",
100-
};
101-
if !sp.contains(p_span) {
102-
diag.span_label(
103-
p_span,
104-
format!("{expected}this type parameter"),
105-
);
106-
}
107-
p_def_id.as_local().and_then(|id| {
108-
let local_id = tcx.local_def_id_to_hir_id(id);
109-
let generics = tcx.parent_hir_node(local_id).generics()?;
110-
Some((id, generics))
111-
})
112-
});
86+
let param = tcx.generics_of(body_owner_def_id).type_param(p, tcx);
87+
let p_def_id = param.def_id;
88+
let p_span = tcx.def_span(p_def_id);
89+
let expected = match (values.expected.kind(), values.found.kind()) {
90+
(ty::Param(_), _) => "expected ",
91+
(_, ty::Param(_)) => "found ",
92+
_ => "",
93+
};
94+
if !sp.contains(p_span) {
95+
diag.span_label(p_span, format!("{expected}this type parameter"));
96+
}
97+
let parent = p_def_id.as_local().and_then(|id| {
98+
let local_id = tcx.local_def_id_to_hir_id(id);
99+
let generics = tcx.parent_hir_node(local_id).generics()?;
100+
Some((id, generics))
101+
});
113102
let mut note = true;
114103
if let Some((local_id, generics)) = parent {
115104
// Synthesize the associated type restriction `Add<Output = Expected>`.
@@ -183,16 +172,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
183172
(ty::Param(p), ty::Dynamic(..) | ty::Alias(ty::Opaque, ..))
184173
| (ty::Dynamic(..) | ty::Alias(ty::Opaque, ..), ty::Param(p)) => {
185174
let generics = tcx.generics_of(body_owner_def_id);
186-
if let Some(param) = generics.opt_type_param(p, tcx) {
187-
let p_span = tcx.def_span(param.def_id);
188-
let expected = match (values.expected.kind(), values.found.kind()) {
189-
(ty::Param(_), _) => "expected ",
190-
(_, ty::Param(_)) => "found ",
191-
_ => "",
192-
};
193-
if !sp.contains(p_span) {
194-
diag.span_label(p_span, format!("{expected}this type parameter"));
195-
}
175+
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
176+
let expected = match (values.expected.kind(), values.found.kind()) {
177+
(ty::Param(_), _) => "expected ",
178+
(_, ty::Param(_)) => "found ",
179+
_ => "",
180+
};
181+
if !sp.contains(p_span) {
182+
diag.span_label(p_span, format!("{expected}this type parameter"));
196183
}
197184
diag.help("type parameters must be constrained to match other types");
198185
if tcx.sess.teach(diag.code.unwrap()) {
@@ -233,11 +220,9 @@ impl<T> Trait<T> for X {
233220
ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..),
234221
) => {
235222
let generics = tcx.generics_of(body_owner_def_id);
236-
if let Some(param) = generics.opt_type_param(p, tcx) {
237-
let p_span = tcx.def_span(param.def_id);
238-
if !sp.contains(p_span) {
239-
diag.span_label(p_span, "expected this type parameter");
240-
}
223+
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
224+
if !sp.contains(p_span) {
225+
diag.span_label(p_span, "expected this type parameter");
241226
}
242227
diag.help(format!(
243228
"every closure has a distinct type and so could not always match the \
@@ -246,16 +231,14 @@ impl<T> Trait<T> for X {
246231
}
247232
(ty::Param(p), _) | (_, ty::Param(p)) => {
248233
let generics = tcx.generics_of(body_owner_def_id);
249-
if let Some(param) = generics.opt_type_param(p, tcx) {
250-
let p_span = tcx.def_span(param.def_id);
251-
let expected = match (values.expected.kind(), values.found.kind()) {
252-
(ty::Param(_), _) => "expected ",
253-
(_, ty::Param(_)) => "found ",
254-
_ => "",
255-
};
256-
if !sp.contains(p_span) {
257-
diag.span_label(p_span, format!("{expected}this type parameter"));
258-
}
234+
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
235+
let expected = match (values.expected.kind(), values.found.kind()) {
236+
(ty::Param(_), _) => "expected ",
237+
(_, ty::Param(_)) => "found ",
238+
_ => "",
239+
};
240+
if !sp.contains(p_span) {
241+
diag.span_label(p_span, format!("{expected}this type parameter"));
259242
}
260243
}
261244
(ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)
@@ -545,10 +528,8 @@ impl<T> Trait<T> for X {
545528
return false;
546529
};
547530
let generics = tcx.generics_of(body_owner_def_id);
548-
let Some(param) = generics.opt_type_param(param_ty, tcx) else {
549-
return false;
550-
};
551-
let Some(def_id) = param.def_id.as_local() else {
531+
let def_id = generics.type_param(param_ty, tcx).def_id;
532+
let Some(def_id) = def_id.as_local() else {
552533
return false;
553534
};
554535

compiler/rustc_middle/src/ty/generics.rs

-28
Original file line numberDiff line numberDiff line change
@@ -237,20 +237,6 @@ impl<'tcx> Generics {
237237
}
238238
}
239239

240-
/// Returns the `GenericParamDef` with the given index if available.
241-
pub fn opt_param_at(
242-
&'tcx self,
243-
param_index: usize,
244-
tcx: TyCtxt<'tcx>,
245-
) -> Option<&'tcx GenericParamDef> {
246-
if let Some(index) = param_index.checked_sub(self.parent_count) {
247-
self.params.get(index)
248-
} else {
249-
tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
250-
.opt_param_at(param_index, tcx)
251-
}
252-
}
253-
254240
pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
255241
if let Some(index) = param_index.checked_sub(self.parent_count) {
256242
&self.params[..index]
@@ -282,20 +268,6 @@ impl<'tcx> Generics {
282268
}
283269
}
284270

285-
/// Returns the `GenericParamDef` associated with this `ParamTy` if it belongs to this
286-
/// `Generics`.
287-
pub fn opt_type_param(
288-
&'tcx self,
289-
param: &ParamTy,
290-
tcx: TyCtxt<'tcx>,
291-
) -> Option<&'tcx GenericParamDef> {
292-
let param = self.opt_param_at(param.index as usize, tcx)?;
293-
match param.kind {
294-
GenericParamDefKind::Type { .. } => Some(param),
295-
_ => None,
296-
}
297-
}
298-
299271
/// Returns the `GenericParamDef` associated with this `ParamConst`.
300272
pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
301273
let param = self.param_at(param.index as usize, tcx);

0 commit comments

Comments
 (0)