Skip to content

Commit 094d95b

Browse files
committed
fix calling const fns with an empty body
fixes #33031
1 parent cf45924 commit 094d95b

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/librustc_const_eval/eval.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,6 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
801801
} else {
802802
signal!(e, NonConstPath)
803803
};
804-
let result = result.as_ref().expect("const fn has no result expression");
805804
assert_eq!(decl.inputs.len(), args.len());
806805

807806
let mut call_args = NodeMap();
@@ -818,7 +817,11 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
818817
assert!(old.is_none());
819818
}
820819
debug!("const call({:?})", call_args);
821-
eval_const_expr_partial(tcx, &result, ty_hint, Some(&call_args))?
820+
if let &Some(ref result) = result {
821+
eval_const_expr_partial(tcx, &result, ty_hint, Some(&call_args))?
822+
} else {
823+
Tuple(None, Vec::new())
824+
}
822825
},
823826
hir::ExprLit(ref lit) => match lit_to_const(&lit.node, tcx, ety, lit.span) {
824827
Ok(val) => val,

src/test/run-pass/const-fn.rs

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const fn generic_arr<T: Copy>(t: [T; 1]) -> T {
3333
t[0]
3434
}
3535

36+
pub const fn test() {}
37+
const X: () = test();
38+
3639
const fn f(_: ()) -> usize { 1 }
3740

3841
const SUM: u32 = add(44, 22);
@@ -50,4 +53,5 @@ fn main() {
5053
let _: [&'static str; generic(1)] = ["hi"];
5154
let _: [&'static str; generic_arr([1])] = ["hi"];
5255
let _: [&'static str; f({})] = ["hi"];
56+
let _ = [0; g(5).field];
5357
}

0 commit comments

Comments
 (0)