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

Make 'Unreachable statement' a warning #2838

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
2 changes: 1 addition & 1 deletion src/semantize/flow.nit
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ redef class ABlockExpr
v.enter_visit(e)
else if not v.current_flow_context.is_already_unreachable then
v.current_flow_context.is_already_unreachable = true
v.toolcontext.error(e.hot_location, "Error: unreachable statement.")
v.toolcontext.warning(e.hot_location, "unreachable", "Warning: unreachable statement.")
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/semantize/local_var_init.nit
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ private class LocalVarInitVisitor
assert variable != null
if not maybe_unset_vars.has(variable) then return

var flow = node.after_flow_context.as(not null)
flow.set_vars.add(variable)
var flow = node.after_flow_context
if flow != null then flow.set_vars.add(variable)
end

fun check_is_set(node: AExpr, variable: nullable Variable)
do
assert variable != null
if not maybe_unset_vars.has(variable) then return

var flow = node.after_flow_context.as(not null)
if not flow.is_variable_set(variable) then
var flow = node.after_flow_context
if flow != null and not flow.is_variable_set(variable) then
self.toolcontext.error(node.hot_location, "Error: possibly unset variable `{variable}`.")
# Remove the variable to avoid repeating errors
self.maybe_unset_vars.remove(variable)
Expand Down
4 changes: 1 addition & 3 deletions src/semantize/typing.nit
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,7 @@ private class TypeVisitor
fun set_variable(node: AExpr, variable: Variable, mtype: nullable MType)
do
var flow = node.after_flow_context
assert flow != null

flow.set_var(self, variable, mtype)
if flow != null then flow.set_var(self, variable, mtype)
end

# Find the exact representable most specific common super-type in `col`.
Expand Down
49 changes: 49 additions & 0 deletions tests/base_control_flow3.nit
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import kernel

var a
a = null
a = 1

a.output
return
# Below this line the code is unreachable

var b
b = null
b = 1
a.output
b.output
a = b
b = a

if a == b then
a = b
b = a
else
a = null
b = null
end
a.output
b.output
var c = a.as(not null)
var d = b.as(Int)
var e
e.output

#alt1#(b+1).output # Error
b = 1
#alt1#(b+1).output # Still error
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow2_alt1.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
alt/base_control_flow2_alt1.nit:24,2: Error: unreachable statement.
alt/base_control_flow2_alt1.nit:24,2: Warning: unreachable statement.
alt/base_control_flow2_alt1.nit:21,1--25,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt1.nit:39,1--43,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt1.nit:47,2--51,4: Warning: use `loop` instead of `while true do`.
Expand Down
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow2_alt2.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alt/base_control_flow2_alt2.nit:21,1--25,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt2.nit:30,2: Error: unreachable statement.
alt/base_control_flow2_alt2.nit:30,2: Warning: unreachable statement.
alt/base_control_flow2_alt2.nit:39,1--43,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt2.nit:47,2--51,4: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt2.nit:45,1--55,3: Warning: use `loop` instead of `while true do`.
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow2_alt3.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alt/base_control_flow2_alt3.nit:21,1--25,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt3.nit:42,2: Error: unreachable statement.
alt/base_control_flow2_alt3.nit:42,2: Warning: unreachable statement.
alt/base_control_flow2_alt3.nit:39,1--43,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt3.nit:47,2--51,4: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt3.nit:45,1--55,3: Warning: use `loop` instead of `while true do`.
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow2_alt4.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alt/base_control_flow2_alt4.nit:21,1--25,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt4.nit:39,1--43,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt4.nit:50,3: Error: unreachable statement.
alt/base_control_flow2_alt4.nit:50,3: Warning: unreachable statement.
alt/base_control_flow2_alt4.nit:47,2--51,4: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt4.nit:45,1--55,3: Warning: use `loop` instead of `while true do`.
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow2_alt5.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alt/base_control_flow2_alt5.nit:21,1--25,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt5.nit:39,1--43,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt5.nit:47,2--51,4: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt5.nit:54,2: Error: unreachable statement.
alt/base_control_flow2_alt5.nit:54,2: Warning: unreachable statement.
alt/base_control_flow2_alt5.nit:45,1--55,3: Warning: use `loop` instead of `while true do`.
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow2_alt6.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ alt/base_control_flow2_alt6.nit:21,1--25,3: Warning: use `loop` instead of `whil
alt/base_control_flow2_alt6.nit:39,1--43,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt6.nit:47,2--51,4: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt6.nit:45,1--55,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt6.nit:60,2: Error: unreachable statement.
alt/base_control_flow2_alt6.nit:60,2: Warning: unreachable statement.
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow2_alt7.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ alt/base_control_flow2_alt7.nit:21,1--25,3: Warning: use `loop` instead of `whil
alt/base_control_flow2_alt7.nit:39,1--43,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt7.nit:47,2--51,4: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt7.nit:45,1--55,3: Warning: use `loop` instead of `while true do`.
alt/base_control_flow2_alt7.nit:67,2: Error: unreachable statement.
alt/base_control_flow2_alt7.nit:67,2: Warning: unreachable statement.
2 changes: 2 additions & 0 deletions tests/sav/base_control_flow3.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
base_control_flow3.nit:25,1--5: Warning: unreachable statement.
1
3 changes: 3 additions & 0 deletions tests/sav/base_control_flow3_alt1.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alt/base_control_flow3_alt1.nit:25,1--5: Warning: unreachable statement.
alt/base_control_flow3_alt1.nit:47,3: Error: method `+` does not exists in `nullable Object`.
alt/base_control_flow3_alt1.nit:49,3: Error: method `+` does not exists in `nullable Object`.
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow_alt2.res
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alt/base_control_flow_alt2.nit:23,2: Error: unreachable statement.
alt/base_control_flow_alt2.nit:23,2: Warning: unreachable statement.
2 changes: 1 addition & 1 deletion tests/sav/base_control_flow_alt3.res
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alt/base_control_flow_alt3.nit:54,2: Error: unreachable statement.
alt/base_control_flow_alt3.nit:54,2: Warning: unreachable statement.
8 changes: 4 additions & 4 deletions tests/sav/nit_args9.res
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
test_keep_going.nit:15,11--14: Error: class `Fail` not found in module `test_keep_going`.
fun plop: Fail
^
test_keep_going.nit:40,2--11: Error: unreachable statement.
999.output
^
test_keep_going.nit:26,2--5: Error: method or variable `fail` unknown in `Sys`.
fail
^
Expand All @@ -13,10 +10,13 @@
test_keep_going.nit:35,5: Type Error: expected `Bool`, got `Int`.
if 1 then abort
^
test_keep_going.nit:40,2--11: Warning: unreachable statement. (unreachable)
999.output
^
test_keep_going.nit:44,18--21: Error: method `fail` does not exists in `Sys`.
var a = new Sys.fail
^
Errors: 6. Warnings: 0.
Errors: 5. Warnings: 1.
1
2
3
Expand Down
8 changes: 4 additions & 4 deletions tests/sav/nitc_args12.res
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
test_keep_going.nit:15,11--14: Error: class `Fail` not found in module `test_keep_going`.
fun plop: Fail
^
test_keep_going.nit:40,2--11: Error: unreachable statement.
999.output
^
test_keep_going.nit:26,2--5: Error: method or variable `fail` unknown in `Sys`.
fail
^
Expand All @@ -13,10 +10,13 @@
test_keep_going.nit:35,5: Type Error: expected `Bool`, got `Int`.
if 1 then abort
^
test_keep_going.nit:40,2--11: Warning: unreachable statement. (unreachable)
999.output
^
test_keep_going.nit:44,18--21: Error: method `fail` does not exists in `Sys`.
var a = new Sys.fail
^
Errors: 6. Warnings: 0.
Errors: 5. Warnings: 1.
1
2
3
Expand Down
18 changes: 17 additions & 1 deletion tests/sav/syntax_lambda.res
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
syntax_lambda.nit:36,1--28: Error: unreachable statement.
syntax_lambda.nit:24,9--18: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:26,9--22: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:28,11--24: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:31,9--33,7: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:36,9--28: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:36,1--28: Warning: unreachable statement.
syntax_lambda.nit:37,10--26: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:39,9--30: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:41,9--48: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:43,10--29: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:45,5--18: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:46,5--18: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:49,1--51,8: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:55,2--15: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:56,1--14: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:58,9--60,10: no implemented accept_typing for ALambdaExpr
syntax_lambda.nit:64,1--32: no implemented accept_typing for ALambdaExpr
Loading