From 448779d899f5fee34f81cef39605314ca52f041f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 18 Jun 2015 15:27:23 +0200 Subject: [PATCH 1/4] Add E0019 error explanation --- src/librustc/diagnostics.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 0857fb3258eec..495b0df2fdda9 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -279,6 +279,41 @@ 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. Bad example: + +``` +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 +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! +} +``` +"##, + 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. @@ -952,7 +987,6 @@ static mut BAR: Option> = None; register_diagnostics! { E0016, E0017, - E0019, E0022, E0038, E0109, From e6696dc63254094d473e16bc735a9e3909b4a288 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 18 Jun 2015 16:37:04 +0200 Subject: [PATCH 2/4] Remove unneeded indentation --- src/librustc/diagnostics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 495b0df2fdda9..a7fd2bea04425 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -296,7 +296,7 @@ impl Test { fn main() { const FOO: Test = Test::V1; - + const A: i32 = FOO.test(); // You can't call Test::func() here ! } ``` @@ -307,7 +307,7 @@ 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! } From 402ac99ad4ad1f91f65a7b8a5a7008adc14bd172 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 19 Jun 2015 12:12:22 +0200 Subject: [PATCH 3/4] Replace "Bad example" by a better sentence --- src/librustc/diagnostics.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index a7fd2bea04425..fe2ab543e0518 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -281,7 +281,8 @@ 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. Bad example: +because the expression's value must be known at compile-time. Example of +erroneous code:: ``` enum Test { From 465b0dbaf53ec464becb5eaccb757bcb047967c2 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 19 Jun 2015 12:33:10 +0200 Subject: [PATCH 4/4] Add const fn feature (thanks to @pnkfelix for signaling it to me!) --- src/librustc/diagnostics.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index fe2ab543e0518..7b44ac1466a1a 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -282,7 +282,7 @@ 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:: +erroneous code: ``` enum Test { @@ -313,6 +313,28 @@ fn main() { 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##"