Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const fn has no result expression #33031

Closed
nagisa opened this issue Apr 16, 2016 · 5 comments
Closed

const fn has no result expression #33031

nagisa opened this issue Apr 16, 2016 · 5 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@nagisa
Copy link
Member

nagisa commented Apr 16, 2016

#![feature(const_fn)]
pub const fn test() {}
const X: () = test();
fn main() {}

ICEs with

error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'const fn has no result expression', src/libcore/option.rs:700
stack backtrace:
   1:     0x7fc09db94d37 - sys::backtrace::tracing::imp::write::h8f11a6299019b37aN7u
   2:     0x7fc09dba498b - panicking::default_hook::_$u7b$$u7b$closure$u7d$$u7d$::closure.46063
   3:     0x7fc09dba4505 - panicking::default_hook::he79d5441312473c9vjA
   4:     0x7fc09db62cef - sys_common::unwind::begin_unwind_inner::h850ac67cf2e6431duZt
   5:     0x7fc09db659f8 - sys_common::unwind::begin_unwind_fmt::h7f9772c177da56ceAYt
   6:     0x7fc09db925e1 - rust_begin_unwind
   7:     0x7fc09dbba94f - panicking::panic_fmt::h7b8691efcf0ee41102L
   8:     0x7fc09dbe0c64 - option::expect_failed::h2f7b609e15d08c701RO
   9:     0x7fc098f2d6f2 - eval::eval_const_expr_partial::hd768fd0c71d5d67ewOa
  10:     0x7fc09cf75fd7 - consts::CheckCrateVisitor<'a, 'tcx>::global_expr::h3fa9a0801caa760cgsa
  11:     0x7fc09cf7dd76 - consts::CheckCrateVisitor<'a, 'tcx>.Visitor<'v>::visit_item::h4e6b6664f41ad6ddvDa
  12:     0x7fc09cf824d7 - consts::check_crate::hc2396fa9b810de44Hlb
  13:     0x7fc09e0ad934 - driver::phase_3_run_analysis_passes::_$u7b$$u7b$closure$u7d$$u7d$::closure.30847
  14:     0x7fc09e0ab601 - ty::context::TyCtxt<'tcx>::create_and_enter::h12752561671132360966
  15:     0x7fc09e0a6852 - driver::phase_3_run_analysis_passes::h7514814909072594316
  16:     0x7fc09e0776d1 - driver::compile_input::h7e38a782c304f126Uca
  17:     0x7fc09e063a80 - run_compiler::ha6ad3fc70e3bb8f8IKc
  18:     0x7fc09e060d3b - sys_common::unwind::try::try_fn::h9022798910832087097
  19:     0x7fc09db9256b - __rust_try
  20:     0x7fc09db924fd - sys_common::unwind::inner_try::h42271e72fc2229cdwWt
  21:     0x7fc09e0615ff - boxed::F.FnBox<A>::call_box::h6627780591262321388
  22:     0x7fc09dba2214 - sys::thread::Thread::new::thread_start::h23b56e13da45f381dhz
  23:     0x7fc09692b423 - start_thread
  24:     0x7fc09d80ccbc - clone
  25:                0x0 - <unknown>

The result expression should probably default to unit?

@nagisa nagisa added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ A-const-fn labels Apr 16, 2016
@Aatch
Copy link
Contributor

Aatch commented Apr 17, 2016

Default to unit, but generate a warning. A const fn with no result is probably a mistake.

@arielb1
Copy link
Contributor

arielb1 commented Apr 17, 2016

@Aatch

It's not a particularly easy mistake to make, given that your function would have to be empty and return unit. More than that, in the future we may have asserts in const fns, so unit-returning const fns would be useful.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 18, 2016

We can't fix this yet, because we can't create struct and tuple ConstVals, we can only use existing constructor expressions. I had a fix for it once, but it was a breaking change, I have an idea how to make sure it isn't a breaking change.

@jagourq
Copy link
Contributor

jagourq commented Jun 3, 2016

I get the same error if I run:

#![feature(const_fn)]
const fn foo(x: i32) -> i32 {
    return x;
}
fn main() {
    let x = foo(2);
    println!("{}", x);
}

Although, if I omit the semicolon in return x; I get:

error: internal compiler error: ../src/librustc_trans/consts.rs:1006: bad constant expression type in consts::const_expr

@Mark-Simulacrum
Copy link
Member

This no longer ICEs.

@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

7 participants