Skip to content

Commit 103c3a3

Browse files
authored
Rollup merge of #93358 - compiler-errors:is-not-const, r=fee1-dead
Add note suggesting that predicate may be satisfied, but is not `const` Not sure if we should be printing this in addition to, or perhaps _instead_ of the help message: ``` help: the trait `~const Add` is not implemented for `NonConstAdd` ``` Also added `ParamEnv::is_const` and `PolyTraitPredicate::is_const_if_const` and, in a separate commit, used those in other places instead of `== hir::Constness::Const`, etc. r? ````@fee1-dead````
2 parents 6e2593d + c6de4d5 commit 103c3a3

File tree

14 files changed

+84
-12
lines changed

14 files changed

+84
-12
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::interpret::{
77
};
88

99
use rustc_errors::ErrorReported;
10-
use rustc_hir as hir;
1110
use rustc_hir::def::DefKind;
1211
use rustc_middle::mir;
1312
use rustc_middle::mir::interpret::ErrorHandled;
@@ -216,7 +215,7 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
216215
tcx: TyCtxt<'tcx>,
217216
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
218217
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
219-
assert!(key.param_env.constness() == hir::Constness::Const);
218+
assert!(key.param_env.is_const());
220219
// see comment in eval_to_allocation_raw_provider for what we're doing here
221220
if key.param_env.reveal() == Reveal::All {
222221
let mut key = key;
@@ -251,7 +250,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
251250
tcx: TyCtxt<'tcx>,
252251
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
253252
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
254-
assert!(key.param_env.constness() == hir::Constness::Const);
253+
assert!(key.param_env.is_const());
255254
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
256255
// reporting the same error twice here. To resolve this, we check whether we can evaluate the
257256
// constant in the more restrictive `Reveal::UserFacing`, which most likely already was

compiler/rustc_lint/src/traits.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,14 @@ declare_lint_pass!(
8686

8787
impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
8888
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
89-
use rustc_middle::ty;
9089
use rustc_middle::ty::PredicateKind::*;
9190

9291
let predicates = cx.tcx.explicit_predicates_of(item.def_id);
9392
for &(predicate, span) in predicates.predicates {
9493
let Trait(trait_predicate) = predicate.kind().skip_binder() else {
9594
continue
9695
};
97-
if trait_predicate.constness == ty::BoundConstness::ConstIfConst {
96+
if trait_predicate.is_const_if_const() {
9897
// `~const Drop` definitely have meanings so avoid linting here.
9998
continue;
10099
}

compiler/rustc_middle/src/ty/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,11 @@ impl<'tcx> TraitPredicate<'tcx> {
784784
pub fn self_ty(self) -> Ty<'tcx> {
785785
self.trait_ref.self_ty()
786786
}
787+
788+
#[inline]
789+
pub fn is_const_if_const(self) -> bool {
790+
self.constness == BoundConstness::ConstIfConst
791+
}
787792
}
788793

789794
impl<'tcx> PolyTraitPredicate<'tcx> {
@@ -803,6 +808,11 @@ impl<'tcx> PolyTraitPredicate<'tcx> {
803808
p
804809
});
805810
}
811+
812+
#[inline]
813+
pub fn is_const_if_const(self) -> bool {
814+
self.skip_binder().is_const_if_const()
815+
}
806816
}
807817

808818
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
@@ -1388,6 +1398,11 @@ impl<'tcx> ParamEnv<'tcx> {
13881398
self.packed.tag().constness
13891399
}
13901400

1401+
#[inline]
1402+
pub fn is_const(self) -> bool {
1403+
self.packed.tag().constness == hir::Constness::Const
1404+
}
1405+
13911406
/// Construct a trait environment with no where-clauses in scope
13921407
/// where the values of all `impl Trait` and other hidden types
13931408
/// are revealed. This is suitable for monomorphized, post-typeck
@@ -1503,6 +1518,7 @@ impl<'tcx> PolyTraitRef<'tcx> {
15031518
polarity: ty::ImplPolarity::Positive,
15041519
})
15051520
}
1521+
15061522
#[inline]
15071523
pub fn without_const(self) -> PolyTraitPredicate<'tcx> {
15081524
self.with_constness(BoundConstness::NotConst)

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

+22
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,28 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
439439
} else {
440440
err.span_label(span, explanation);
441441
}
442+
443+
if trait_predicate.is_const_if_const() && obligation.param_env.is_const() {
444+
let non_const_predicate = trait_ref.without_const();
445+
let non_const_obligation = Obligation {
446+
cause: obligation.cause.clone(),
447+
param_env: obligation.param_env.without_const(),
448+
predicate: non_const_predicate.to_predicate(tcx),
449+
recursion_depth: obligation.recursion_depth,
450+
};
451+
if self.predicate_may_hold(&non_const_obligation) {
452+
err.span_note(
453+
span,
454+
&format!(
455+
"the trait `{}` is implemented for `{}`, \
456+
but that implementation is not `const`",
457+
non_const_predicate.print_modifiers_and_trait_path(),
458+
trait_ref.skip_binder().self_ty(),
459+
),
460+
);
461+
}
462+
}
463+
442464
if let Some((msg, span)) = type_def {
443465
err.span_label(span, &msg);
444466
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
305305
} else if lang_items.unsize_trait() == Some(def_id) {
306306
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
307307
} else if lang_items.drop_trait() == Some(def_id)
308-
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::ConstIfConst
308+
&& obligation.predicate.is_const_if_const()
309309
{
310310
self.assemble_const_drop_candidates(obligation, &mut candidates);
311311
} else {

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
7272
// CheckPredicate(&A: Super)
7373
// CheckPredicate(A: ~const Super) // <- still const env, failure
7474
// ```
75-
if obligation.param_env.constness() == Constness::Const
76-
&& obligation.predicate.skip_binder().constness == ty::BoundConstness::NotConst
77-
{
75+
if obligation.param_env.is_const() && !obligation.predicate.is_const_if_const() {
7876
new_obligation = TraitObligation {
7977
cause: obligation.cause.clone(),
8078
param_env: obligation.param_env.without_const(),

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -1173,9 +1173,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11731173
ImplCandidate(def_id)
11741174
if tcx.impl_constness(def_id) == hir::Constness::Const => {}
11751175
// const param
1176-
ParamCandidate(trait_pred)
1177-
if trait_pred.skip_binder().constness
1178-
== ty::BoundConstness::ConstIfConst => {}
1176+
ParamCandidate(trait_pred) if trait_pred.is_const_if_const() => {}
11791177
// auto trait impl
11801178
AutoImplCandidate(..) => {}
11811179
// generator, this will raise error in other places

src/test/ui/intrinsics/const-eval-select-bad.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LL | const_eval_select((), || {}, || {});
77
| required by a bound introduced by this call
88
|
99
= help: the trait `~const FnOnce<()>` is not implemented for `[closure@$DIR/const-eval-select-bad.rs:6:27: 6:32]`
10+
note: the trait `FnOnce<()>` is implemented for `[closure@$DIR/const-eval-select-bad.rs:6:27: 6:32]`, but that implementation is not `const`
11+
--> $DIR/const-eval-select-bad.rs:6:27
12+
|
13+
LL | const_eval_select((), || {}, || {});
14+
| ^^^^^
1015
= note: wrap the `[closure@$DIR/const-eval-select-bad.rs:6:27: 6:32]` in a closure with no arguments: `|| { /* code */ }`
1116
note: required by a bound in `const_eval_select`
1217
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL

src/test/ui/rfc-2632-const-trait-impl/assoc-type.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | type Bar = NonConstAdd;
55
| ^^^^^^^^^^^ no implementation for `NonConstAdd + NonConstAdd`
66
|
77
= help: the trait `~const Add` is not implemented for `NonConstAdd`
8+
note: the trait `Add` is implemented for `NonConstAdd`, but that implementation is not `const`
9+
--> $DIR/assoc-type.rs:18:16
10+
|
11+
LL | type Bar = NonConstAdd;
12+
| ^^^^^^^^^^^
813
note: required by a bound in `Foo::Bar`
914
--> $DIR/assoc-type.rs:14:15
1015
|

src/test/ui/rfc-2632-const-trait-impl/call-generic-method-nonconst.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LL | pub const EQ: bool = equals_self(&S);
77
| required by a bound introduced by this call
88
|
99
= help: the trait `~const PartialEq` is not implemented for `S`
10+
note: the trait `PartialEq` is implemented for `S`, but that implementation is not `const`
11+
--> $DIR/call-generic-method-nonconst.rs:19:34
12+
|
13+
LL | pub const EQ: bool = equals_self(&S);
14+
| ^^
1015
note: required by a bound in `equals_self`
1116
--> $DIR/call-generic-method-nonconst.rs:12:25
1217
|

src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.precise.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ LL | const _: () = check($exp);
2828
LL | ConstImplWithDropGlue(NonTrivialDrop),
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ within `ConstImplWithDropGlue`, the trait `~const Drop` is not implemented for `NonTrivialDrop`
3030
|
31+
note: the trait `Drop` is implemented for `NonTrivialDrop`, but that implementation is not `const`
32+
--> $DIR/const-drop-fail.rs:46:5
33+
|
34+
LL | ConstImplWithDropGlue(NonTrivialDrop),
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3136
note: required because it appears within the type `ConstImplWithDropGlue`
3237
--> $DIR/const-drop-fail.rs:17:8
3338
|

src/test/ui/rfc-2632-const-trait-impl/const-drop-fail.stock.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ LL | const _: () = check($exp);
2828
LL | ConstImplWithDropGlue(NonTrivialDrop),
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ within `ConstImplWithDropGlue`, the trait `~const Drop` is not implemented for `NonTrivialDrop`
3030
|
31+
note: the trait `Drop` is implemented for `NonTrivialDrop`, but that implementation is not `const`
32+
--> $DIR/const-drop-fail.rs:46:5
33+
|
34+
LL | ConstImplWithDropGlue(NonTrivialDrop),
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3136
note: required because it appears within the type `ConstImplWithDropGlue`
3237
--> $DIR/const-drop-fail.rs:17:8
3338
|

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

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: the trait bound `(): ~const Tr` is not satisfied
44
LL | foo::<()>();
55
| ^^ the trait `~const Tr` is not implemented for `()`
66
|
7+
note: the trait `Tr` is implemented for `()`, but that implementation is not `const`
8+
--> $DIR/default-method-body-is-const-body-checking.rs:12:15
9+
|
10+
LL | foo::<()>();
11+
| ^^
712
note: required by a bound in `foo`
813
--> $DIR/default-method-body-is-const-body-checking.rs:7:28
914
|

src/test/ui/rfc-2632-const-trait-impl/trait-where-clause.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: the trait bound `T: ~const Bar` is not satisfied
44
LL | T::b();
55
| ^^^^ the trait `~const Bar` is not implemented for `T`
66
|
7+
note: the trait `Bar` is implemented for `T`, but that implementation is not `const`
8+
--> $DIR/trait-where-clause.rs:14:5
9+
|
10+
LL | T::b();
11+
| ^^^^
712
note: required by a bound in `Foo::b`
813
--> $DIR/trait-where-clause.rs:8:24
914
|
@@ -20,6 +25,11 @@ error[E0277]: the trait bound `T: ~const Bar` is not satisfied
2025
LL | T::c::<T>();
2126
| ^^^^^^^^^ the trait `~const Bar` is not implemented for `T`
2227
|
28+
note: the trait `Bar` is implemented for `T`, but that implementation is not `const`
29+
--> $DIR/trait-where-clause.rs:16:5
30+
|
31+
LL | T::c::<T>();
32+
| ^^^^^^^^^
2333
note: required by a bound in `Foo::c`
2434
--> $DIR/trait-where-clause.rs:9:13
2535
|

0 commit comments

Comments
 (0)