ivandardi / RustbotPython

A Discord bot for the Community Rust Language server, written in Python.
MIT License
16 stars 9 forks source link

Comments in ?eval command delete everything after it #16

Open kangalio opened 4 years ago

kangalio commented 4 years ago

Bug description

I thought this was a bug, but apparently it's a feature...?

https://github.com/ivandardi/RustbotPython/blob/25ecd0eaf663afc51307288f0072d18ed1688841/bot/cogs/playground.py#L76-L83

It deliberately deletes everything after the occurence of //. I imagine this is a very blunt workaround to prevent comments on the last line from shadowing the closing brackets });}. However, this is much better done by just putting the user input into their own lines:

'fn main(){println!("{:?}",{\n' + code.source[:end_idx] + "\n});}'

Why fix this

This bug peculiarity just cost me about 10 minutes of headaches because I couldn't figure out why this snippet of code complained about lifetimes:

let strings = vec!["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"];
let k = 2;

strings
  .windows(k)
  .rev() // prefer first match instead of last
  .max_by_key(|window| window.iter().map(|string| string.len()).sum::<usize>())
  .unwrap()
  .join("")

Screenshot_20201010_163540

Also

On that note, might as well get the formatting correct once and for all. You could replace

'fn main(){println!("{:?}",{' + code.source[:end_idx] + "});}"'

with

'''
fn main() {
    println!("{:?}", {
        %s
    });
}
'''.format("\n\t\t".join(code.source.splitlines()))

That way, error messages will have nicely formatted code (right) instead of the ugly minified code (left)