(Mostly) revert "Account for type param from other item in note_and_explain
"
This mostly reverts commit 7449478c2f
.
It also removes an `opt_param_at` that really is unnecessary given our
ICE policy for malformed intrinsics.
This commit is contained in:
parent
5ee4db4e05
commit
a25bb5f4ac
3 changed files with 45 additions and 93 deletions
|
@ -164,9 +164,8 @@ pub fn check_intrinsic_type(
|
||||||
) {
|
) {
|
||||||
let generics = tcx.generics_of(intrinsic_id);
|
let generics = tcx.generics_of(intrinsic_id);
|
||||||
let param = |n| {
|
let param = |n| {
|
||||||
if let Some(&ty::GenericParamDef {
|
if let &ty::GenericParamDef { name, kind: ty::GenericParamDefKind::Type { .. }, .. } =
|
||||||
name, kind: ty::GenericParamDefKind::Type { .. }, ..
|
generics.param_at(n as usize, tcx)
|
||||||
}) = generics.opt_param_at(n as usize, tcx)
|
|
||||||
{
|
{
|
||||||
Ty::new_param(tcx, n, name)
|
Ty::new_param(tcx, n, name)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -54,18 +54,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
}
|
}
|
||||||
(ty::Param(expected), ty::Param(found)) => {
|
(ty::Param(expected), ty::Param(found)) => {
|
||||||
let generics = tcx.generics_of(body_owner_def_id);
|
let generics = tcx.generics_of(body_owner_def_id);
|
||||||
if let Some(param) = generics.opt_type_param(expected, tcx) {
|
let e_span = tcx.def_span(generics.type_param(expected, tcx).def_id);
|
||||||
let e_span = tcx.def_span(param.def_id);
|
|
||||||
if !sp.contains(e_span) {
|
if !sp.contains(e_span) {
|
||||||
diag.span_label(e_span, "expected type parameter");
|
diag.span_label(e_span, "expected type parameter");
|
||||||
}
|
}
|
||||||
}
|
let f_span = tcx.def_span(generics.type_param(found, tcx).def_id);
|
||||||
if let Some(param) = generics.opt_type_param(found, tcx) {
|
|
||||||
let f_span = tcx.def_span(param.def_id);
|
|
||||||
if !sp.contains(f_span) {
|
if !sp.contains(f_span) {
|
||||||
diag.span_label(f_span, "found type parameter");
|
diag.span_label(f_span, "found type parameter");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
diag.note(
|
diag.note(
|
||||||
"a type parameter was expected, but a different one was found; \
|
"a type parameter was expected, but a different one was found; \
|
||||||
you might be missing a type parameter or trait bound",
|
you might be missing a type parameter or trait bound",
|
||||||
|
@ -87,10 +83,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
| (ty::Alias(ty::Projection, proj), ty::Param(p))
|
| (ty::Alias(ty::Projection, proj), ty::Param(p))
|
||||||
if !tcx.is_impl_trait_in_trait(proj.def_id) =>
|
if !tcx.is_impl_trait_in_trait(proj.def_id) =>
|
||||||
{
|
{
|
||||||
let parent = tcx
|
let param = tcx.generics_of(body_owner_def_id).type_param(p, tcx);
|
||||||
.generics_of(body_owner_def_id)
|
|
||||||
.opt_type_param(p, tcx)
|
|
||||||
.and_then(|param| {
|
|
||||||
let p_def_id = param.def_id;
|
let p_def_id = param.def_id;
|
||||||
let p_span = tcx.def_span(p_def_id);
|
let p_span = tcx.def_span(p_def_id);
|
||||||
let expected = match (values.expected.kind(), values.found.kind()) {
|
let expected = match (values.expected.kind(), values.found.kind()) {
|
||||||
|
@ -99,16 +92,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
_ => "",
|
_ => "",
|
||||||
};
|
};
|
||||||
if !sp.contains(p_span) {
|
if !sp.contains(p_span) {
|
||||||
diag.span_label(
|
diag.span_label(p_span, format!("{expected}this type parameter"));
|
||||||
p_span,
|
|
||||||
format!("{expected}this type parameter"),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
p_def_id.as_local().and_then(|id| {
|
let parent = p_def_id.as_local().and_then(|id| {
|
||||||
let local_id = tcx.local_def_id_to_hir_id(id);
|
let local_id = tcx.local_def_id_to_hir_id(id);
|
||||||
let generics = tcx.parent_hir_node(local_id).generics()?;
|
let generics = tcx.parent_hir_node(local_id).generics()?;
|
||||||
Some((id, generics))
|
Some((id, generics))
|
||||||
})
|
|
||||||
});
|
});
|
||||||
let mut note = true;
|
let mut note = true;
|
||||||
if let Some((local_id, generics)) = parent {
|
if let Some((local_id, generics)) = parent {
|
||||||
|
@ -183,8 +172,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
(ty::Param(p), ty::Dynamic(..) | ty::Alias(ty::Opaque, ..))
|
(ty::Param(p), ty::Dynamic(..) | ty::Alias(ty::Opaque, ..))
|
||||||
| (ty::Dynamic(..) | ty::Alias(ty::Opaque, ..), ty::Param(p)) => {
|
| (ty::Dynamic(..) | ty::Alias(ty::Opaque, ..), ty::Param(p)) => {
|
||||||
let generics = tcx.generics_of(body_owner_def_id);
|
let generics = tcx.generics_of(body_owner_def_id);
|
||||||
if let Some(param) = generics.opt_type_param(p, tcx) {
|
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
|
||||||
let p_span = tcx.def_span(param.def_id);
|
|
||||||
let expected = match (values.expected.kind(), values.found.kind()) {
|
let expected = match (values.expected.kind(), values.found.kind()) {
|
||||||
(ty::Param(_), _) => "expected ",
|
(ty::Param(_), _) => "expected ",
|
||||||
(_, ty::Param(_)) => "found ",
|
(_, ty::Param(_)) => "found ",
|
||||||
|
@ -193,7 +181,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
if !sp.contains(p_span) {
|
if !sp.contains(p_span) {
|
||||||
diag.span_label(p_span, format!("{expected}this type parameter"));
|
diag.span_label(p_span, format!("{expected}this type parameter"));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
diag.help("type parameters must be constrained to match other types");
|
diag.help("type parameters must be constrained to match other types");
|
||||||
if tcx.sess.teach(diag.code.unwrap()) {
|
if tcx.sess.teach(diag.code.unwrap()) {
|
||||||
diag.help(
|
diag.help(
|
||||||
|
@ -233,12 +220,10 @@ impl<T> Trait<T> for X {
|
||||||
ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..),
|
ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..),
|
||||||
) => {
|
) => {
|
||||||
let generics = tcx.generics_of(body_owner_def_id);
|
let generics = tcx.generics_of(body_owner_def_id);
|
||||||
if let Some(param) = generics.opt_type_param(p, tcx) {
|
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
|
||||||
let p_span = tcx.def_span(param.def_id);
|
|
||||||
if !sp.contains(p_span) {
|
if !sp.contains(p_span) {
|
||||||
diag.span_label(p_span, "expected this type parameter");
|
diag.span_label(p_span, "expected this type parameter");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
diag.help(format!(
|
diag.help(format!(
|
||||||
"every closure has a distinct type and so could not always match the \
|
"every closure has a distinct type and so could not always match the \
|
||||||
caller-chosen type of parameter `{p}`"
|
caller-chosen type of parameter `{p}`"
|
||||||
|
@ -246,8 +231,7 @@ impl<T> Trait<T> for X {
|
||||||
}
|
}
|
||||||
(ty::Param(p), _) | (_, ty::Param(p)) => {
|
(ty::Param(p), _) | (_, ty::Param(p)) => {
|
||||||
let generics = tcx.generics_of(body_owner_def_id);
|
let generics = tcx.generics_of(body_owner_def_id);
|
||||||
if let Some(param) = generics.opt_type_param(p, tcx) {
|
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
|
||||||
let p_span = tcx.def_span(param.def_id);
|
|
||||||
let expected = match (values.expected.kind(), values.found.kind()) {
|
let expected = match (values.expected.kind(), values.found.kind()) {
|
||||||
(ty::Param(_), _) => "expected ",
|
(ty::Param(_), _) => "expected ",
|
||||||
(_, ty::Param(_)) => "found ",
|
(_, ty::Param(_)) => "found ",
|
||||||
|
@ -257,7 +241,6 @@ impl<T> Trait<T> for X {
|
||||||
diag.span_label(p_span, format!("{expected}this type parameter"));
|
diag.span_label(p_span, format!("{expected}this type parameter"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
(ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)
|
(ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)
|
||||||
if !tcx.is_impl_trait_in_trait(proj_ty.def_id) =>
|
if !tcx.is_impl_trait_in_trait(proj_ty.def_id) =>
|
||||||
{
|
{
|
||||||
|
@ -545,10 +528,8 @@ impl<T> Trait<T> for X {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
let generics = tcx.generics_of(body_owner_def_id);
|
let generics = tcx.generics_of(body_owner_def_id);
|
||||||
let Some(param) = generics.opt_type_param(param_ty, tcx) else {
|
let def_id = generics.type_param(param_ty, tcx).def_id;
|
||||||
return false;
|
let Some(def_id) = def_id.as_local() else {
|
||||||
};
|
|
||||||
let Some(def_id) = param.def_id.as_local() else {
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -244,20 +244,6 @@ impl<'tcx> Generics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the `GenericParamDef` with the given index if available.
|
|
||||||
pub fn opt_param_at(
|
|
||||||
&'tcx self,
|
|
||||||
param_index: usize,
|
|
||||||
tcx: TyCtxt<'tcx>,
|
|
||||||
) -> Option<&'tcx GenericParamDef> {
|
|
||||||
if let Some(index) = param_index.checked_sub(self.parent_count) {
|
|
||||||
self.own_params.get(index)
|
|
||||||
} else {
|
|
||||||
tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
|
|
||||||
.opt_param_at(param_index, tcx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
|
pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
|
||||||
if let Some(index) = param_index.checked_sub(self.parent_count) {
|
if let Some(index) = param_index.checked_sub(self.parent_count) {
|
||||||
&self.own_params[..index]
|
&self.own_params[..index]
|
||||||
|
@ -289,20 +275,6 @@ impl<'tcx> Generics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the `GenericParamDef` associated with this `ParamTy` if it belongs to this
|
|
||||||
/// `Generics`.
|
|
||||||
pub fn opt_type_param(
|
|
||||||
&'tcx self,
|
|
||||||
param: ParamTy,
|
|
||||||
tcx: TyCtxt<'tcx>,
|
|
||||||
) -> Option<&'tcx GenericParamDef> {
|
|
||||||
let param = self.opt_param_at(param.index as usize, tcx)?;
|
|
||||||
match param.kind {
|
|
||||||
GenericParamDefKind::Type { .. } => Some(param),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the `GenericParamDef` associated with this `ParamConst`.
|
/// Returns the `GenericParamDef` associated with this `ParamConst`.
|
||||||
pub fn const_param(&'tcx self, param: ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
|
pub fn const_param(&'tcx self, param: ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
|
||||||
let param = self.param_at(param.index as usize, tcx);
|
let param = self.param_at(param.index as usize, tcx);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue