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

Fix locations for if else arm and switch if arms #173

Merged
merged 2 commits into from
Apr 3, 2020
Merged
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
18 changes: 9 additions & 9 deletions src/dreamchecker/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,6 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
// TODO: factor in the previous return type if there was one
let return_type = self.visit_expression(location, expr, None, local_vars);
local_vars.get_mut(".").unwrap().analysis = return_type;
// TODO: break out of the analysis for this branch?
return ControlFlow { returns: true, continues: false, breaks: false, fuzzy: false }
},
Statement::Return(None) => { return ControlFlow { returns: true, continues: false, breaks: false, fuzzy: false } },
Expand Down Expand Up @@ -1270,9 +1269,10 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
}
if let Some(else_arm) = else_arm {
if alwaystrue {
// TODO: fix location for else blocks
error(location,"unreachable else block, preceeding if/elseif condition(s) are always true")
.register(self.context);
if let Some(else_expr) = else_arm.first() {
error(else_expr.location ,"unreachable else block, preceeding if/elseif condition(s) are always true")
.register(self.context);
}
}
let state = self.visit_block(else_arm, &mut local_vars.clone());
allterm.merge_false(state);
Expand Down Expand Up @@ -1370,14 +1370,14 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
let mut allterm = ControlFlow::alltrue();
self.visit_control_condition(location, input);
self.visit_expression(location, input, None, local_vars);
for &(ref case, ref block) in cases.iter() {
for (case, ref block) in cases.iter() {
let mut scoped_locals = local_vars.clone();
for case_part in case.iter() {
for case_part in case.elem.iter() {
match case_part {
dm::ast::Case::Exact(expr) => { self.visit_expression(location, expr, None, &mut scoped_locals); },
dm::ast::Case::Exact(expr) => { self.visit_expression(case.location, expr, None, &mut scoped_locals); },
dm::ast::Case::Range(start, end) => {
self.visit_expression(location, start, None, &mut scoped_locals);
self.visit_expression(location, end, None, &mut scoped_locals);
self.visit_expression(case.location, start, None, &mut scoped_locals);
self.visit_expression(case.location, end, None, &mut scoped_locals);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dreamchecker/tests/branch_eval_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub const IF_ARMS_ERRORS: &[(u32, u16, &str)] = &[
(2, 7, "if condition is always true"),
(4, 12, "unreachable if block, preceeding if/elseif condition(s) are always true"),
// TODO: fix location reporting on this
(2, 5, "unreachable else block, preceeding if/elseif condition(s) are always true"),
(7, 9, "unreachable else block, preceeding if/elseif condition(s) are always true"),
];

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/dreammaker/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ pub enum Statement {
},
Switch {
input: Expression,
cases: Vec<(Vec<Case>, Block)>,
cases: Vec<(Spanned<Vec<Case>>, Block)>,
default: Option<Block>,
},
TryCatch {
Expand Down
2 changes: 1 addition & 1 deletion src/dreammaker/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {
self.context.register_error(self.error("switch case cannot be empty"));
}
let block = require!(self.block(loop_ctx));
cases.push((what, block));
cases.push((Spanned::new(self.location(), what), block));
}
let default = if let Some(()) = self.exact_ident("else")? {
Some(require!(self.block(loop_ctx)))
Expand Down
10 changes: 5 additions & 5 deletions src/langserver/find_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ impl<'o> WalkProc<'o> {
},
Statement::Switch { input, cases, default } => {
self.visit_expression(location, input, None);
for &(ref case, ref block) in cases.iter() {
for case_part in case.iter() {
for (case, ref block) in cases.iter() {
for case_part in case.elem.iter() {
match case_part {
dm::ast::Case::Exact(expr) => { self.visit_expression(location, expr, None); },
dm::ast::Case::Exact(expr) => { self.visit_expression(case.location, expr, None); },
dm::ast::Case::Range(start, end) => {
self.visit_expression(location, start, None);
self.visit_expression(location, end, None);
self.visit_expression(case.location, start, None);
self.visit_expression(case.location, end, None);
}
}
}
Expand Down