Skip to content

Add E0019 error explanation #26396

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

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,64 @@ println!("{}", Y);
```
"##,

E0019: r##"
A function call isn't allowed in the const's initialization expression
because the expression's value must be known at compile-time. Example of
erroneous code:

```
enum Test {
V1
}

impl Test {
fn test(&self) -> i32 {
12
}
}

fn main() {
const FOO: Test = Test::V1;

const A: i32 = FOO.test(); // You can't call Test::func() here !
}
```

Remember: you can't use a function call inside a const's initialization
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add an additional paragraph about the const fn feature, which is currently feature-gated and thus only available in Nightly (but there are other error explanations like E0152 that reference feature-gated things). The sole purpose of const fn is to be a function call that breaks the rule you wrote here. :)

Here is a demonstration of const fn:

https://play.rust-lang.org/?gist=449fb78958b96eca7528&version=nightly

expression! However, you can totally use it elsewhere you want:

```
fn main() {
const FOO: Test = Test::V1;

FOO.func(); // here is good
let x = FOO.func(); // or even here!
}
```

It's important to note that it is possible to use const fn feature in
Nightly. You can use them like this:

```
#![feature(const_fn)]

const fn foo() -> i32 { 3 }

enum Test { V1, V2(i32) }
impl Test {
const fn foo(&self) -> i32 { 4 }
}

const A: i32 = foo(); // we can initialize const with a function!
const B: i32 = Test::V1.foo(); // and even with a method!
const C: i32 = Test::V2(5).foo();

pub fn main() {
println!("A: {} B: {} C: {}", A, B, C);
}
```
"##,

E0020: r##"
This error indicates that an attempt was made to divide by zero (or take the
remainder of a zero divisor) in a static or constant expression.
Expand Down Expand Up @@ -952,7 +1010,6 @@ static mut BAR: Option<Vec<i32>> = None;
register_diagnostics! {
E0016,
E0017,
E0019,
E0022,
E0038,
E0109,
Expand Down