Skip to content

Commit 2b7ec4f

Browse files
oli-obkfee1-dead
authored andcommitted
Require explicitly handling the tristate constness
1 parent 03d3d0d commit 2b7ec4f

File tree

10 files changed

+28
-32
lines changed

10 files changed

+28
-32
lines changed

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,6 @@ pub enum ConstnessArg {
317317
Param,
318318
}
319319

320-
impl ConstnessArg {
321-
pub fn is_const(self) -> bool {
322-
matches!(self, ConstnessArg::Required)
323-
}
324-
}
325-
326320
impl fmt::Display for ConstnessArg {
327321
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328322
f.write_str(match self {

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ impl<'tcx> TraitRef<'tcx> {
826826
}
827827

828828
pub fn without_const(mut self, tcx: TyCtxt<'tcx>) -> Self {
829-
if self.constness().is_const() {
829+
if self.constness() != ty::ConstnessArg::Not {
830830
self.substs = tcx.mk_substs(self.substs.iter().map(|arg| match arg.unpack() {
831831
ty::subst::GenericArgKind::Constness(_) => ty::ConstnessArg::Not.into(),
832832
_ => arg,
@@ -837,7 +837,7 @@ impl<'tcx> TraitRef<'tcx> {
837837
}
838838

839839
pub fn with_const(mut self, tcx: TyCtxt<'tcx>) -> Self {
840-
if !self.constness().is_const() {
840+
if self.constness() != ty::ConstnessArg::Required {
841841
self.substs = tcx.mk_substs(self.substs.iter().map(|arg| match arg.unpack() {
842842
ty::subst::GenericArgKind::Constness(_) => ty::ConstnessArg::Required.into(),
843843
_ => arg,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
489489
self.suggest_borrowing_for_object_cast(&mut err, &root_obligation, *concrete_ty, *obj_ty);
490490
}
491491

492-
if trait_predicate.constness().is_const() {
492+
if trait_predicate.constness() != ty::ConstnessArg::Not {
493493
let non_const_predicate = trait_ref.without_const(self.tcx);
494494
let non_const_obligation = Obligation {
495495
cause: obligation.cause.clone(),

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
957957
) {
958958
// If the predicate is `~const Destruct` in a non-const environment, we don't actually need
959959
// to check anything. We'll short-circuit checking any obligations in confirmation, too.
960-
if !obligation.predicate.constness().is_const() {
960+
if obligation.predicate.constness() == ty::ConstnessArg::Not {
961961
candidates.vec.push(ConstDestructCandidate(None));
962962
return;
963963
}

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11011101
impl_def_id: Option<DefId>,
11021102
) -> Result<ImplSourceConstDestructData<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
11031103
// `~const Destruct` in a non-const environment is always trivially true, since our type is `Drop`
1104-
if !obligation.predicate.constness().is_const() {
1104+
if obligation.predicate.constness() == ty::ConstnessArg::Not {
11051105
return Ok(ImplSourceConstDestructData { nested: vec![] });
11061106
}
11071107

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,12 +1191,16 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11911191

11921192
for candidate in candidates {
11931193
// Respect const trait obligations
1194-
if obligation.predicate.constness().is_const() {
1194+
if obligation.predicate.constness() == ty::ConstnessArg::Const {
11951195
match candidate {
11961196
// const impl
11971197
ImplCandidate(def_id) if tcx.constness(def_id) == hir::Constness::Const => {}
11981198
// const param
1199-
ParamCandidate(trait_pred) if trait_pred.constness().is_const() => {}
1199+
ParamCandidate(trait_pred)
1200+
if trait_pred.constness() != ty::ConstnessArg::Not =>
1201+
{
1202+
debug_assert_eq!(trait_pred.constness(), ty::ConstnessArg::Param);
1203+
}
12001204
// auto trait impl
12011205
AutoImplCandidate(..) => {}
12021206
// generator, this will raise error in other places

compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,7 @@ fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
369369
_ if predicate.is_global() => (),
370370
// We allow specializing on explicitly marked traits with no associated
371371
// items.
372-
ty::PredicateKind::Trait(ty::TraitPredicate {
373-
trait_ref,
374-
polarity: _,
375-
}) if !trait_ref.constness().is_const() => { // TODO fix this
372+
ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref, polarity: _ }) => {
376373
if !matches!(
377374
trait_predicate_kind(tcx, predicate),
378375
Some(TraitSpecializationKind::Marker)

src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ LL | NonConstImpl.a();
55
| ^^^ the trait `~const ConstDefaultFn` is not implemented for `NonConstImpl`
66
|
77
note: the trait `ConstDefaultFn` is implemented for `NonConstImpl`, but that implementation is not `const`
8-
--> $DIR/const-default-method-bodies.rs:24:18
8+
--> $DIR/const-default-method-bodies.rs:15:6
99
|
10-
LL | NonConstImpl.a();
11-
| ^^^
10+
LL | impl ConstDefaultFn for NonConstImpl {
11+
| ^^^^^^^^^^^^^^
12+
= help: the trait `ConstDefaultFn` is implemented for `NonConstImpl`
1213

1314
error[E0015]: cannot call non-const fn `<NonConstImpl as ConstDefaultFn>::a` in constant functions
1415
--> $DIR/const-default-method-bodies.rs:24:18

src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-same-trait-ck.stderr

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,11 @@ LL | ().a()
55
| ^^^ the trait `~const Tr` is not implemented for `()`
66
|
77
note: the trait `Tr` is implemented for `()`, but that implementation is not `const`
8-
--> $DIR/default-method-body-is-const-same-trait-ck.rs:8:12
9-
|
10-
LL | ().a()
11-
| ^^^
12-
13-
error[E0015]: cannot call non-const fn `<() as Tr>::a` in constant functions
14-
--> $DIR/default-method-body-is-const-same-trait-ck.rs:8:12
15-
|
16-
LL | ().a()
17-
| ^^^
8+
--> $DIR/default-method-body-is-const-same-trait-ck.rs:14:6
189
|
19-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
10+
LL | impl Tr for () {}
11+
| ^^
12+
= help: the trait `Tr` is implemented for `()`
2013

2114
error: aborting due to 2 previous errors
2215

src/test/ui/rfc-2632-const-trait-impl/impl-with-default-fn-fail.stderr

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ LL | fn req(&self);
55
| -------------- `req` from trait
66
...
77
LL | impl const Tr for u16 {
8-
| ^^^^^^^^^^^^^^^^^^^^^ missing `req` in implementation
8+
| ^^ the trait `Tr` is not implemented for `u16`
9+
|
10+
note: the trait `Tr` is implemented for `u16`, but that implementation is not `const`
11+
--> $DIR/impl-with-default-fn-fail.rs:12:12
12+
|
13+
LL | impl const Tr for u16 {
14+
| ^^
15+
= help: the trait `Tr` is implemented for `u16`
916

1017
error: aborting due to previous error
1118

0 commit comments

Comments
 (0)