Skip to content

Commit 61ed4cb

Browse files
Remove the BoundConstness::NotConst variant
1 parent bfab34a commit 61ed4cb

File tree

7 files changed

+46
-41
lines changed

7 files changed

+46
-41
lines changed

compiler/rustc_hir_analysis/src/bounds.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'tcx> Bounds<'tcx> {
5151
bound_trait_ref: ty::PolyTraitRef<'tcx>,
5252
span: Span,
5353
polarity: ty::PredicatePolarity,
54-
constness: ty::BoundConstness,
54+
constness: Option<ty::BoundConstness>,
5555
predicate_filter: PredicateFilter,
5656
) {
5757
let clause = (
@@ -88,19 +88,20 @@ impl<'tcx> Bounds<'tcx> {
8888
// associated type of `<T as Tr>` and make sure that the effect is compatible.
8989
let compat_val = match (tcx.def_kind(defining_def_id), constness) {
9090
// FIXME(effects): revisit the correctness of this
91-
(_, ty::BoundConstness::Const) => tcx.consts.false_,
91+
(_, Some(ty::BoundConstness::Const)) => tcx.consts.false_,
9292
// body owners that can have trait bounds
93-
(DefKind::Const | DefKind::Fn | DefKind::AssocFn, ty::BoundConstness::ConstIfConst) => {
94-
tcx.expected_host_effect_param_for_body(defining_def_id)
95-
}
93+
(
94+
DefKind::Const | DefKind::Fn | DefKind::AssocFn,
95+
Some(ty::BoundConstness::ConstIfConst),
96+
) => tcx.expected_host_effect_param_for_body(defining_def_id),
9697

97-
(_, ty::BoundConstness::NotConst) => {
98+
(_, None) => {
9899
if !tcx.is_const_trait(bound_trait_ref.def_id()) {
99100
return;
100101
}
101102
tcx.consts.true_
102103
}
103-
(DefKind::Trait, ty::BoundConstness::ConstIfConst) => {
104+
(DefKind::Trait, Some(ty::BoundConstness::ConstIfConst)) => {
104105
// we are in a trait, where `bound_trait_ref` could be:
105106
// (1) a super trait `trait Foo: ~const Bar`.
106107
// - This generates `<Self as Foo>::Effects: TyCompat<<Self as Bar>::Effects>`
@@ -138,7 +139,7 @@ impl<'tcx> Bounds<'tcx> {
138139
return;
139140
}
140141

141-
(DefKind::Impl { of_trait: true }, ty::BoundConstness::ConstIfConst) => {
142+
(DefKind::Impl { of_trait: true }, Some(ty::BoundConstness::ConstIfConst)) => {
142143
// this is a where clause on an impl header.
143144
// push `<T as Tr>::Effects` into the set for the `Min` bound.
144145
let Some(assoc) = tcx.associated_type_for_effects(bound_trait_ref.def_id()) else {
@@ -172,12 +173,12 @@ impl<'tcx> Bounds<'tcx> {
172173
//
173174
// FIXME(effects) this is equality for now, which wouldn't be helpful for a non-const implementor
174175
// that uses a `Bar` that implements `Trait` with `Maybe` effects.
175-
(DefKind::AssocTy, ty::BoundConstness::ConstIfConst) => {
176+
(DefKind::AssocTy, Some(ty::BoundConstness::ConstIfConst)) => {
176177
// FIXME(effects): implement this
177178
return;
178179
}
179180
// probably illegal in this position.
180-
(_, ty::BoundConstness::ConstIfConst) => {
181+
(_, Some(ty::BoundConstness::ConstIfConst)) => {
181182
tcx.dcx().span_delayed_bug(span, "invalid `~const` encountered");
182183
return;
183184
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
171171
hir::GenericBound::Trait(poly_trait_ref) => {
172172
let (constness, polarity) = match poly_trait_ref.modifiers {
173173
hir::TraitBoundModifier::Const => {
174-
(ty::BoundConstness::Const, ty::PredicatePolarity::Positive)
175-
}
176-
hir::TraitBoundModifier::MaybeConst => {
177-
(ty::BoundConstness::ConstIfConst, ty::PredicatePolarity::Positive)
178-
}
179-
hir::TraitBoundModifier::None => {
180-
(ty::BoundConstness::NotConst, ty::PredicatePolarity::Positive)
174+
(Some(ty::BoundConstness::Const), ty::PredicatePolarity::Positive)
181175
}
176+
hir::TraitBoundModifier::MaybeConst => (
177+
Some(ty::BoundConstness::ConstIfConst),
178+
ty::PredicatePolarity::Positive,
179+
),
180+
hir::TraitBoundModifier::None => (None, ty::PredicatePolarity::Positive),
182181
hir::TraitBoundModifier::Negative => {
183-
(ty::BoundConstness::NotConst, ty::PredicatePolarity::Negative)
182+
(None, ty::PredicatePolarity::Negative)
184183
}
185184
hir::TraitBoundModifier::Maybe => continue,
186185
};

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
5151
} = self.lower_poly_trait_ref(
5252
&trait_bound.trait_ref,
5353
trait_bound.span,
54-
ty::BoundConstness::NotConst,
54+
None,
5555
ty::PredicatePolarity::Positive,
5656
dummy_self,
5757
&mut bounds,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
652652
&self,
653653
trait_ref: &hir::TraitRef<'tcx>,
654654
span: Span,
655-
constness: ty::BoundConstness,
655+
constness: Option<ty::BoundConstness>,
656656
polarity: ty::PredicatePolarity,
657657
self_ty: Ty<'tcx>,
658658
bounds: &mut Bounds<'tcx>,
@@ -675,7 +675,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
675675
Some(self_ty),
676676
);
677677

678-
if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness
678+
if let Some(constness) = constness
679679
&& !self.tcx().is_const_trait(trait_def_id)
680680
{
681681
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {

compiler/rustc_middle/src/ty/print/pretty.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
19561956
define_scoped_cx!(self);
19571957

19581958
match constness {
1959-
ty::BoundConstness::NotConst => {}
19601959
ty::BoundConstness::Const => {
19611960
p!("const ");
19621961
}
@@ -2948,7 +2947,10 @@ impl<'tcx> ty::TraitPredicate<'tcx> {
29482947
}
29492948

29502949
#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
2951-
pub struct TraitPredPrintWithBoundConstness<'tcx>(ty::TraitPredicate<'tcx>, ty::BoundConstness);
2950+
pub struct TraitPredPrintWithBoundConstness<'tcx>(
2951+
ty::TraitPredicate<'tcx>,
2952+
Option<ty::BoundConstness>,
2953+
);
29522954

29532955
impl<'tcx> fmt::Debug for TraitPredPrintWithBoundConstness<'tcx> {
29542956
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -2966,7 +2968,7 @@ impl<'tcx> ty::PolyTraitPredicate<'tcx> {
29662968

29672969
fn print_with_bound_constness(
29682970
self,
2969-
constness: ty::BoundConstness,
2971+
constness: Option<ty::BoundConstness>,
29702972
) -> ty::Binder<'tcx, TraitPredPrintWithBoundConstness<'tcx>> {
29712973
self.map_bound(|trait_pred| TraitPredPrintWithBoundConstness(trait_pred, constness))
29722974
}
@@ -3206,7 +3208,9 @@ define_print_and_forward_display! {
32063208

32073209
TraitPredPrintWithBoundConstness<'tcx> {
32083210
p!(print(self.0.trait_ref.self_ty()), ": ");
3209-
p!(pretty_print_bound_constness(self.1));
3211+
if let Some(constness) = self.1 {
3212+
p!(pretty_print_bound_constness(constness));
3213+
}
32103214
if let ty::PredicatePolarity::Negative = self.0.polarity {
32113215
p!("!");
32123216
}

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
290290
}
291291

292292
if tcx.is_lang_item(leaf_trait_ref.def_id(), LangItem::Drop)
293-
&& matches!(predicate_constness, ty::BoundConstness::ConstIfConst | ty::BoundConstness::Const)
293+
&& matches!(predicate_constness, Some(ty::BoundConstness::ConstIfConst | ty::BoundConstness::Const))
294294
{
295295
err.note("`~const Drop` was renamed to `~const Destruct`");
296296
err.note("See <https://github.com/rust-lang/rust/pull/94901> for more details");
@@ -2192,27 +2192,29 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21922192
&self,
21932193
trait_predicate: ty::PolyTraitPredicate<'tcx>,
21942194
message: Option<String>,
2195-
predicate_constness: ty::BoundConstness,
2195+
predicate_constness: Option<ty::BoundConstness>,
21962196
append_const_msg: Option<AppendConstMessage>,
21972197
post_message: String,
21982198
) -> String {
21992199
message
22002200
.and_then(|cannot_do_this| {
22012201
match (predicate_constness, append_const_msg) {
22022202
// do nothing if predicate is not const
2203-
(ty::BoundConstness::NotConst, _) => Some(cannot_do_this),
2203+
(None, _) => Some(cannot_do_this),
22042204
// suggested using default post message
22052205
(
2206-
ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst,
2206+
Some(ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst),
22072207
Some(AppendConstMessage::Default),
22082208
) => Some(format!("{cannot_do_this} in const contexts")),
22092209
// overridden post message
22102210
(
2211-
ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst,
2211+
Some(ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst),
22122212
Some(AppendConstMessage::Custom(custom_msg, _)),
22132213
) => Some(format!("{cannot_do_this}{custom_msg}")),
22142214
// fallback to generic message
2215-
(ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst, None) => None,
2215+
(Some(ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst), None) => {
2216+
None
2217+
}
22162218
}
22172219
})
22182220
.unwrap_or_else(|| {
@@ -2358,26 +2360,27 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
23582360
p: ty::PolyTraitPredicate<'tcx>,
23592361
leaf: ty::PolyTraitPredicate<'tcx>,
23602362
span: Span,
2361-
) -> (ty::PolyTraitPredicate<'tcx>, ty::PolyTraitPredicate<'tcx>, ty::BoundConstness) {
2363+
) -> (ty::PolyTraitPredicate<'tcx>, ty::PolyTraitPredicate<'tcx>, Option<ty::BoundConstness>)
2364+
{
23622365
let trait_ref = p.to_poly_trait_ref();
23632366
if !self.tcx.is_lang_item(trait_ref.def_id(), LangItem::EffectsCompat) {
2364-
return (p, leaf, ty::BoundConstness::NotConst);
2367+
return (p, leaf, None);
23652368
}
23662369

23672370
let Some(ty::Alias(ty::AliasTyKind::Projection, projection)) =
23682371
trait_ref.self_ty().no_bound_vars().map(Ty::kind)
23692372
else {
2370-
return (p, leaf, ty::BoundConstness::NotConst);
2373+
return (p, leaf, None);
23712374
};
23722375

23732376
let constness = trait_ref.skip_binder().args.const_at(1);
23742377

23752378
let constness = if constness == self.tcx.consts.true_ || constness.is_ct_infer() {
2376-
ty::BoundConstness::NotConst
2379+
None
23772380
} else if constness == self.tcx.consts.false_ {
2378-
ty::BoundConstness::Const
2381+
Some(ty::BoundConstness::Const)
23792382
} else if matches!(constness.kind(), ty::ConstKind::Param(_)) {
2380-
ty::BoundConstness::ConstIfConst
2383+
Some(ty::BoundConstness::ConstIfConst)
23812384
} else {
23822385
self.dcx().span_bug(span, format!("Unknown constness argument: {constness:?}"));
23832386
};

compiler/rustc_type_ir/src/predicate.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ pub struct CoercePredicate<I: Interner> {
726726
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
727727
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext, TyEncodable, TyDecodable))]
728728
pub enum BoundConstness {
729-
/// `Type: Trait`
730-
NotConst,
731729
/// `Type: const Trait`
730+
///
731+
/// A bound is required to be unconditionally const, even in a runtime function.
732732
Const,
733733
/// `Type: ~const Trait`
734734
///
@@ -739,7 +739,6 @@ pub enum BoundConstness {
739739
impl BoundConstness {
740740
pub fn as_str(self) -> &'static str {
741741
match self {
742-
Self::NotConst => "",
743742
Self::Const => "const",
744743
Self::ConstIfConst => "~const",
745744
}
@@ -749,7 +748,6 @@ impl BoundConstness {
749748
impl fmt::Display for BoundConstness {
750749
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
751750
match self {
752-
Self::NotConst => f.write_str("normal"),
753751
Self::Const => f.write_str("const"),
754752
Self::ConstIfConst => f.write_str("~const"),
755753
}

0 commit comments

Comments
 (0)