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

rt_format! returns error which refers on temprorary data #6

Open
MikailBag opened this issue Jun 2, 2019 · 0 comments
Open

rt_format! returns error which refers on temprorary data #6

MikailBag opened this issue Jun 2, 2019 · 0 comments

Comments

@MikailBag
Copy link

Hello!
I'm using your library like this:

let res = rt_format!(file_tpl, id);
match res {
//...
}

and I'm getting following error (with -Z external-macro-backtrace):

error[E0716]: temporary value dropped while borrowed
  --> <::runtime_fmt::macros::rt_format_args macros>:22:44
   |
1  | /    (
2  | |    @ [ $ spec : expr ] [ $ ( $ args : tt ) * ] $ name : tt = $ e : expr , $ (
3  | |    $ rest : tt ) * ) => {
4  | |    rt_format_args ! (
...  |
19 | /    rt_format_args ! (
20 | |    @ [ $ spec ] [ $ ( $ args ) * $ crate :: Param :: normal ( & $ e ) , ] ) } ; (
   | |___________________________________________________________________________- in this macro invocation (#4)
21 |      @ [ $ spec : expr ] [ $ ( $ args : tt ) * ] ) => {
22 | |    $ crate :: FormatBuf :: new ( & $ spec , & [ $ ( $ args ) * ] ) } ; (
   | |                                               ^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
23 | |    $ spec : expr , $ ( $ rest : tt ) * ) => {
24 | |    rt_format_args ! ( @ [ $ spec ] [  ] $ ( $ rest ) * ) } ; ( $ spec : expr ) =>
   | |    -----------------------------------------------------
   | |    |
   | |    in this macro invocation (#3)
25 | |    { $ crate :: FormatBuf :: new ( & $ spec , & [  ] ) } ;
   | |                                                          -
   | |                                                          |
   | |                                                          in this expansion of `rt_format_args!` (#2)
   | |__________________________________________________________in this expansion of `rt_format_args!` (#3)
   |                                                            in this expansion of `rt_format_args!` (#4)
   | 
  ::: tt/src/cfg.rs:77:51
   |
77 |                      let res = rt_format!(file_tpl, id);
   |                                ------------------------- temporary value is freed at the end of this statement
   |                                |
   |                                in this macro invocation (#1)
78 |                      match res {
   |                            --- borrow later used here
   | 
  ::: <::runtime_fmt::macros::rt_format macros>:1:1
   |
1  |    / ( $ ( $ rest : tt ) * ) => {
2  |    | rt_format_args ! ( $ ( $ rest ) * ) . map (
   |    | -----------------------------------
   |    | |
   |    | in this macro invocation (#2)
3  |    | | x | x . with ( :: std :: fmt :: format ) ) }
   |    |______________________________________________- in this expansion of `rt_format!` (#1)
   |
   = note: consider using a `let` binding to create a longer lived value

Some additional points:

  • explicit reborrow doesn't fix error
  • but transforming possible error into e.g. String (via rt_format!(...).map_err(|e| e.to_string())) fixes
    So, AFAIU, during invokation of macro, temporary string is created, and lifetime of error is bound to that temporary.
@MikailBag MikailBag changed the title rt_format! doesn't work rt_format! returns error which refers on temprorary data Jun 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant