Skip to content

Commit 6726f1e

Browse files
authored
Rollup merge of #92432 - fee1-dead:constck-impl-constness, r=oli-obk
Error when selected impl is not const in constck Catches bad things when checking a `default_method_body_is_const` body, such as: ```rust self.map(/* .. */).is_sorted(); ``` When `Map` does not yet have a `const` `impl` for `Iterator`. r? ```@oli-obk```
2 parents 677f8f0 + bf5130b commit 6726f1e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
810810
param_env,
811811
Binder::dummy(TraitPredicate {
812812
trait_ref,
813-
constness: ty::BoundConstness::ConstIfConst,
813+
constness: ty::BoundConstness::NotConst,
814814
polarity: ty::ImplPolarity::Positive,
815815
}),
816816
);
@@ -829,6 +829,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
829829
return;
830830
}
831831
Ok(Some(ImplSource::UserDefined(data))) => {
832+
if let hir::Constness::NotConst = tcx.impl_constness(data.impl_def_id) {
833+
self.check_op(ops::FnCallNonConst(None));
834+
return;
835+
}
832836
let callee_name = tcx.item_name(callee);
833837
if let Some(&did) = tcx
834838
.associated_item_def_ids(data.impl_def_id)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(const_fn_trait_bound)]
2+
#![feature(const_trait_impl)]
3+
4+
pub trait Tr {
5+
#[default_method_body_is_const]
6+
fn a(&self) {}
7+
8+
#[default_method_body_is_const]
9+
fn b(&self) {
10+
().a()
11+
//~^ ERROR calls in constant functions are limited
12+
}
13+
}
14+
15+
impl Tr for () {}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2+
--> $DIR/default-method-body-is-const-same-trait-ck.rs:10:9
3+
|
4+
LL | ().a()
5+
| ^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)