Skip to content

Comments in ?eval command delete everything after it #16

@kangalio

Description

@kangalio

Bug description

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

async def eval(self, ctx: commands.Context, *, arg):
"""Evaluates Rust code and debug prints the results. Exactly equal to https://play.rust-lang.org/"""
(mode, code) = self.parse_args(arg)
comment_index = code.source.find("//")
end_idx = comment_index if comment_index != -1 else len(code.source)
await self.send_playground(
ctx, mode, 'fn main(){println!("{:?}",{' + code.source[:end_idx] + "});}"
)

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions