parent
3bbc70a5f7
commit
7449478c2f
@ -54,14 +54,18 @@ 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);
|
||||||
let e_span = tcx.def_span(generics.type_param(expected, tcx).def_id);
|
if let Some(param) = generics.opt_type_param(expected, tcx) {
|
||||||
|
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",
|
||||||
@ -83,7 +87,10 @@ 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 p_def_id = tcx.generics_of(body_owner_def_id).type_param(p, tcx).def_id;
|
let parent = tcx.generics_of(body_owner_def_id)
|
||||||
|
.opt_type_param(p, tcx)
|
||||||
|
.and_then(|param| {
|
||||||
|
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()) {
|
||||||
(ty::Param(_), _) => "expected ",
|
(ty::Param(_), _) => "expected ",
|
||||||
@ -91,15 +98,18 @@ 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"),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
let hir = tcx.hir();
|
p_def_id.as_local().and_then(|id| {
|
||||||
let mut note = true;
|
let local_id = tcx.hir().local_def_id_to_hir_id(id);
|
||||||
let parent = p_def_id.as_local().and_then(|id| {
|
|
||||||
let local_id = hir.local_def_id_to_hir_id(id);
|
|
||||||
let generics = tcx.hir().find_parent(local_id)?.generics()?;
|
let generics = tcx.hir().find_parent(local_id)?.generics()?;
|
||||||
Some((id, generics))
|
Some((id, generics))
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
let mut note = true;
|
||||||
if let Some((local_id, generics)) = parent {
|
if let Some((local_id, generics)) = parent {
|
||||||
// Synthesize the associated type restriction `Add<Output = Expected>`.
|
// Synthesize the associated type restriction `Add<Output = Expected>`.
|
||||||
// FIXME: extract this logic for use in other diagnostics.
|
// FIXME: extract this logic for use in other diagnostics.
|
||||||
@ -172,7 +182,8 @@ 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);
|
||||||
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
|
if let Some(param) = generics.opt_type_param(p, tcx) {
|
||||||
|
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 ",
|
||||||
@ -181,6 +192,7 @@ 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.get_code().unwrap()) {
|
if tcx.sess.teach(&diag.get_code().unwrap()) {
|
||||||
diag.help(
|
diag.help(
|
||||||
@ -217,10 +229,12 @@ impl<T> Trait<T> for X {
|
|||||||
}
|
}
|
||||||
(ty::Param(p), ty::Closure(..) | ty::Coroutine(..)) => {
|
(ty::Param(p), ty::Closure(..) | ty::Coroutine(..)) => {
|
||||||
let generics = tcx.generics_of(body_owner_def_id);
|
let generics = tcx.generics_of(body_owner_def_id);
|
||||||
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
|
if let Some(param) = generics.opt_type_param(p, tcx) {
|
||||||
|
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}`"
|
||||||
@ -228,7 +242,8 @@ 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);
|
||||||
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
|
if let Some(param) = generics.opt_type_param(p, tcx) {
|
||||||
|
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 ",
|
||||||
@ -238,6 +253,7 @@ 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) =>
|
||||||
{
|
{
|
||||||
@ -368,8 +384,10 @@ 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 def_id = generics.type_param(param_ty, tcx).def_id;
|
let Some(param) = generics.opt_type_param(param_ty, tcx) else {
|
||||||
let Some(def_id) = def_id.as_local() else {
|
return false;
|
||||||
|
};
|
||||||
|
let Some(def_id) = param.def_id.as_local() else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -237,6 +237,20 @@ 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.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.params[..index]
|
&self.params[..index]
|
||||||
@ -268,6 +282,20 @@ 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);
|
||||||
|
7
tests/ui/malformed/do-not-ice-on-note_and_explain.rs
Normal file
7
tests/ui/malformed/do-not-ice-on-note_and_explain.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
struct A<B>(B);
|
||||||
|
impl<B>A<B>{fn d(){fn d(){Self(1)}}}
|
||||||
|
//~^ ERROR the size for values of type `B` cannot be known at compilation time
|
||||||
|
//~| ERROR the size for values of type `B` cannot be known at compilation time
|
||||||
|
//~| ERROR mismatched types
|
||||||
|
//~| ERROR mismatched types
|
||||||
|
//~| ERROR `main` function not found in crate
|
79
tests/ui/malformed/do-not-ice-on-note_and_explain.stderr
Normal file
79
tests/ui/malformed/do-not-ice-on-note_and_explain.stderr
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
error[E0601]: `main` function not found in crate `do_not_ice_on_note_and_explain`
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:2:37
|
||||||
|
|
|
||||||
|
LL | impl<B>A<B>{fn d(){fn d(){Self(1)}}}
|
||||||
|
| ^ consider adding a `main` function to `$DIR/do-not-ice-on-note_and_explain.rs`
|
||||||
|
|
||||||
|
error[E0277]: the size for values of type `B` cannot be known at compilation time
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:2:32
|
||||||
|
|
|
||||||
|
LL | impl<B>A<B>{fn d(){fn d(){Self(1)}}}
|
||||||
|
| - ---- ^ doesn't have a size known at compile-time
|
||||||
|
| | |
|
||||||
|
| | required by a bound introduced by this call
|
||||||
|
| this type parameter needs to be `Sized`
|
||||||
|
|
|
||||||
|
note: required by a bound in `A`
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:1:10
|
||||||
|
|
|
||||||
|
LL | struct A<B>(B);
|
||||||
|
| ^ required by this bound in `A`
|
||||||
|
|
||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:2:32
|
||||||
|
|
|
||||||
|
LL | impl<B>A<B>{fn d(){fn d(){Self(1)}}}
|
||||||
|
| ---- ^ expected type parameter `B`, found integer
|
||||||
|
| |
|
||||||
|
| arguments to this function are incorrect
|
||||||
|
|
|
||||||
|
= note: expected type parameter `B`
|
||||||
|
found type `{integer}`
|
||||||
|
note: tuple struct defined here
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:1:8
|
||||||
|
|
|
||||||
|
LL | struct A<B>(B);
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:2:27
|
||||||
|
|
|
||||||
|
LL | impl<B>A<B>{fn d(){fn d(){Self(1)}}}
|
||||||
|
| ^^^^^^^ expected `()`, found `A<B>`
|
||||||
|
|
|
||||||
|
= note: expected unit type `()`
|
||||||
|
found struct `A<B>`
|
||||||
|
help: consider using a semicolon here
|
||||||
|
|
|
||||||
|
LL | impl<B>A<B>{fn d(){fn d(){Self(1);}}}
|
||||||
|
| +
|
||||||
|
help: try adding a return type
|
||||||
|
|
|
||||||
|
LL | impl<B>A<B>{fn d(){fn d() -> A<B>{Self(1)}}}
|
||||||
|
| +++++++
|
||||||
|
|
||||||
|
error[E0277]: the size for values of type `B` cannot be known at compilation time
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:2:27
|
||||||
|
|
|
||||||
|
LL | impl<B>A<B>{fn d(){fn d(){Self(1)}}}
|
||||||
|
| - ^^^^^^^ doesn't have a size known at compile-time
|
||||||
|
| |
|
||||||
|
| this type parameter needs to be `Sized`
|
||||||
|
|
|
||||||
|
note: required by a bound in `A`
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:1:10
|
||||||
|
|
|
||||||
|
LL | struct A<B>(B);
|
||||||
|
| ^ required by this bound in `A`
|
||||||
|
help: you could relax the implicit `Sized` bound on `B` if it were used through indirection like `&B` or `Box<B>`
|
||||||
|
--> $DIR/do-not-ice-on-note_and_explain.rs:1:10
|
||||||
|
|
|
||||||
|
LL | struct A<B>(B);
|
||||||
|
| ^ - ...if indirection were used here: `Box<B>`
|
||||||
|
| |
|
||||||
|
| this could be changed to `B: ?Sized`...
|
||||||
|
|
||||||
|
error: aborting due to 5 previous errors
|
||||||
|
|
||||||
|
Some errors have detailed explanations: E0277, E0308, E0601.
|
||||||
|
For more information about an error, try `rustc --explain E0277`.
|
Loading…
x
Reference in New Issue
Block a user