Skip to content

Commit efda681

Browse files
committed
Allow evaluating trivial drop glue in constants
1 parent 1dc4e41 commit efda681

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/librustc_mir/const_eval.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -391,19 +391,22 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
391391
ret: Option<mir::BasicBlock>,
392392
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
393393
debug!("eval_fn_call: {:?}", instance);
394-
// Execution might have wandered off into other crates, so we cannot to a stability-
395-
// sensitive check here. But we can at least rule out functions that are not const
396-
// at all.
397-
if !ecx.tcx.is_const_fn_raw(instance.def_id()) {
398-
// Some functions we support even if they are non-const -- but avoid testing
399-
// that for const fn! We certainly do *not* want to actually call the fn
400-
// though, so be sure we return here.
401-
return if ecx.hook_fn(instance, args, dest)? {
402-
ecx.goto_block(ret)?; // fully evaluated and done
403-
Ok(None)
404-
} else {
405-
err!(MachineError(format!("calling non-const function `{}`", instance)))
406-
};
394+
// Only check non-glue functions
395+
if let ty::InstanceDef::Item(def_id) = instance.def {
396+
// Execution might have wandered off into other crates, so we cannot to a stability-
397+
// sensitive check here. But we can at least rule out functions that are not const
398+
// at all.
399+
if !ecx.tcx.is_const_fn_raw(def_id) {
400+
// Some functions we support even if they are non-const -- but avoid testing
401+
// that for const fn! We certainly do *not* want to actually call the fn
402+
// though, so be sure we return here.
403+
return if ecx.hook_fn(instance, args, dest)? {
404+
ecx.goto_block(ret)?; // fully evaluated and done
405+
Ok(None)
406+
} else {
407+
err!(MachineError(format!("calling non-const function `{}`", instance)))
408+
};
409+
}
407410
}
408411
// This is a const fn. Call it.
409412
Ok(Some(match ecx.load_mir(instance.def) {

src/test/ui/consts/drop_none.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile-pass
2+
#![allow(dead_code)]
3+
struct A;
4+
impl Drop for A {
5+
fn drop(&mut self) {}
6+
}
7+
8+
const FOO: Option<A> = None;
9+
10+
const BAR: () = (FOO, ()).1;
11+
12+
13+
fn main() {}

0 commit comments

Comments
 (0)