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

Inconsistent name transformation of pattern matched variables eg: if let Err(...) = ... #230

Closed
gxtaillon opened this issue Jul 4, 2021 · 5 comments

Comments

@gxtaillon
Copy link

Hello,

I can work around this issue, but a fix may improve UX for new users.

When attempting to pattern match a Result<>, the compiler complains about the declared variable not existing.

error[E0425]: cannot find value `e` in this scope
   --> src/bin/x.rs:y:z
    |
123 |         .body(auto!(ywrite_min!(String, "{{> pages/query }}"))))
    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |               |
    |               not found in this scope
    |               in this macro invocation
    | 
   ::: /home/xyz/.cargo/registry/src/github.com-1ecc6299db9ec823/yarte_derive-0.15.4/src/lib.rs:464:1
    |
464 | pub fn ywrite_min(i: TokenStream) -> TokenStream {
    | ------------------------------------------------ in this expansion of procedural macro `ywrite_min!`

Eg:

--------------v
{{#if let Err(e) = feedback }}
    <div class="error-message">{{ format!("{}", e) }}</div>
{{/if}}
------------------------------------------------^

When looking at the output using cargo-expand (cargo install cargo-expand && cargo expand), the declaration has a suffix but the use does not :
image

Putting the generated suffix in the source template does fix the error.

System versions:

yarte 0.15.4
rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-unknown-linux-gnu
release: 1.53.0
LLVM version: 12.0.1

Best,

@zzau13
Copy link
Owner

zzau13 commented Jul 4, 2021 via email

@zzau13
Copy link
Owner

zzau13 commented Jul 11, 2021

@gxtaillon Did this solve the problem? Any questions? Close?

@gxtaillon
Copy link
Author

I'm not sure where I'd put an inline function. Do you have an handlebar example of what you're describing?

@zzau13
Copy link
Owner

zzau13 commented Jul 11, 2021

Like,

#[inline]
fn format<D: std::fmt::Display>(e: D) -> String {
  format!("{}", e)
}

In template

{{ format(bar) }}

Remember import format function in the current module.

@zzau13
Copy link
Owner

zzau13 commented Jul 18, 2021

The body of macros is not transpiled. You can use a function with the inline attribute. There is a bug #211 related to the Span of proc_macro2. If you want transpilation in the body of the macros you can create an issue with the implementation proposal on yarte_lexer.

@zzau13 zzau13 closed this as completed Jul 18, 2021
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

2 participants