Skip to content

Commit

Permalink
Rollup merge of #92892 - compiler-errors:const-param-env-for-const-bl…
Browse files Browse the repository at this point in the history
…ock, r=fee1-dead

Do not fail evaluation in const blocks

Evaluate const blocks with a const param-env, so we properly check `~const` trait bounds.

Fixes #92713
(I will fix the poor diagnostics in #92713 and #92712 in a separate PR)

cc `@nbdd0121` who wrote the code this PR touches in #89561
  • Loading branch information
matthiaskrgr authored Jan 15, 2022
2 parents 784e4ba + b9a3c32 commit 539175c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let body = self.tcx.hir().body(anon_const.body);

// Create a new function context.
let fcx = FnCtxt::new(self, self.param_env, body.value.hir_id);
let fcx = FnCtxt::new(self, self.param_env.with_const(), body.value.hir_id);
crate::check::GatherLocalsVisitor::new(&fcx).visit_body(body);

let ty = fcx.check_expr_with_expectation(&body.value, expected);
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/consts/const-block-const-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![allow(unused)]
#![feature(const_fn_trait_bound, const_trait_impl, inline_const)]

const fn f<T: ~const Drop>(x: T) {}

struct UnconstDrop;

impl Drop for UnconstDrop {
fn drop(&mut self) {}
}

fn main() {
const {
f(UnconstDrop);
//~^ ERROR the trait bound `UnconstDrop: Drop` is not satisfied
}
}
21 changes: 21 additions & 0 deletions src/test/ui/consts/const-block-const-bound.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0277]: the trait bound `UnconstDrop: Drop` is not satisfied
--> $DIR/const-block-const-bound.rs:14:11
|
LL | f(UnconstDrop);
| - ^^^^^^^^^^^ the trait `Drop` is not implemented for `UnconstDrop`
| |
| required by a bound introduced by this call
|
note: required by a bound in `f`
--> $DIR/const-block-const-bound.rs:4:15
|
LL | const fn f<T: ~const Drop>(x: T) {}
| ^^^^^^^^^^^ required by this bound in `f`
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
|
LL | fn main() where UnconstDrop: Drop {
| +++++++++++++++++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

0 comments on commit 539175c

Please sign in to comment.