E5004

E5004 — A call of main, or main used as a value. Only the runtime starts main. A main that takes a Root makes the capabilities of the program, so a second start would give them to code that did not get them.

What the compiler reports

Error[E5004] at {{.*}}a_program_cannot_call_main.vx:[[@LINE+1]]:10: A program cannot call `main` or use it as a value
Error[E5004] at {{.*}}a_program_cannot_call_main.vx:[[@LINE+1]]:16: A program cannot call `main` or use it as a value
2 error(s) emitted

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Only the runtime starts `main`. This `main` takes a `Root`, so each start makes a new `Root`.
// `helper` gets no capability, and a call of `main` would give it every capability of the
// program. `main` as a value is the same error.

fn apply(f : fn() -> i32) -> i32 {
  return f();
}

fn helper() -> i32 {
  return main();
}

fn pass_main() -> i32 {
  return apply(main);
}

fn main(root : Root) -> i32 {
  println!("main has a Root");
  return helper() + pass_main();
}

From tests/frontend/fail/a_program_cannot_call_main.vx, which asserts this diagnostic on every commit.

Related