rust-lang / discord-mods-bot

discord moderation bot
MIT License
71 stars 30 forks source link

fix(playground): eval command failing with trailing single line comments #96

Closed insomnimus closed 3 years ago

insomnimus commented 3 years ago

If a code passed to ?eval contains trailing single-line comments, the matching } in the code is commented out.

For example:

?eval `2 + 2 // returns 4`

Will not work as expected.

I've added a single newline(\n) in playground.rs Where the expression is wrapped in a block to print out so the } is not commented out.

technetos commented 3 years ago

Thanks for bringing this to our attention!!

The problem boils down to the following

The generated code is this:

fn main(){ println!("{:?}",{ 2 + 2 // returns 4 }); }

By adding the \n after the end of the user provided expression it transforms the expression posted to play.rlo into:

fn main(){ println!("{:?}",{ 2 + 2 // returns 4
}); }

effectively moving the remaining closing braces to the next line, preventing them from being commented out.

Instead of just breaking up the end, could we break up the whole expression into separate lines?

fn main(){
println!("{:?}",{
2 + 2 // returns 4
}); }

@tinaun Do you have any thoughts about this?

khionu commented 3 years ago

The difference between a single new line and "more" formatting is how it looks in the Gist, should we share one with the user. So, it's inconsequential, atm.