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

Requested feature, detect for loops over turf contents #330

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions CONFIGURING.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ The `[code_standards]` section has the following options:

* `disallow_relative_proc_definitions` - Raised on relative pathed proc definitions
* `disallow_relative_type_definitions` - Raised on relative pathed subtype defintions
* `disallow_turf_contents_iteration` - Raised on attempting to use a for loop over a turf or turf.contents

### DM Doc

Expand Down
26 changes: 24 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 @@ -1385,6 +1385,28 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
let ForListStatement { var_type, name, input_type, in_list, block } = &**for_list;
let mut scoped_locals = local_vars.clone();
if let Some(in_list) = in_list {
if self.context.config().code_standards.disallow_turf_contents_iteration {
if let Expression::Base { term, follow } = in_list {
let ty = self.visit_term(term.location, &term.elem, None, &mut scoped_locals);
if ty.static_ty == StaticType::Type(self.objtree.expect("/turf")) {
if follow.is_empty() {
error(term.location, "iterating over turf contents")
.set_severity(Severity::Error)
.register(self.context);
}

if follow.len() == 1 {
if let Follow::Field(_, ident) = &follow.first().unwrap().elem {
if ident == "contents" {
error(term.location, "iterating over turf contents")
.set_severity(Severity::Error)
.register(self.context);
}
}
}
}
}
}
let list = self.visit_expression(location, in_list, None, &mut scoped_locals);
match list.static_ty {
StaticType::None => {
Expand Down
10 changes: 8 additions & 2 deletions crates/dreamchecker/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use crate::{run_inner};

pub const NO_ERRORS: &[(u32, u16, &str)] = &[];

pub fn parse_a_file_for_test<S: Into<Cow<'static, str>>>(buffer: S) -> Context {
pub fn parse_a_file_for_test<S: Into<Cow<'static, str>>>(buffer: S, config: String) -> Context {
let context = Context::default();

context.set_config(config);

let pp = dm::preprocessor::Preprocessor::from_buffer(&context, "unit_tests.rs".into(), buffer.into());

let indents = dm::indents::IndentProcessor::new(&context, pp);
Expand All @@ -22,7 +24,11 @@ pub fn parse_a_file_for_test<S: Into<Cow<'static, str>>>(buffer: S) -> Context {
}

pub fn check_errors_match<S: Into<Cow<'static, str>>>(buffer: S, errorlist: &[(u32, u16, &str)]) {
let context = parse_a_file_for_test(buffer);
check_errors_match_with_config(buffer, errorlist, String::new());
}

pub fn check_errors_match_with_config<S: Into<Cow<'static, str>>>(buffer: S, errorlist: &[(u32, u16, &str)], config: String) {
let context = parse_a_file_for_test(buffer, config);
let errors = context.errors();
let mut iter = errors.iter();
for (line, column, desc) in errorlist {
Expand Down
27 changes: 27 additions & 0 deletions crates/dreamchecker/tests/turf_iteration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

extern crate dreamchecker as dc;

use dc::test_helpers::*;

pub const CONST_EVAL_ERRORS: &[(u32, u16, &str)] = &[
(3, 18, "iterating over turf contents"),
(5, 18, "iterating over turf contents"),
];

#[test]
fn const_eval() {
let code = r##"
/proc/test()
var/turf/T = new /turf
for(var/a in T)
world.log << a
for(var/a in T.contents)
world.log << a
"##.trim();
let config = r##"
[code_standards]
disallow_turf_contents_iteration = true
"##.trim();
check_errors_match_with_config(code, CONST_EVAL_ERRORS, config.to_string());
}

5 changes: 5 additions & 0 deletions crates/dreammaker/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct Langserver {
pub struct CodeStandards {
pub disallow_relative_proc_definitions: bool,
pub disallow_relative_type_definitions: bool,
pub disallow_turf_contents_iteration: bool,
}

/// DMDoc config options
Expand Down Expand Up @@ -119,6 +120,10 @@ impl Config {
Ok(toml::from_str(&config_toml)?)
}

pub fn read_toml_string(toml: String) -> Result<Config, Error> {
Ok(toml::from_str(&toml)?)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this takes &str, presumably so could the chain that calls it, rather than requiring config.to_string() in the tests

}

fn config_warninglevel(&self, error: &DMError) -> Option<&WarningLevel> {
if let Some(errortype) = error.errortype() {
return self.diagnostics.get(errortype)
Expand Down
4 changes: 4 additions & 0 deletions crates/dreammaker/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ impl Context {
// ------------------------------------------------------------------------
// Configuration

pub fn set_config(&self, config: String) {
*self.config.borrow_mut() = Config::read_toml_string(config).unwrap();
}

pub fn force_config(&self, toml: &Path) {
match Config::read_toml(toml) {
Ok(config) => *self.config.borrow_mut() = config,
Expand Down