Skip to content

Commit

Permalink
Rollup merge of rust-lang#92432 - fee1-dead:constck-impl-constness, r…
Browse files Browse the repository at this point in the history
…=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``
  • Loading branch information
matthiaskrgr authored Jan 11, 2022
2 parents 2ab347b + bf5130b commit 9a6dd30
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 9a6dd30

Please sign in to comment.