You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When having a function that returns a static reference, you can return borrows to direct structs such as A(5) but not const fns which return the same struct such as A::new(5). It's kind of tricky to explain, the example is easier to understand.
Example
#[derive(Copy,Clone)]pubstructA(i32);implA{pubconstfnnew(i:i32) -> A{A(i)}}// comment to make it compilefnnot_good() -> &'staticA{&A::new(5)// <- this should work since A::new is const fn}fngood1() -> &'staticA{&A(5)}fngood2() -> &'staticA{staticTHIS_DOES_WORK:&'staticA = &A::new(5);THIS_DOES_WORK}fnmain(){}
error[E0515]: cannot return reference to temporary value
--> src/main.rs:13:5
|
13 | &A::new(5)
| ^---------
| ||
| |temporary value created here
| returns a reference to data owned by the current function
There is a borrow error when there shouldn't be: this error is perfectly valid when A::new is not const fn, but if A::new is const fn, it shouldn't output an error.
The text was updated successfully, but these errors were encountered:
const fn calls are deliberately not promoted to statics. Please refer to rust-lang/const-eval#19 for details on why that is. Closing as expected behavior.
When having a function that returns a static reference, you can return borrows to direct structs such as
A(5)
but notconst fn
s which return the same struct such asA::new(5)
. It's kind of tricky to explain, the example is easier to understand.Example
playground
Current Behavior (1.46.0 Stable)
There is a borrow error when there shouldn't be: this error is perfectly valid when
A::new
is not const fn, but ifA::new
is const fn, it shouldn't output an error.The text was updated successfully, but these errors were encountered: