Auto merge of #122802 - estebank:unconstrained-generic-const, r=Nadrieril
Provide structured suggestion for unconstrained generic constant ``` error: unconstrained generic constant --> $DIR/const-argument-if-length.rs:18:10 | LL | pad: [u8; is_zst::<T>()], | ^^^^^^^^^^^^^^^^^^^ | help: try adding a `where` bound | LL | pub struct AtLeastByte<T: ?Sized> where [(); is_zst::<T>()]: { | ++++++++++++++++++++++++++ ``` Detect when the constant expression isn't `usize` and suggest casting: ``` error: unconstrained generic constant --> f300.rs:6:10 | 6 | bb::<{!N}>(); | ^^^^ -Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs:3539:36 | help: try adding a `where` bound | 5 | fn b<const N: bool>() where [(); {!N} as usize]: { | ++++++++++++++++++++++++++ ``` Fix #122395.
This commit is contained in:
commit
dda2372cf3
41 changed files with 351 additions and 111 deletions
|
@ -3536,12 +3536,39 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||
let mut err =
|
||||
self.dcx().struct_span_err(span, "unconstrained generic constant");
|
||||
let const_span = self.tcx.def_span(uv.def);
|
||||
|
||||
let const_ty = self.tcx.type_of(uv.def).instantiate(self.tcx, uv.args);
|
||||
let cast = if const_ty != self.tcx.types.usize { " as usize" } else { "" };
|
||||
let msg = "try adding a `where` bound";
|
||||
match self.tcx.sess.source_map().span_to_snippet(const_span) {
|
||||
Ok(snippet) => err.help(format!(
|
||||
"try adding a `where` bound using this expression: `where [(); {snippet}]:`"
|
||||
)),
|
||||
_ => err.help("consider adding a `where` bound using this expression"),
|
||||
};
|
||||
Ok(snippet) => {
|
||||
let code = format!("[(); {snippet}{cast}]:");
|
||||
let def_id = if let ObligationCauseCode::CompareImplItemObligation {
|
||||
trait_item_def_id,
|
||||
..
|
||||
} = obligation.cause.code()
|
||||
{
|
||||
trait_item_def_id.as_local()
|
||||
} else {
|
||||
Some(obligation.cause.body_id)
|
||||
};
|
||||
if let Some(def_id) = def_id
|
||||
&& let Some(generics) = self.tcx.hir().get_generics(def_id)
|
||||
{
|
||||
err.span_suggestion_verbose(
|
||||
generics.tail_span_for_predicate_suggestion(),
|
||||
msg,
|
||||
format!("{} {code}", generics.add_where_or_trailing_comma()),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
} else {
|
||||
err.help(format!("{msg}: where {code}"));
|
||||
};
|
||||
}
|
||||
_ => {
|
||||
err.help(msg);
|
||||
}
|
||||
};
|
||||
Ok(err)
|
||||
}
|
||||
ty::ConstKind::Expr(_) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue