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```
This commit is contained in:
Matthias Krüger 2022-01-12 07:12:06 +01:00 committed by GitHub
commit 6726f1e013
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View file

@ -810,7 +810,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
param_env,
Binder::dummy(TraitPredicate {
trait_ref,
constness: ty::BoundConstness::ConstIfConst,
constness: ty::BoundConstness::NotConst,
polarity: ty::ImplPolarity::Positive,
}),
);
@ -829,6 +829,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
return;
}
Ok(Some(ImplSource::UserDefined(data))) => {
if let hir::Constness::NotConst = tcx.impl_constness(data.impl_def_id) {
self.check_op(ops::FnCallNonConst(None));
return;
}
let callee_name = tcx.item_name(callee);
if let Some(&did) = tcx
.associated_item_def_ids(data.impl_def_id)

View file

@ -0,0 +1,17 @@
#![feature(const_fn_trait_bound)]
#![feature(const_trait_impl)]
pub trait Tr {
#[default_method_body_is_const]
fn a(&self) {}
#[default_method_body_is_const]
fn b(&self) {
().a()
//~^ ERROR calls in constant functions are limited
}
}
impl Tr for () {}
fn main() {}

View file

@ -0,0 +1,9 @@
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> $DIR/default-method-body-is-const-same-trait-ck.rs:10:9
|
LL | ().a()
| ^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0015`.