Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3507,6 +3507,68 @@ return t
assert_eq!(changed.1.len(), 1, "{changed:?}");
}

/// #1334: a read through an index signature has the signature's value type.
#[test]
fn test_index_signature_read_has_value_type() {
for (decl, read, accepted) in [
("---@param pt table<string, integer>", "pt.X", false),
("---@param pt table<string, integer>", "pt['X']", false),
("---@param pt table<string, integer>", "pt[k]", false),
("---@param pt table<integer, integer>", "pt[1]", false),
("---@param pt I1334", "pt.X", false),
("---@param pt I1334", "pt[k]", false),
// The key must fit the index domain; a named field beats the signature.
("---@param pt table<'X', integer>", "pt.Y", true),
("---@param pt table<1, integer>", "pt[2]", true),
("---@param pt I1334", "pt.flag", true),
] {
let source = format!(
r#"
---@class I1334
---@field flag boolean
---@field [string] integer

{decl}
---@param k string
local function g(pt, k)
---@type boolean
local a = {read}
return a
end
return g
"#
);
let mut ws = VirtualWorkspace::new();
assert_eq!(
ws.has_no_diagnostic(DiagnosticCode::AssignTypeMismatch, &source),
accepted,
"{source}"
);
}
}

/// #1334: the `{ [K]: V }` form (a `---@param` of it is still a plain `table`, so a typed local).
#[test]
fn test_object_index_signature_read_has_value_type() {
for read in ["pt.X", "pt[k]"] {
let source = format!(
r#"
---@type {{ [string]: integer }}
local pt = {{}}
local k = tostring(1)
---@type boolean
local a = {read}
return a
"#
);
let mut ws = VirtualWorkspace::new();
assert!(
!ws.has_no_diagnostic(DiagnosticCode::AssignTypeMismatch, &source),
"{source}"
);
}
}

/// Mixed guards on one member, read past the merge of earlier ones: what the path
/// collected applies to the memoized state in the order of the program.
#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,22 @@ mod test {
"#,
));
}

/// #1334: a `table<string, integer>` read is `integer`, which is not callable.
#[test]
fn test_index_signature_value_is_not_callable() {
let diagnostics = check::test::check_source(
r#"
---@param pt table<string, integer>
local function g(pt)
return pt.X()
end
return g
"#,
);
assert_eq!(
check::test::count_by_code(&diagnostics, DiagnosticCode::CallNonCallable),
1
);
}
}
157 changes: 157 additions & 0 deletions crates/emmylua_code_analysis/src/check/test/need_check_nil_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1897,4 +1897,161 @@ mod test {
"#
));
}

#[test]
fn test_dynamic_index_guard_by_local_key() {
// A dynamic key named by a local identifies the access: the guard covers the same `toks[k]`.
let mut ws = VirtualWorkspace::new();
assert!(ws.has_no_diagnostic(
DiagnosticCode::NeedCheckNil,
r#"
---@class Tok
---@field line integer

---@param toks Tok[]
---@param k integer
local function f(toks, k)
local line = toks[k] and toks[k].line
while toks[k] and toks[k].line == line do
k = k + 1
end
if toks[k] ~= nil then return toks[k].line end
end
return f
"#
));
}

#[test]
fn test_dynamic_index_guard_lost_on_key_or_slot_write() {
let mut ws = VirtualWorkspace::new();
// The key moved: `toks[k]` is now another slot.
assert!(!ws.has_no_diagnostic(
DiagnosticCode::NeedCheckNil,
r#"
---@class Tok
---@field line integer

---@param toks Tok[]
---@param k integer
local function f(toks, k)
if toks[k] then
k = k + 1
return toks[k].line
end
end
return f
"#
));
// A different local key is a different access.
assert!(!ws.has_no_diagnostic(
DiagnosticCode::NeedCheckNil,
r#"
---@class Tok
---@field line integer

---@param toks Tok[]
---@param k integer
---@param j integer
local function f(toks, k, j)
if toks[k] then return toks[j].line end
end
return f
"#
));
// A literal-key write may be the same slot.
assert!(!ws.has_no_diagnostic(
DiagnosticCode::NeedCheckNil,
r#"
---@class Tok
---@field line integer

---@param toks Tok[]
---@param k integer
local function f(toks, k)
if toks[k] then
toks[1] = nil
return toks[k].line
end
end
return f
"#
));
}

#[test]
fn test_dynamic_index_guard_by_length_key() {
let mut ws = VirtualWorkspace::new();
assert!(ws.has_no_diagnostic(
DiagnosticCode::NeedCheckNil,
r#"
---@class Stage
---@field final boolean

---@param stages Stage[]
local function f(stages)
return stages[#stages] and stages[#stages].final
end
return f
"#
));
// Any write below the length's path may move it.
assert!(!ws.has_no_diagnostic(
DiagnosticCode::NeedCheckNil,
r#"
---@class Stage
---@field final boolean

---@param stages Stage[]
---@param other Stage[]
local function f(stages, other)
if stages[#other] then
other[#other + 1] = stages[1]
return stages[#other].final
end
end
return f
"#
));
}

#[test]
fn test_dynamic_key_write_establishes_the_read() {
// `t[k] = t[k] or {}` makes a later `t[k]` the assigned value on that path, as long as
// neither `t[k]` nor `k` is written in between.
let cases = [
("t[k] = t[k] or {}\nlocal b = t[k]\nb[#b + 1] = 'x'", 0),
("t[k] = {}\nlocal n = #t[k]", 0),
(
"if c then t[k] = {} else t[k] = { 'y' } end\nlocal n = #t[k]",
0,
),
("t[k] = {}\nk = j\nlocal n = #t[k]", 1),
("t[k] = {}\nlocal n = #t[j]", 1),
("t[k] = {}\nt[j] = nil\nlocal n = #t[k]", 1),
("t[k] = maybe\nlocal n = #t[k]", 1),
("if c then t[k] = {} end\nlocal n = #t[k]", 1),
];
let wrong: Vec<_> = cases
.iter()
.filter_map(|(body, expected)| {
let source = format!(
r#"
---@param k string
---@param j string
---@param c boolean
---@param maybe string[]?
local function f(k, j, c, maybe)
---@type table<string, string[]?>
local t = {{}}
{body}
end
"#
);
let found = count_by_code(&check_source(&source), DiagnosticCode::NeedCheckNil);
(found != *expected).then_some((body, found))
})
.collect();
assert!(wrong.is_empty(), "{wrong:#?}");
}
}
Loading
Loading