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

Macro code variables shadow the retryable function parameters #5

Merged
merged 1 commit into from
Jun 14, 2024

Conversation

haxorcize
Copy link
Contributor

@haxorcize haxorcize commented Jun 13, 2024

I was working on this function:

[retry(DEFAULT_BACKOFF_CONFIG, logged_retry_on_error)]
async fn extract_soap_date_range(
    ctx: Arc<Mutex<StreamRunContext>>,
    soap_object_type: Arc<String>,
    properties: Arc<Vec<String>>,
    token: Arc<String>,
    subdomain: Arc<String>,
    start: DateTime<Utc>,
    end: DateTime<Utc>,
) -> Result<(

When I ran into issues with my start variable not being the type I expected.

Thankfully I am familiar with the macro code, and there are 3 variables defined by the #block is inserted into the macro:

let start = tokio::time::Instant::now();
let backoff_max = #config.backoff_max.unwrap_or(std::time::Duration::MAX);
let mut attempt = 0;

So what happens is that start from the macro is overriding my start parameter.

My solution was simple - to prefix these 3 variables with __ so the chances you'll ever have a function parameter called __start would be very low. If you want to be extra safe you might want to prefix even further, because if you do have the macro shadowing your function parameter - the compilation error is superrr weird and most folks wouldn't understand why it's happening, so if we can avoid it even further it's better.

@Brendan-Blanchard
Copy link
Owner

This works great! Since the macro only has three variables, we could parse the inner function parameters and raise a clear error if any of them match the __* ones we're using, but I think this is fine for now. Like you said, the risk of collision is fairly low, and someone prefixing with __* is likely to have an idea what's going on under the hood, much like you. Thanks for contributing back!

@Brendan-Blanchard Brendan-Blanchard merged commit 1d43465 into Brendan-Blanchard:master Jun 14, 2024
4 checks passed
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

Successfully merging this pull request may close these issues.

2 participants