Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
Use nightly channel automatically when detecting #![feature]
Browse files Browse the repository at this point in the history
Closes #40.
  • Loading branch information
upsuper committed Nov 25, 2023
1 parent 1a36355 commit 7c87228
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/eval/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ async fn run_code(
) -> Result<String, reqwest::Error> {
let code = utils::normalize_unicode_chars(code);
let code = generate_code_to_send(&code, flags.bare);
let channel = flags.channel.unwrap_or(Channel::Stable);
let channel = flags.channel.unwrap_or_else(|| {
if has_feature_attr(&code) {
Channel::Nightly
} else {
Channel::Stable
}
});
let req = Request {
channel,
edition: flags.edition.unwrap_or("2021"),
Expand Down Expand Up @@ -112,6 +118,13 @@ fn generate_code_to_send(code: &str, bare: bool) -> String {
)
}

/// Check whether the code includes `#![feature(...)]`
fn has_feature_attr(code: &str) -> bool {
static RE_FEATURE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"#\s*!\s*\[\s*feature\s*\(").unwrap());
RE_FEATURE.find(code).is_some()
}

fn generate_result_from_response(resp: Response, channel: Channel, is_private: bool) -> String {
if resp.success {
let output = resp.stdout.trim();
Expand Down Expand Up @@ -271,4 +284,16 @@ mod tests {
assert_eq!(result, (header, body));
}
}

#[test]
fn test_has_feature_attr() {
assert!(has_feature_attr("#![feature(test)]\nfn main() {}"));
assert!(has_feature_attr(
"#![allow(x)]\n#![feature(test)]\nfn main() {}"
));
assert!(has_feature_attr(
"# \n!\r [ \r\n feature (test)]\nfn main() {}"
));
assert!(!has_feature_attr("#![cfg(x)]"));
}
}

0 comments on commit 7c87228

Please sign in to comment.