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

Lint initial(thing.var), where var is a list #307

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
33 changes: 31 additions & 2 deletions crates/dreamchecker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl<'o> AnalyzeObjectTree<'o> {
.with_blocking_builtins(self.sleeping_procs.get_violators(*child_violator).unwrap())
.register(self.context)
}
}
}
if let Some(calledvec) = self.call_tree.get(&nextproc) {
for (proccalled, location, new_context) in calledvec.iter() {
let mut newstack = callstack.clone();
Expand Down Expand Up @@ -743,7 +743,7 @@ impl<'o> AnalyzeObjectTree<'o> {
.with_blocking_builtins(self.impure_procs.get_violators(*child_violator).unwrap())
.register(self.context)
}
}
}
if let Some(calledvec) = self.call_tree.get(&nextproc) {
for (proccalled, location, new_context) in calledvec.iter() {
let mut newstack = callstack.clone();
Expand Down Expand Up @@ -2133,6 +2133,21 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
}
}

fn initial_null_error(&mut self, static_ty: StaticType, name: String, location: Location) {
match static_ty {
StaticType::List { list: _, keys: _ } => {
error(location, format!("initial() called on var/list/{}, but it always returns null for lists", name))
.register(self.context);
}
StaticType::Type(ty) if ty == self.objtree.expect("/icon") => {}
StaticType::Type(ty) => {
error(location, format!("initial() called on var{}/{}, but it always returns null for types", ty.pretty_path() , name))
.register(self.context);
}
_ => {}
}
}

fn visit_call(&mut self, location: Location, src: TypeRef<'o>, proc: ProcRef<'o>, args: &'o [Expression], is_exact: bool, local_vars: &mut HashMap<String, LocalVar<'o>, RandomState>) -> Analysis<'o> {
self.env.call_tree.entry(self.proc_ref).or_default().push((proc, location, self.inside_newcontext != 0));
if let Some((privateproc, true, decllocation)) = self.env.private.get_self_or_parent(proc) {
Expand Down Expand Up @@ -2219,6 +2234,20 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
}

let analysis = self.visit_expression(location, argument_value, None, local_vars);

if proc.is_builtin() && proc.name() == "initial" && analysis.static_ty != StaticType::None {
if let Expression::Base{term, follow} = arg {
if let Some(Spanned{location, elem: Follow::Field(_, ident)}) = follow.last() {
self.initial_null_error(analysis.static_ty.clone(), ident.to_string(), *location);
} else {
let argument_analysis = self.visit_term(term.location, &term.elem, None, local_vars);
if let Term::Ident(ident_name) = &term.elem {
self.initial_null_error(argument_analysis.static_ty, ident_name.to_string(), term.location);
}
}
}
}

if let Some(kw) = this_kwarg {
param_name_map.insert(kw.as_str(), analysis);
param_expr_map.insert(kw.as_str(), argument_value);
Expand Down
30 changes: 30 additions & 0 deletions crates/dreamchecker/tests/initial_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

extern crate dreamchecker as dc;

use dc::test_helpers::*;

pub const INITIAL_FOLLOW_ERRORS: &[(u32, u16, &str)] = &[
(7, 17, "initial() called on var/list/bar, but it always returns null for lists"),
(8, 17, "initial() called on var/turf/T, but it always returns null for types"),
(11, 13, "initial() called on var/list/bar, but it always returns null for lists"),
(12, 13, "initial() called on var/turf/T, but it always returns null for types"),
];

#[test]
fn initial_follow() {
let code = r##"
/datum/foo
var/list/bar
var/turf/T

/proc/test()
var/datum/foo/test = new()
initial(test.bar)
initial(test.T)

/datum/foo/proc/test()
initial(bar)
initial(T)
"##.trim();
check_errors_match(code, INITIAL_FOLLOW_ERRORS);
}