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

fixes )};{( and other fun #27

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ once_cell = "1.13"
humantime = "2.1.0"
rand = "0.8.5"
chrono = "0.4.26"
syn = { version = "2.0.60", features = ["full"] }
quote = "1.0.36"
15 changes: 12 additions & 3 deletions src/commands/playground/misc_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ pub async fn miri(
code: poise::CodeBlock,
) -> Result<(), Error> {
ctx.say(stub_message(ctx)).await?;

let code = &maybe_wrap(&code.code, ResultHandling::Discard);
let code = &maybe_wrapped(
&code.code,
ResultHandling::Discard,
ctx.prefix().contains("sweat"),
ctx.prefix().contains("owo"),
);
let (flags, flag_parse_errors) = parse_flags(flags);

let mut result: PlayResult = ctx
Expand Down Expand Up @@ -142,7 +146,12 @@ pub async fn clippy(
// dead_code: https://github.com/kangalioo/rustbot/issues/44
// let_unit_value: silence warning about `let _ = { ... }` wrapper that swallows return val
"#![allow(dead_code, clippy::let_unit_value)] {}",
maybe_wrap(&code.code, ResultHandling::Discard)
maybe_wrapped(
&code.code,
ResultHandling::Discard,
ctx.prefix().contains("sweat"),
false,
)
);
let (flags, flag_parse_errors) = parse_flags(flags);

Expand Down
7 changes: 6 additions & 1 deletion src/commands/playground/play_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ async fn play_or_eval(
) -> Result<(), Error> {
ctx.say(stub_message(ctx)).await?;

let code = maybe_wrap(&code.code, result_handling);
let code = maybe_wrapped(
&code.code,
result_handling,
ctx.prefix().contains("sweat"),
ctx.prefix().contains("owo"),
);
let (mut flags, flag_parse_errors) = parse_flags(flags);

if force_warnings {
Expand Down
76 changes: 60 additions & 16 deletions src/commands/playground/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,69 @@ pub fn hoise_crate_attributes(code: &str, after_crate_attrs: &str, after_code: &
/// Utility used by the commands to wrap the given code in a `fn main` if not already wrapped.
/// To check, whether a wrap was done, check if the return type is Cow::Borrowed vs Cow::Owned
/// If a wrap was done, also hoists crate attributes to the top so they keep working
pub fn maybe_wrap(code: &str, result_handling: ResultHandling) -> Cow<'_, str> {
if code.contains("fn main") || code.contains("#![no_main]") {
return Cow::Borrowed(code);
pub fn maybe_wrap(code: &str, result_handling: ResultHandling) -> Cow<str> {
maybe_wrapped(code, result_handling, false, false)
}

pub fn maybe_wrapped(
code: &str,
result_handling: ResultHandling,
unsf: bool,
pretty: bool,
) -> Cow<str> {
use quote::quote;
use syn::{parse::Parse, *};

struct Inline {
attrs: Vec<Attribute>,
stmts: Vec<Stmt>,
}

// fn main boilerplate
let after_crate_attrs = match result_handling {
ResultHandling::None => "fn main() {\n",
ResultHandling::Discard => "fn main() { let _ = {\n",
ResultHandling::Print => "fn main() { println!(\"{:?}\", {\n",
};
impl Parse for Inline {
fn parse(input: parse::ParseStream) -> Result<Self> {
let attrs = Attribute::parse_inner(input)?;
let stmts = Block::parse_within(input)?;
for stmt in &stmts {
if let Stmt::Item(Item::Fn(ItemFn { sig, .. })) = stmt {
if sig.ident.to_string() == "main" && sig.inputs.len() == 0 {
return Err(input.error("main"));
}
}
}
Ok(Self { attrs, stmts })
}
}

// fn main boilerplate counterpart
let after_code = match result_handling {
ResultHandling::None => "}",
ResultHandling::Discard => "}; }",
ResultHandling::Print => "}); }",
let Ok(Inline { attrs, mut stmts }) = parse_str::<Inline>(code) else {
return Cow::Borrowed(code);
};

Cow::Owned(hoise_crate_attributes(code, after_crate_attrs, after_code))
if unsf {
stmts = vec![Stmt::Expr(
Expr::Unsafe(ExprUnsafe {
attrs: vec![],
unsafe_token: syn::token::Unsafe::default(),
block: Block {
brace_token: syn::token::Brace::default(),
stmts,
},
}),
None,
)];
}
match result_handling {
ResultHandling::None => quote! { #(#attrs)* fn main() { #(#stmts)* } },
ResultHandling::Discard => {
quote! { #(#attrs)* fn main() { _ = (|| { #(#stmts)* })() } }
}
ResultHandling::Print if pretty => {
quote! { #(#attrs)* fn main() { ::std::println!("{:#?}", (|| { #(#stmts)* })()) } }
}
ResultHandling::Print => {
quote! { #(#attrs)* fn main() { ::std::println!("{:?}", (|| { #(#stmts)* })()) } }
}
}
.to_string()
.into()
}

/// Send a Discord reply with the formatted contents of a Playground result
Expand Down Expand Up @@ -362,6 +405,7 @@ pub fn format_play_eval_stderr(stderr: &str, show_compiler_warnings: bool) -> St
} else {
program_stderr.to_owned()
}
.replace('`', "\u{200b}`")
} else {
// Program didn't get to run, so there must be an error, so we yield the compiler output
// regardless of whether warn is enabled or not
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ async fn poise(#[shuttle_secrets::Secrets] secret_store: SecretStore) -> Shuttle
poise::Prefix::Literal("🦀"),
poise::Prefix::Literal("<:ferris:358652670585733120> "),
poise::Prefix::Literal("<:ferris:358652670585733120>"),
poise::Prefix::Literal("<:ferrisballSweat:678714352450142239> "),
poise::Prefix::Literal("<:ferrisballSweat:678714352450142239>"),
poise::Prefix::Literal("<:sweat:678714352450142239> "),
poise::Prefix::Literal("<:sweat:678714352450142239>"),
poise::Prefix::Literal("<:owo:678714352450142239> "),
poise::Prefix::Literal("<:owo:678714352450142239>"),
poise::Prefix::Literal("<:owo:579331467000283136> "),
poise::Prefix::Literal("<:owo:579331467000283136>"),
poise::Prefix::Regex(
"(yo|hey) (crab|ferris|fewwis),? can you (please |pwease )?"
"(yo |hey )?(crab|ferris|fewwis),? can you (please |pwease )?"
.parse()
.unwrap(),
),
Expand Down
Loading