Skip to content

Commit bbf4127

Browse files
BoxyUwUcompiler-errors
authored andcommitted
Require that const param tys implement ConstParamTy
1 parent 642c92e commit bbf4127

File tree

2 files changed

+19
-77
lines changed

2 files changed

+19
-77
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -829,83 +829,20 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
829829
let ty = tcx.type_of(param.def_id).subst_identity();
830830

831831
if tcx.features().adt_const_params {
832-
if let Some(non_structural_match_ty) =
833-
traits::search_for_adt_const_param_violation(param.span, tcx, ty)
834-
{
835-
// We use the same error code in both branches, because this is really the same
836-
// issue: we just special-case the message for type parameters to make it
837-
// clearer.
838-
match non_structural_match_ty.kind() {
839-
ty::Param(_) => {
840-
// Const parameters may not have type parameters as their types,
841-
// because we cannot be sure that the type parameter derives `PartialEq`
842-
// and `Eq` (just implementing them is not enough for `structural_match`).
843-
struct_span_err!(
844-
tcx.sess,
845-
hir_ty.span,
846-
E0741,
847-
"`{ty}` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be \
848-
used as the type of a const parameter",
849-
)
850-
.span_label(
851-
hir_ty.span,
852-
format!("`{ty}` may not derive both `PartialEq` and `Eq`"),
853-
)
854-
.note(
855-
"it is not currently possible to use a type parameter as the type of a \
856-
const parameter",
857-
)
858-
.emit();
859-
}
860-
ty::Float(_) => {
861-
struct_span_err!(
862-
tcx.sess,
863-
hir_ty.span,
864-
E0741,
865-
"`{ty}` is forbidden as the type of a const generic parameter",
866-
)
867-
.note("floats do not derive `Eq` or `Ord`, which are required for const parameters")
868-
.emit();
869-
}
870-
ty::FnPtr(_) => {
871-
struct_span_err!(
872-
tcx.sess,
873-
hir_ty.span,
874-
E0741,
875-
"using function pointers as const generic parameters is forbidden",
876-
)
877-
.emit();
878-
}
879-
ty::RawPtr(_) => {
880-
struct_span_err!(
881-
tcx.sess,
882-
hir_ty.span,
883-
E0741,
884-
"using raw pointers as const generic parameters is forbidden",
885-
)
886-
.emit();
887-
}
888-
_ => {
889-
let mut diag = struct_span_err!(
890-
tcx.sess,
891-
hir_ty.span,
892-
E0741,
893-
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
894-
the type of a const parameter",
895-
non_structural_match_ty,
896-
);
897-
898-
if ty == non_structural_match_ty {
899-
diag.span_label(
900-
hir_ty.span,
901-
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),
902-
);
903-
}
904-
905-
diag.emit();
906-
}
907-
}
908-
}
832+
enter_wf_checking_ctxt(tcx, hir_ty.span, param.def_id, |wfcx| {
833+
let trait_def_id =
834+
tcx.require_lang_item(LangItem::ConstParamTy, Some(hir_ty.span));
835+
wfcx.register_bound(
836+
ObligationCause::new(
837+
hir_ty.span,
838+
param.def_id,
839+
ObligationCauseCode::WellFormed(Some(hir_ty.span)),
840+
),
841+
wfcx.param_env,
842+
ty,
843+
trait_def_id,
844+
);
845+
});
909846
} else {
910847
let err_ty_str;
911848
let mut is_ptr = true;

library/core/src/mem/transmutability.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::marker::ConstParamTy;
2+
13
/// Are values of a type transmutable into values of another type?
24
///
35
/// This trait is implemented on-the-fly by the compiler for types `Src` and `Self` when the bits of
@@ -33,6 +35,9 @@ pub struct Assume {
3335
pub validity: bool,
3436
}
3537

38+
#[unstable(feature = "transmutability", issue = "99571")]
39+
impl ConstParamTy for Assume {}
40+
3641
impl Assume {
3742
/// Do not assume that *you* have ensured any safety properties are met.
3843
#[unstable(feature = "transmutability", issue = "99571")]

0 commit comments

Comments
 (0)