#![cfg(feature = "async")]
use htl::Htl;
use htl::config::LangConfig;
use std::path::{Path, PathBuf};
mod common;
fn tempdir(name: &str) -> common::TempDir {
common::tempdir("htl-async-lints", name)
}
fn write(dir: &Path, name: &str, text: &str) -> PathBuf {
let p = dir.join(name);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(&p, text).unwrap();
p
}
fn checker(dir: &Path, on: bool, spec: &str) -> Htl {
let h = Htl::new().unwrap();
h.set_lang(&LangConfig { async_: Some(on) }).unwrap();
if !spec.is_empty() {
h.configure_lints(spec).unwrap();
}
h.install_task_lib().unwrap();
h.add_path(dir).unwrap();
h.add_path(&dir.join("types")).unwrap();
h
}
const HTTP: &str = "\
local record http
get: function(path: string): string ---@async
sync: function(path: string): string
end
return http
";
const MAIN: &str = "\
local http = require(\"http\")
local async function work(n: integer): integer
return n
end
local async function pair(a: string, b: string): string, string
async local x = http.get(a)
local y = http.get(b)
local z = await http.sync(b)
local f = function(): string return x:await() end
return await x, y .. z .. f()
end
local function plain(): integer
local v = await work(1)
async local w = work(2)
return v + await w
end
local async function leak(): any
async local q = work(3)
return q
end
print(await pair(\"/a\", \"/b\"), plain())
print(await leak())
";
fn of_rule<'a>(lints: &'a [String], rule: &str) -> Vec<&'a String> {
let tag = format!("[htl {rule}]");
lints.iter().filter(|l| l.contains(&tag)).collect()
}
fn at<'a>(lints: &'a [String], pos: &str) -> Vec<&'a String> {
lints.iter().filter(|l| l.contains(pos)).collect()
}
#[test]
fn each_rule_reports_its_case_at_the_line_and_column() {
let dir = tempdir("rules");
write(&dir, "types/http.d.tl", HTTP);
let main = write(&dir, "main.tl", MAIN);
let ci = checker(&dir, true, "").check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
let l = &ci.lints;
let m = of_rule(l, "await-missing");
assert_eq!(m.len(), 1, "{l:?}");
assert!(
m[0].contains(
"main.tl:9:14: call of an async function without await: http.get may suspend"
),
"{}",
m[0]
);
let n = of_rule(l, "await-non-async");
assert_eq!(n.len(), 1, "{l:?}");
assert!(n[0].contains("main.tl:10:14: await on a call of a function that is not async: http.sync cannot suspend"), "{}", n[0]);
let e = of_rule(l, "task-escape");
assert_eq!(e.len(), 2, "{l:?}");
assert!(
e[0].contains("main.tl:11:40: task 'x' is captured by a function"),
"{}",
e[0]
);
assert!(
e[1].contains("main.tl:23:11: task 'q' is returned"),
"{}",
e[1]
);
let o = of_rule(l, "await-outside-async");
assert_eq!(o.len(), 3, "{l:?}");
assert!(
o[0].contains("main.tl:16:14: await in a function that is not async"),
"{}",
o[0]
);
assert!(
o[1].contains("main.tl:17:4: async local in a function that is not async"),
"{}",
o[1]
);
assert!(
o[2].contains("main.tl:18:15: await in a function that is not async"),
"{}",
o[2]
);
assert!(
at(l, ":8:").is_empty()
&& at(l, ":22:").is_empty()
&& at(l, ":26:").is_empty()
&& at(l, ":27:").is_empty(),
"{l:?}"
);
assert_eq!(l.len(), 7, "{l:?}");
}
#[test]
fn a_required_modules_top_level_await_is_reported_by_the_requirer_and_not_by_its_own_check() {
let dir = tempdir("module");
write(&dir, "types/http.d.tl", HTTP);
let module = write(
&dir,
"mod.tl",
"local http = require(\"http\")\nlocal mod = {}\nlocal x = await http.get(\"/top\")\nasync local t = http.get(\"/t\")\nfunction mod.f(): string\n return x .. tostring(t:done())\nend\nreturn mod\n",
);
let main = write(
&dir,
"main.tl",
"local mod = require(\"mod\")\nprint(mod.f())\n",
);
let h = checker(&dir, true, "");
let ci = h.check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
let o = of_rule(&ci.lints, "await-outside-async");
assert_eq!(o.len(), 2, "{:?}", ci.lints);
assert!(
o[0].contains("mod.tl:3:11: await at the top level of a module: 'require' cannot yield"),
"{}",
o[0]
);
assert!(
o[1].contains("mod.tl:4:1: async local at the top level of a module"),
"{}",
o[1]
);
let own = h.check(&module).unwrap();
assert!(own.errors.is_empty(), "{:?}", own.errors);
assert!(
of_rule(&own.lints, "await-outside-async").is_empty(),
"{:?}",
own.lints
);
}
#[test]
fn an_async_function_of_a_required_module_needs_await_at_its_call() {
let dir = tempdir("teal-callee");
write(
&dir,
"lib.tl",
"local lib = {}\nasync function lib.fetch(p: string): string\n return p\nend\nreturn lib\n",
);
let main = write(
&dir,
"main.tl",
"local lib = require(\"lib\")\nlocal a = lib.fetch(\"/a\")\nlocal b = await lib.fetch(\"/b\")\nprint(a, b)\n",
);
let ci = checker(&dir, true, "").check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
let m = of_rule(&ci.lints, "await-missing");
assert_eq!(m.len(), 1, "{:?}", ci.lints);
assert!(
m[0].contains(
"main.tl:2:11: call of an async function without await: lib.fetch may suspend"
),
"{}",
m[0]
);
assert!(
of_rule(&ci.lints, "await-non-async").is_empty(),
"{:?}",
ci.lints
);
}
#[test]
fn a_spec_turns_a_rule_off_or_up() {
let dir = tempdir("levels");
write(&dir, "types/http.d.tl", HTTP);
let main = write(&dir, "main.tl", MAIN);
let off = checker(&dir, true, "-await-non-async,-task-escape")
.check(&main)
.unwrap();
assert!(
of_rule(&off.lints, "await-non-async").is_empty(),
"{:?}",
off.lints
);
assert!(
of_rule(&off.lints, "task-escape").is_empty(),
"{:?}",
off.lints
);
assert_eq!(
of_rule(&off.lints, "await-missing").len(),
1,
"{:?}",
off.lints
);
assert_eq!(
htl::lint::RULES
.iter()
.find(|r| r.name == "await-non-async")
.unwrap()
.default,
htl::lint::Level::Warn
);
assert_eq!(
htl::lint::RULES
.iter()
.find(|r| r.name == "await-missing")
.unwrap()
.default,
htl::lint::Level::Deny
);
}
#[test]
fn with_the_setting_off_nothing_is_reported() {
let dir = tempdir("off");
write(&dir, "types/http.d.tl", HTTP);
write(&dir, "types/api.d.tl", API);
let main = write(
&dir,
"main.tl",
"local http = require(\"http\")\nlocal await = 1\nlocal async = 2\nprint(http.get(\"/a\"), await + async)\nlocal t = { \"b\", \"a\" }\ntable.sort(t, function(a: string, b: string): boolean return a < b end)\nprint(string.gsub(\"a\", \"%w\", http.get))\nlocal api = require(\"api\")\nprint(api:each(api.fetch), api.each(api, api.fetch))\nlocal game: api.Game = { update = api.tick, draw = function() end }\nprint(game)\n",
);
let ci = checker(&dir, false, "").check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
assert!(
ci.lints.iter().all(|l| !l.contains("[htl await")
&& !l.contains("[htl task-escape]")
&& !l.contains("[htl async-as-sync-callback]")),
"{:?}",
ci.lints
);
}
#[test]
fn the_macro_declares_an_async_fn_with_the_marker() {
pub struct Api;
#[htl::host_module(name = "api")]
impl Api {
pub async fn fetch(&self, path: String) -> String {
path
}
pub fn sync(&self, path: String) -> String {
path
}
}
let decl = <Api as htl::teal::HostModule>::DECL;
assert!(
decl.contains("fetch: function(self: api, path: string): string ---@async\n"),
"{decl}"
);
assert!(
decl.contains("sync: function(self: api, path: string): string\n"),
"{decl}"
);
let dir = tempdir("macro");
write(&dir, "types/api.d.tl", decl);
let main = write(
&dir,
"main.tl",
"local api = require(\"api\")\nlocal a: api = nil\nprint(a:fetch(\"/x\"), await a:sync(\"/y\"))\n",
);
let ci = checker(&dir, true, "").check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
let m = of_rule(&ci.lints, "await-missing");
assert_eq!(m.len(), 1, "{:?}", ci.lints);
assert!(
m[0].contains("main.tl:3:7: call of an async function without await: a:fetch may suspend"),
"{}",
m[0]
);
let n = of_rule(&ci.lints, "await-non-async");
assert_eq!(n.len(), 1, "{:?}", ci.lints);
assert!(
n[0].contains(
"main.tl:3:22: await on a call of a function that is not async: a:sync cannot suspend"
),
"{}",
n[0]
);
}
#[test]
fn the_macro_declares_a_sync_fn_s_function_parameters_noyield() {
use htl::mlua::Function;
pub struct Api;
#[htl::host_module(name = "api")]
impl Api {
pub async fn fetch(&self, path: String) -> String {
path
}
pub fn each(&self, f: Function) -> String {
f.call(()).unwrap()
}
pub async fn each_async(&self, f: Function) -> String {
f.call_async(()).await.unwrap()
}
pub fn on(&self, #[teal(yields)] f: Function) {
drop(f)
}
pub async fn each_sync_call(&self, #[teal(noyield)] f: Function) -> String {
f.call(()).unwrap()
}
pub fn walk(&self, f: Option<Function>) {
drop(f)
}
pub fn split(&self, on_ok: Function, on_err: Function) {
drop((on_ok, on_err))
}
}
let decl = <Api as htl::teal::HostModule>::DECL;
assert!(
decl.contains("each: function(self: api, f: function): string ---@noyield(f)\n"),
"{decl}"
);
let dir = tempdir("macro-noyield");
write(&dir, "types/api.d.tl", decl);
let main = write(
&dir,
"main.tl",
"local api = require(\"api\")
local async function cb(): string
return await api:fetch(\"/a\")
end
print(api:each(cb))
print(await api:each_async(cb))
api:on(cb)
print(await api:each_sync_call(cb))
api:walk(cb)
api:split(cb, cb)
",
);
let ci = checker(&dir, true, "").check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
let c = of_rule(&ci.lints, "async-as-sync-callback");
let want = [
"main.tl:5:16: async function passed to api:each: its declaration says 'f' is called from C (---@noyield)",
"main.tl:8:32: async function passed to api:each_sync_call: its declaration says 'f' is called from C (---@noyield)",
"main.tl:9:10: async function passed to api:walk: its declaration says 'f' is called from C (---@noyield)",
"main.tl:10:11: async function passed to api:split: its declaration says 'on_ok' is called from C (---@noyield)",
"main.tl:10:15: async function passed to api:split: its declaration says 'on_err' is called from C (---@noyield)",
];
assert_eq!(c.len(), want.len(), "{c:?}");
for (got, w) in c.iter().zip(want) {
assert!(got.contains(w), "{got} lacks {w}");
}
assert_eq!(ci.lints.len(), want.len(), "{:?}", ci.lints);
}
mod game_record {
use htl::mlua::Function;
#[derive(htl::TealRecord)]
#[allow(dead_code)]
pub struct Game {
#[teal(noyield)]
load: Option<Function>,
#[teal(noyield)]
update: Function,
#[teal(noyield)]
draw: Function,
later: Option<Function>,
}
pub struct Api;
#[htl::host_module(name = "api", records = [Game])]
impl Api {
pub async fn fetch(&self, path: String) -> String {
path
}
}
}
#[test]
fn the_derive_declares_a_record_field_noyield() {
let decl = <game_record::Api as htl::teal::HostModule>::DECL;
assert!(
decl.contains(
" record Game\n load: function ---@noyield\n update: function ---@noyield\n draw: function ---@noyield\n later: function\n end\n"
),
"{decl}"
);
let dir = tempdir("derive-noyield");
write(&dir, "types/api.d.tl", decl);
let main = write(
&dir,
"main.tl",
"local api = require(\"api\")
local game: api.Game = {
update = async function(dt: number): boolean
return (await api:fetch(\"/a\")) ~= \"\" and dt > 0
end,
draw = function() end,
}
local calm: api.Game = {
update = function(dt: number): boolean return dt > 0 end,
draw = function() end,
}
local later: api.Game = { update = calm.update, draw = calm.draw, later = async function() end }
print(game, later)
",
);
let ci = checker(&dir, true, "").check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
let c = of_rule(&ci.lints, "async-as-sync-callback");
let want = "main.tl:3:13: async function bound to update of api.Game: the record's declaration says the field is called from C (---@noyield)";
assert_eq!(c.len(), 1, "{:?}", ci.lints);
assert!(c[0].contains(want), "{} lacks {want}", c[0]);
assert_eq!(ci.lints.len(), 1, "{:?}", ci.lints);
}
const ORDER: &str = "\
local record order
less: function(a: string, b: string): boolean ---@async
end
return order
";
const CALLBACKS: &str = "\
local http = require(\"http\")
local order = require(\"order\")
local Doc = require(\"doc\")
local async function cmp(a: string, b: string): boolean
return a < b
end
local d: Doc = nil
local async function shout(w: string): string
return w:upper()
end
local function sync_cmp(a: string, b: string): boolean
return a < b
end
local function awaiting_cmp(a: string, b: string): boolean
local s = await http.get(a)
return s < b
end
local async function body(): string
return \"ok\"
end
local async function handler(e: any): string
return tostring(e)
end
local record R
name: string
metamethod __tostring: function(R): string
end
local async function show(r: R): string return r.name end
local items: {string} = { \"b\", \"a\" }
table.sort(items, cmp)
table.sort(items, order.less)
table.sort(items, async function(a: string, b: string): boolean return a < b end)
table.sort(items, function(a: string, b: string): boolean return a < b end)
table.sort(items, function(a: string, b: string): boolean return (await http.get(a)) < b end)
print(string.gsub(\"a b\", \"%w+\", shout))
print(string.gsub(\"a b\", \"%w+\", http.get))
print(xpcall(body, handler))
local r: R = setmetatable({ name = \"r\" }, { __tostring = show } as metatable<R>)
print(r, d:gsub(\"%w+\", shout))
local s = \"a b\"
print(s:gsub(\"%w+\", shout))
";
const DOC: &str = "\
local record Doc
gsub: function(self: Doc, pat: string, f: function(string): string): string
end
return Doc
";
#[test]
fn an_async_function_handed_to_a_c_callee_is_reported_at_the_argument() {
let dir = tempdir("callbacks");
write(&dir, "types/http.d.tl", HTTP);
write(&dir, "types/order.d.tl", ORDER);
write(&dir, "types/doc.d.tl", DOC);
let main = write(&dir, "main.tl", CALLBACKS);
let ci = checker(&dir, true, "").check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
let l = &ci.lints;
let c = of_rule(l, "async-as-sync-callback");
let want = [
"main.tl:35:19: async function passed to table.sort",
"main.tl:36:19: async function passed to table.sort",
"main.tl:37:19: async function passed to table.sort",
"main.tl:40:33: async function passed to string.gsub",
"main.tl:41:33: async function passed to string.gsub",
"main.tl:42:20: async function passed to xpcall",
"main.tl:43:58: async function bound to __tostring",
"main.tl:46:21: async function passed to string.gsub",
];
assert_eq!(c.len(), want.len(), "{c:?}");
for (got, w) in c.iter().zip(want) {
assert!(got.contains(w), "{got} lacks {w}");
}
assert!(
c[0].contains("attempt to yield across a C-call boundary"),
"{}",
c[0]
);
assert!(c[5].contains("error in error handling"), "{}", c[5]);
for pos in [":38:", ":39:", ":42:14:", ":44:"] {
assert!(!c.iter().any(|s| s.contains(pos)), "{pos} in {c:?}");
}
let o = of_rule(l, "await-outside-async");
assert_eq!(o.len(), 2, "{l:?}");
assert!(
o[0].contains("main.tl:17:14: await in a function that is not async"),
"{}",
o[0]
);
assert!(
o[1].contains("main.tl:39:67: await in a function that is not async"),
"{}",
o[1]
);
let off = checker(&dir, true, "-async-as-sync-callback")
.check(&main)
.unwrap();
assert!(
of_rule(&off.lints, "async-as-sync-callback").is_empty(),
"{:?}",
off.lints
);
assert_eq!(
htl::lint::RULES
.iter()
.find(|r| r.name == "async-as-sync-callback")
.unwrap()
.default,
htl::lint::Level::Deny
);
}
const API: &str = "\
local record api
record Game
update: function(dt: number): boolean ---@noyield
draw: function() ---@noyield
end
each: function(self: api, f: function): string ---@noyield(f)
each_async: function(self: api, f: function): string ---@async
plain: function(self: api, f: function)
split: function(self: api, on_ok: function, on_err: function) ---@noyield(on_ok, on_err)
walk: function(self: api, f?: function) ---@noyield(f)
fetch: function(self: api, path: string): string ---@async
tick: function(dt: number): boolean ---@async
mk: function(self: api, f: function): function(a: string, b: function) ---@noyield(f)
first: function( ---@noyield(f)
self: api,
f: function
)
vr: function(self: api, ...: function) ---@noyield(...)
run: function(self: api, game: Game)
record Sub
each: function(self: Sub, f: function) ---@noyield(f)
end
sub: Sub
free: function(f: function) ---@noyield(f)
nself: function(a: api, f: function) ---@noyield(f)
end
return api
";
const HOST_CALLBACKS: &str = "\
local api = require(\"api\")
local async function cb(): string
return await api:fetch(\"/a\")
end
local function sync_cb(): string return \"s\" end
local function each(f: function): string
f()
return \"t\"
end
print(api:each(cb))
print(api:each(async function(): string return await api:fetch(\"/b\") end))
print(api.each(api, cb))
print(await api:each_async(cb))
api:plain(cb)
print(each(cb))
print(api:each(sync_cb))
api:split(cb, async function() end)
api:walk(cb)
local game: api.Game = {
update = async function(dt: number): boolean return dt > 0 end,
draw = function() end,
}
local calm: api.Game = { update = function(dt: number): boolean return dt > 0 end, draw = function() end }
print(game, calm)
local r = api:mk(function() end)
r(\"x\", cb)
api:mk(function() end)(\"y\", cb)
api:first(cb)
api:vr(function() end, cb)
api:run({ update = async function(dt: number): boolean return dt > 0 end, draw = function() end })
local run = api.each
print(run(api, cb))
api.sub:each(cb)
api.free(cb)
api:nself(cb)
local keyed: api.Game = { [\"update\"] = async function(dt: number): boolean return dt > 0 end, draw = function() end }
local named: api.Game = { update = api.tick, draw = function() end }
print(keyed, named)
";
#[test]
fn an_async_function_handed_to_a_host_parameter_marked_noyield_is_reported_at_the_argument() {
let dir = tempdir("noyield");
write(&dir, "types/api.d.tl", API);
let main = write(&dir, "main.tl", HOST_CALLBACKS);
let ci = checker(&dir, true, "").check(&main).unwrap();
assert!(ci.errors.is_empty(), "{:?}", ci.errors);
let l = &ci.lints;
let c = of_rule(l, "async-as-sync-callback");
let want = [
"main.tl:10:16: async function passed to api:each: its declaration says 'f' is called from C (---@noyield)",
"main.tl:11:16: async function passed to api:each: its declaration says 'f' is called from C (---@noyield)",
"main.tl:12:21: async function passed to api.each: its declaration says 'f' is called from C (---@noyield)",
"main.tl:17:11: async function passed to api:split: its declaration says 'on_ok' is called from C (---@noyield)",
"main.tl:17:15: async function passed to api:split: its declaration says 'on_err' is called from C (---@noyield)",
"main.tl:18:10: async function passed to api:walk: its declaration says 'f' is called from C (---@noyield)",
"main.tl:20:13: async function bound to update of api.Game: the record's declaration says the field is called from C (---@noyield)",
"main.tl:28:11: async function passed to api:first: its declaration says 'f' is called from C (---@noyield)",
"main.tl:29:24: async function passed to api:vr: its declaration says '...' is called from C (---@noyield)",
"main.tl:30:20: async function bound to update of api.Game: the record's declaration says the field is called from C (---@noyield)",
"main.tl:32:16: async function passed to run: its declaration says 'f' is called from C (---@noyield)",
"main.tl:33:14: async function passed to api.sub:each: its declaration says 'f' is called from C (---@noyield)",
"main.tl:34:10: async function passed to api.free: its declaration says 'f' is called from C (---@noyield)",
"main.tl:35:11: async function passed to api:nself: its declaration says 'f' is called from C (---@noyield)",
"main.tl:36:40: async function bound to update of api.Game: the record's declaration says the field is called from C (---@noyield)",
"main.tl:37:36: async function bound to update of api.Game: the record's declaration says the field is called from C (---@noyield)",
];
assert_eq!(c.len(), want.len(), "{c:?}");
for (got, w) in c.iter().zip(want) {
assert!(got.contains(w), "{got} lacks {w}");
}
assert!(
c.iter()
.all(|s| s.contains("attempt to yield across a C-call boundary")),
"{c:?}"
);
for pos in [
"main.tl:13:",
"main.tl:14:",
"main.tl:15:",
"main.tl:16:",
"main.tl:21:",
"main.tl:23:",
"main.tl:26:",
"main.tl:27:",
"main.tl:29:8:",
] {
assert!(!c.iter().any(|s| s.contains(pos)), "{pos} in {c:?}");
}
assert_eq!(l.len(), want.len(), "{l:?}");
}