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

log!("{x}") treats input as a raw string #1188

Closed
Sliman4 opened this issue Jun 2, 2024 · 1 comment · Fixed by #1189
Closed

log!("{x}") treats input as a raw string #1188

Sliman4 opened this issue Jun 2, 2024 · 1 comment · Fixed by #1189

Comments

@Sliman4
Copy link
Contributor

Sliman4 commented Jun 2, 2024

When you write log!("{x}"), it outputs {x} literally instead of the value of variable x because log! is implemented differently if it has only 1 expression:

macro_rules! log {
($arg:expr) => {
$crate::env::log_str($arg.as_ref())
};
($($arg:tt)*) => {
$crate::env::log_str(format!($($arg)*).as_str())
};
}

log!("{x}",) (with a trailing comma) bypasses this, but I think it would be nice to maintain 100% compatibility with std::fmt.

If a string allocation makes a significant difference for some contract, is there a reason why they can't just use env::log_str directly? This call will also explicitly mark that this log outputs a lot of data and should not be changed to something that allocates, to avoid situations when at some point you add one {} to the log, and gas usage skyrockets for an unknown reason.

@frol
Copy link
Collaborator

frol commented Jun 2, 2024

@Sliman4 The macro was implemented before string interpolation became a thing in Rust (stable). I agree with your reasoning. Would you like to prepare a PR?

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 a pull request may close this issue.

2 participants