Remove the BoundConstness::NotConst variant
This commit is contained in:
parent
bfab34af4c
commit
61ed4cb5b4
7 changed files with 46 additions and 41 deletions
|
@ -51,7 +51,7 @@ impl<'tcx> Bounds<'tcx> {
|
||||||
bound_trait_ref: ty::PolyTraitRef<'tcx>,
|
bound_trait_ref: ty::PolyTraitRef<'tcx>,
|
||||||
span: Span,
|
span: Span,
|
||||||
polarity: ty::PredicatePolarity,
|
polarity: ty::PredicatePolarity,
|
||||||
constness: ty::BoundConstness,
|
constness: Option<ty::BoundConstness>,
|
||||||
predicate_filter: PredicateFilter,
|
predicate_filter: PredicateFilter,
|
||||||
) {
|
) {
|
||||||
let clause = (
|
let clause = (
|
||||||
|
@ -88,19 +88,20 @@ impl<'tcx> Bounds<'tcx> {
|
||||||
// associated type of `<T as Tr>` and make sure that the effect is compatible.
|
// associated type of `<T as Tr>` and make sure that the effect is compatible.
|
||||||
let compat_val = match (tcx.def_kind(defining_def_id), constness) {
|
let compat_val = match (tcx.def_kind(defining_def_id), constness) {
|
||||||
// FIXME(effects): revisit the correctness of this
|
// FIXME(effects): revisit the correctness of this
|
||||||
(_, ty::BoundConstness::Const) => tcx.consts.false_,
|
(_, Some(ty::BoundConstness::Const)) => tcx.consts.false_,
|
||||||
// body owners that can have trait bounds
|
// body owners that can have trait bounds
|
||||||
(DefKind::Const | DefKind::Fn | DefKind::AssocFn, ty::BoundConstness::ConstIfConst) => {
|
(
|
||||||
tcx.expected_host_effect_param_for_body(defining_def_id)
|
DefKind::Const | DefKind::Fn | DefKind::AssocFn,
|
||||||
}
|
Some(ty::BoundConstness::ConstIfConst),
|
||||||
|
) => tcx.expected_host_effect_param_for_body(defining_def_id),
|
||||||
|
|
||||||
(_, ty::BoundConstness::NotConst) => {
|
(_, None) => {
|
||||||
if !tcx.is_const_trait(bound_trait_ref.def_id()) {
|
if !tcx.is_const_trait(bound_trait_ref.def_id()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
tcx.consts.true_
|
tcx.consts.true_
|
||||||
}
|
}
|
||||||
(DefKind::Trait, ty::BoundConstness::ConstIfConst) => {
|
(DefKind::Trait, Some(ty::BoundConstness::ConstIfConst)) => {
|
||||||
// we are in a trait, where `bound_trait_ref` could be:
|
// we are in a trait, where `bound_trait_ref` could be:
|
||||||
// (1) a super trait `trait Foo: ~const Bar`.
|
// (1) a super trait `trait Foo: ~const Bar`.
|
||||||
// - This generates `<Self as Foo>::Effects: TyCompat<<Self as Bar>::Effects>`
|
// - This generates `<Self as Foo>::Effects: TyCompat<<Self as Bar>::Effects>`
|
||||||
|
@ -138,7 +139,7 @@ impl<'tcx> Bounds<'tcx> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
(DefKind::Impl { of_trait: true }, ty::BoundConstness::ConstIfConst) => {
|
(DefKind::Impl { of_trait: true }, Some(ty::BoundConstness::ConstIfConst)) => {
|
||||||
// this is a where clause on an impl header.
|
// this is a where clause on an impl header.
|
||||||
// push `<T as Tr>::Effects` into the set for the `Min` bound.
|
// push `<T as Tr>::Effects` into the set for the `Min` bound.
|
||||||
let Some(assoc) = tcx.associated_type_for_effects(bound_trait_ref.def_id()) else {
|
let Some(assoc) = tcx.associated_type_for_effects(bound_trait_ref.def_id()) else {
|
||||||
|
@ -172,12 +173,12 @@ impl<'tcx> Bounds<'tcx> {
|
||||||
//
|
//
|
||||||
// FIXME(effects) this is equality for now, which wouldn't be helpful for a non-const implementor
|
// FIXME(effects) this is equality for now, which wouldn't be helpful for a non-const implementor
|
||||||
// that uses a `Bar` that implements `Trait` with `Maybe` effects.
|
// that uses a `Bar` that implements `Trait` with `Maybe` effects.
|
||||||
(DefKind::AssocTy, ty::BoundConstness::ConstIfConst) => {
|
(DefKind::AssocTy, Some(ty::BoundConstness::ConstIfConst)) => {
|
||||||
// FIXME(effects): implement this
|
// FIXME(effects): implement this
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// probably illegal in this position.
|
// probably illegal in this position.
|
||||||
(_, ty::BoundConstness::ConstIfConst) => {
|
(_, Some(ty::BoundConstness::ConstIfConst)) => {
|
||||||
tcx.dcx().span_delayed_bug(span, "invalid `~const` encountered");
|
tcx.dcx().span_delayed_bug(span, "invalid `~const` encountered");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,16 +171,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
||||||
hir::GenericBound::Trait(poly_trait_ref) => {
|
hir::GenericBound::Trait(poly_trait_ref) => {
|
||||||
let (constness, polarity) = match poly_trait_ref.modifiers {
|
let (constness, polarity) = match poly_trait_ref.modifiers {
|
||||||
hir::TraitBoundModifier::Const => {
|
hir::TraitBoundModifier::Const => {
|
||||||
(ty::BoundConstness::Const, ty::PredicatePolarity::Positive)
|
(Some(ty::BoundConstness::Const), ty::PredicatePolarity::Positive)
|
||||||
}
|
|
||||||
hir::TraitBoundModifier::MaybeConst => {
|
|
||||||
(ty::BoundConstness::ConstIfConst, ty::PredicatePolarity::Positive)
|
|
||||||
}
|
|
||||||
hir::TraitBoundModifier::None => {
|
|
||||||
(ty::BoundConstness::NotConst, ty::PredicatePolarity::Positive)
|
|
||||||
}
|
}
|
||||||
|
hir::TraitBoundModifier::MaybeConst => (
|
||||||
|
Some(ty::BoundConstness::ConstIfConst),
|
||||||
|
ty::PredicatePolarity::Positive,
|
||||||
|
),
|
||||||
|
hir::TraitBoundModifier::None => (None, ty::PredicatePolarity::Positive),
|
||||||
hir::TraitBoundModifier::Negative => {
|
hir::TraitBoundModifier::Negative => {
|
||||||
(ty::BoundConstness::NotConst, ty::PredicatePolarity::Negative)
|
(None, ty::PredicatePolarity::Negative)
|
||||||
}
|
}
|
||||||
hir::TraitBoundModifier::Maybe => continue,
|
hir::TraitBoundModifier::Maybe => continue,
|
||||||
};
|
};
|
||||||
|
|
|
@ -51,7 +51,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
||||||
} = self.lower_poly_trait_ref(
|
} = self.lower_poly_trait_ref(
|
||||||
&trait_bound.trait_ref,
|
&trait_bound.trait_ref,
|
||||||
trait_bound.span,
|
trait_bound.span,
|
||||||
ty::BoundConstness::NotConst,
|
None,
|
||||||
ty::PredicatePolarity::Positive,
|
ty::PredicatePolarity::Positive,
|
||||||
dummy_self,
|
dummy_self,
|
||||||
&mut bounds,
|
&mut bounds,
|
||||||
|
|
|
@ -652,7 +652,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
||||||
&self,
|
&self,
|
||||||
trait_ref: &hir::TraitRef<'tcx>,
|
trait_ref: &hir::TraitRef<'tcx>,
|
||||||
span: Span,
|
span: Span,
|
||||||
constness: ty::BoundConstness,
|
constness: Option<ty::BoundConstness>,
|
||||||
polarity: ty::PredicatePolarity,
|
polarity: ty::PredicatePolarity,
|
||||||
self_ty: Ty<'tcx>,
|
self_ty: Ty<'tcx>,
|
||||||
bounds: &mut Bounds<'tcx>,
|
bounds: &mut Bounds<'tcx>,
|
||||||
|
@ -675,7 +675,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
|
||||||
Some(self_ty),
|
Some(self_ty),
|
||||||
);
|
);
|
||||||
|
|
||||||
if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness
|
if let Some(constness) = constness
|
||||||
&& !self.tcx().is_const_trait(trait_def_id)
|
&& !self.tcx().is_const_trait(trait_def_id)
|
||||||
{
|
{
|
||||||
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
|
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
|
||||||
|
|
|
@ -1956,7 +1956,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
|
||||||
define_scoped_cx!(self);
|
define_scoped_cx!(self);
|
||||||
|
|
||||||
match constness {
|
match constness {
|
||||||
ty::BoundConstness::NotConst => {}
|
|
||||||
ty::BoundConstness::Const => {
|
ty::BoundConstness::Const => {
|
||||||
p!("const ");
|
p!("const ");
|
||||||
}
|
}
|
||||||
|
@ -2948,7 +2947,10 @@ impl<'tcx> ty::TraitPredicate<'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
|
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
|
||||||
pub struct TraitPredPrintWithBoundConstness<'tcx>(ty::TraitPredicate<'tcx>, ty::BoundConstness);
|
pub struct TraitPredPrintWithBoundConstness<'tcx>(
|
||||||
|
ty::TraitPredicate<'tcx>,
|
||||||
|
Option<ty::BoundConstness>,
|
||||||
|
);
|
||||||
|
|
||||||
impl<'tcx> fmt::Debug for TraitPredPrintWithBoundConstness<'tcx> {
|
impl<'tcx> fmt::Debug for TraitPredPrintWithBoundConstness<'tcx> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -2966,7 +2968,7 @@ impl<'tcx> ty::PolyTraitPredicate<'tcx> {
|
||||||
|
|
||||||
fn print_with_bound_constness(
|
fn print_with_bound_constness(
|
||||||
self,
|
self,
|
||||||
constness: ty::BoundConstness,
|
constness: Option<ty::BoundConstness>,
|
||||||
) -> ty::Binder<'tcx, TraitPredPrintWithBoundConstness<'tcx>> {
|
) -> ty::Binder<'tcx, TraitPredPrintWithBoundConstness<'tcx>> {
|
||||||
self.map_bound(|trait_pred| TraitPredPrintWithBoundConstness(trait_pred, constness))
|
self.map_bound(|trait_pred| TraitPredPrintWithBoundConstness(trait_pred, constness))
|
||||||
}
|
}
|
||||||
|
@ -3206,7 +3208,9 @@ define_print_and_forward_display! {
|
||||||
|
|
||||||
TraitPredPrintWithBoundConstness<'tcx> {
|
TraitPredPrintWithBoundConstness<'tcx> {
|
||||||
p!(print(self.0.trait_ref.self_ty()), ": ");
|
p!(print(self.0.trait_ref.self_ty()), ": ");
|
||||||
p!(pretty_print_bound_constness(self.1));
|
if let Some(constness) = self.1 {
|
||||||
|
p!(pretty_print_bound_constness(constness));
|
||||||
|
}
|
||||||
if let ty::PredicatePolarity::Negative = self.0.polarity {
|
if let ty::PredicatePolarity::Negative = self.0.polarity {
|
||||||
p!("!");
|
p!("!");
|
||||||
}
|
}
|
||||||
|
|
|
@ -290,7 +290,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if tcx.is_lang_item(leaf_trait_ref.def_id(), LangItem::Drop)
|
if tcx.is_lang_item(leaf_trait_ref.def_id(), LangItem::Drop)
|
||||||
&& matches!(predicate_constness, ty::BoundConstness::ConstIfConst | ty::BoundConstness::Const)
|
&& matches!(predicate_constness, Some(ty::BoundConstness::ConstIfConst | ty::BoundConstness::Const))
|
||||||
{
|
{
|
||||||
err.note("`~const Drop` was renamed to `~const Destruct`");
|
err.note("`~const Drop` was renamed to `~const Destruct`");
|
||||||
err.note("See <https://github.com/rust-lang/rust/pull/94901> for more details");
|
err.note("See <https://github.com/rust-lang/rust/pull/94901> for more details");
|
||||||
|
@ -2192,7 +2192,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
||||||
&self,
|
&self,
|
||||||
trait_predicate: ty::PolyTraitPredicate<'tcx>,
|
trait_predicate: ty::PolyTraitPredicate<'tcx>,
|
||||||
message: Option<String>,
|
message: Option<String>,
|
||||||
predicate_constness: ty::BoundConstness,
|
predicate_constness: Option<ty::BoundConstness>,
|
||||||
append_const_msg: Option<AppendConstMessage>,
|
append_const_msg: Option<AppendConstMessage>,
|
||||||
post_message: String,
|
post_message: String,
|
||||||
) -> String {
|
) -> String {
|
||||||
|
@ -2200,19 +2200,21 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
||||||
.and_then(|cannot_do_this| {
|
.and_then(|cannot_do_this| {
|
||||||
match (predicate_constness, append_const_msg) {
|
match (predicate_constness, append_const_msg) {
|
||||||
// do nothing if predicate is not const
|
// do nothing if predicate is not const
|
||||||
(ty::BoundConstness::NotConst, _) => Some(cannot_do_this),
|
(None, _) => Some(cannot_do_this),
|
||||||
// suggested using default post message
|
// suggested using default post message
|
||||||
(
|
(
|
||||||
ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst,
|
Some(ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst),
|
||||||
Some(AppendConstMessage::Default),
|
Some(AppendConstMessage::Default),
|
||||||
) => Some(format!("{cannot_do_this} in const contexts")),
|
) => Some(format!("{cannot_do_this} in const contexts")),
|
||||||
// overridden post message
|
// overridden post message
|
||||||
(
|
(
|
||||||
ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst,
|
Some(ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst),
|
||||||
Some(AppendConstMessage::Custom(custom_msg, _)),
|
Some(AppendConstMessage::Custom(custom_msg, _)),
|
||||||
) => Some(format!("{cannot_do_this}{custom_msg}")),
|
) => Some(format!("{cannot_do_this}{custom_msg}")),
|
||||||
// fallback to generic message
|
// fallback to generic message
|
||||||
(ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst, None) => None,
|
(Some(ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst), None) => {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
|
@ -2358,26 +2360,27 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
|
||||||
p: ty::PolyTraitPredicate<'tcx>,
|
p: ty::PolyTraitPredicate<'tcx>,
|
||||||
leaf: ty::PolyTraitPredicate<'tcx>,
|
leaf: ty::PolyTraitPredicate<'tcx>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> (ty::PolyTraitPredicate<'tcx>, ty::PolyTraitPredicate<'tcx>, ty::BoundConstness) {
|
) -> (ty::PolyTraitPredicate<'tcx>, ty::PolyTraitPredicate<'tcx>, Option<ty::BoundConstness>)
|
||||||
|
{
|
||||||
let trait_ref = p.to_poly_trait_ref();
|
let trait_ref = p.to_poly_trait_ref();
|
||||||
if !self.tcx.is_lang_item(trait_ref.def_id(), LangItem::EffectsCompat) {
|
if !self.tcx.is_lang_item(trait_ref.def_id(), LangItem::EffectsCompat) {
|
||||||
return (p, leaf, ty::BoundConstness::NotConst);
|
return (p, leaf, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some(ty::Alias(ty::AliasTyKind::Projection, projection)) =
|
let Some(ty::Alias(ty::AliasTyKind::Projection, projection)) =
|
||||||
trait_ref.self_ty().no_bound_vars().map(Ty::kind)
|
trait_ref.self_ty().no_bound_vars().map(Ty::kind)
|
||||||
else {
|
else {
|
||||||
return (p, leaf, ty::BoundConstness::NotConst);
|
return (p, leaf, None);
|
||||||
};
|
};
|
||||||
|
|
||||||
let constness = trait_ref.skip_binder().args.const_at(1);
|
let constness = trait_ref.skip_binder().args.const_at(1);
|
||||||
|
|
||||||
let constness = if constness == self.tcx.consts.true_ || constness.is_ct_infer() {
|
let constness = if constness == self.tcx.consts.true_ || constness.is_ct_infer() {
|
||||||
ty::BoundConstness::NotConst
|
None
|
||||||
} else if constness == self.tcx.consts.false_ {
|
} else if constness == self.tcx.consts.false_ {
|
||||||
ty::BoundConstness::Const
|
Some(ty::BoundConstness::Const)
|
||||||
} else if matches!(constness.kind(), ty::ConstKind::Param(_)) {
|
} else if matches!(constness.kind(), ty::ConstKind::Param(_)) {
|
||||||
ty::BoundConstness::ConstIfConst
|
Some(ty::BoundConstness::ConstIfConst)
|
||||||
} else {
|
} else {
|
||||||
self.dcx().span_bug(span, format!("Unknown constness argument: {constness:?}"));
|
self.dcx().span_bug(span, format!("Unknown constness argument: {constness:?}"));
|
||||||
};
|
};
|
||||||
|
|
|
@ -726,9 +726,9 @@ pub struct CoercePredicate<I: Interner> {
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext, TyEncodable, TyDecodable))]
|
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext, TyEncodable, TyDecodable))]
|
||||||
pub enum BoundConstness {
|
pub enum BoundConstness {
|
||||||
/// `Type: Trait`
|
|
||||||
NotConst,
|
|
||||||
/// `Type: const Trait`
|
/// `Type: const Trait`
|
||||||
|
///
|
||||||
|
/// A bound is required to be unconditionally const, even in a runtime function.
|
||||||
Const,
|
Const,
|
||||||
/// `Type: ~const Trait`
|
/// `Type: ~const Trait`
|
||||||
///
|
///
|
||||||
|
@ -739,7 +739,6 @@ pub enum BoundConstness {
|
||||||
impl BoundConstness {
|
impl BoundConstness {
|
||||||
pub fn as_str(self) -> &'static str {
|
pub fn as_str(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Self::NotConst => "",
|
|
||||||
Self::Const => "const",
|
Self::Const => "const",
|
||||||
Self::ConstIfConst => "~const",
|
Self::ConstIfConst => "~const",
|
||||||
}
|
}
|
||||||
|
@ -749,7 +748,6 @@ impl BoundConstness {
|
||||||
impl fmt::Display for BoundConstness {
|
impl fmt::Display for BoundConstness {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::NotConst => f.write_str("normal"),
|
|
||||||
Self::Const => f.write_str("const"),
|
Self::Const => f.write_str("const"),
|
||||||
Self::ConstIfConst => f.write_str("~const"),
|
Self::ConstIfConst => f.write_str("~const"),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue