Add warnings on useless as(not null) (one less TODO) and fix the bug on non-null primitives.
Fixes #589
Pull-Request: #591
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>
var edges = new List[NeoEdge]
var res = neo.get("{url.to_s}/relationships/in").as(JsonArray)
for obj in res do
- edges.add(new NeoEdge.from_json(neo.as(not null), obj.as(JsonObject)))
+ edges.add(new NeoEdge.from_json(neo, obj.as(JsonObject)))
end
internal_in_edges = edges
return edges
redef init from_neo(neo, url) do
super
- var obj = neo.get(url.as(not null)).as(JsonObject)
+ var obj = neo.get(url).as(JsonObject)
self.internal_type = obj["type"].to_s
self.internal_from_url = obj["start"].to_s
self.internal_to_url = obj["end"].to_s
redef fun pretty_default
do
if default_value != null then
- return " ({values[default_value.as(not null)]})"
+ return " ({values[default_value]})"
else
return ""
end
return new Path(root.as(Leaf), length, st)
end
end
- return get_node_from(root.as(not null), 0, position, new List[PathElement])
+ return get_node_from(root, 0, position, new List[PathElement])
end
# Special case for when the required pos is length
var i = v.expr(self.n_expr, null)
if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i
+ if i.mtype.ctype != "val*" then return i
+
v.add("if (unlikely({i} == NULL)) \{")
v.add_abort("Cast failed")
v.add("\}")
do
var recv = v.expr(self.n_expr, null)
var args = [recv]
- for a in self.raw_arguments.as(not null) do
+ for a in self.raw_arguments do
args.add(v.expr(a, null))
end
return v.compile_callsite(self.callsite.as(not null), args)
do
var recv = v.expr(self.n_expr, null)
var args = [recv]
- for a in self.raw_arguments.as(not null) do
+ for a in self.raw_arguments do
args.add(v.expr(a, null))
end
var value = v.expr(self.n_value, null)
redef fun compile_extern_method(block, m, ecc, mmodule)
do
- var fc = new ExternCFunction(m, mmodule.as(not null))
+ var fc = new ExternCFunction(m, mmodule)
fc.decls.add( block.location.as_line_pragma )
fc.exprs.add( block.code )
ecc.add_exported_function( fc )
var bound = self.mpropdef.bound
if bound == null then return # Error thus skiped
- modelbuilder.check_visibility(n_type.as(not null), bound, mpropdef)
+ modelbuilder.check_visibility(n_type, bound, mpropdef)
# Fast case: the bound is not a formal type
if not bound isa MVirtualType then return
var recv = v.expr(self.n_expr)
if recv == null then return null
var args = [recv]
- for a in self.raw_arguments.as(not null) do
+ for a in self.raw_arguments do
var i = v.expr(a)
if i == null then return null
args.add(i)
var recv = v.expr(self.n_expr)
if recv == null then return
var args = [recv]
- for a in self.raw_arguments.as(not null) do
+ for a in self.raw_arguments do
var i = v.expr(a)
if i == null then return
args.add(i)
# We need a model to collect stufs
var model = new Model
# An a model builder to parse files
-var modelbuilder = new ModelBuilder(model, toolcontext.as(not null))
+var modelbuilder = new ModelBuilder(model, toolcontext)
var arguments = toolcontext.option_context.rest
var progname = arguments.first
mainmodule.set_imported_mmodules(mmodules)
end
-var self_mm = mainmodule.as(not null)
-var self_args = arguments.as(not null)
+var self_mm = mainmodule
+var self_args = arguments
if toolcontext.opt_debugger_autorun.value then
modelbuilder.run_debugger_autorun(self_mm, self_args)
var model = new Model
# Add a model builder to parse files
-var modelbuilder = new ModelBuilder(model, toolcontext.as(not null))
+var modelbuilder = new ModelBuilder(model, toolcontext)
var arguments = toolcontext.option_context.rest
var progname = arguments.first
mainmodule.set_imported_mmodules(mmodules)
end
-var self_mm = mainmodule.as(not null)
-var self_args = arguments.as(not null)
+var self_mm = mainmodule
+var self_args = arguments
modelbuilder.run_naive_interpreter(self_mm, self_args)
var read_args = new Array[AExpr]
var write_args = new Array[AExpr]
- for a in raw_arguments.as(not null) do
+ for a in raw_arguments do
nblock.add(a)
read_args.add(a.make_var_read)
write_args.add(a.make_var_read)
redef fun accept_typing(v)
do
var mtype = v.visit_expr(self.n_expr)
+ if mtype == null then return # Forward error
+
if mtype isa MNullType then
v.error(self, "Type error: as(not null) on null")
return
self.mtype = mtype.mtype
return
end
- # TODO: warn on useless as not null
self.mtype = mtype
+
+ if mtype isa MClassType then
+ v.modelbuilder.warning(self, "Warning: expression is already not null, since it is a `{mtype}`.")
+ return
+ end
+ assert mtype.need_anchor
+ var u = v.anchor_to(mtype)
+ if not u isa MNullableType then
+ v.modelbuilder.warning(self, "Warning: expression is already not null, since it is a `{mtype}: {u}`.")
+ return
+ end
end
end
--- /dev/null
+# 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
+
+class G[E]
+ fun foo(e: E): E
+ do
+ var ee = e.as(not null)
+ ee.output
+ return ee
+ end
+end
+
+class A
+ super G[Object]
+ redef fun foo(e)
+ do
+ var ee = e.as(not null)
+ ee.output
+ return ee
+ end
+end
+
+class B[F: Object]
+ super G[nullable F]
+ redef fun foo(e)
+ do
+ var ee = e.as(not null)
+ ee.output
+ return ee
+ end
+end
+
+class C[F: Object]
+ super G[F]
+ redef fun foo(e)
+ do
+ var ee = e.as(not null)
+ ee.output
+ return ee
+ end
+end
+
+var a = new A
+a.foo(1).output
+#alt1#a.foo(null).output
+var b = new B[Int]
+b.foo(2).output
+#alt2#b.foo(null).output
+var c = new C[Int]
+c.foo(3).output
+#alt3#c.foo(null).output
--- /dev/null
+# 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 i: Int = 1
+i.as(not null).output
+var oi: Object = i
+oi.as(not null).output
+var ni: nullable Int = i
+ni.as(not null).output
+
+base_as_notnull.nit:41,6--19: Warning: expression is already not null, since it is a `A`.
+base_as_notnull.nit:42,6--20: Warning: expression is already not null, since it is a `A`.
+base_as_notnull.nit:43,6--19: Warning: expression is already not null, since it is a `B`.
+base_as_notnull.nit:52,6--19: Warning: expression is already not null, since it is a `B`.
1
2
3
--- /dev/null
+base_as_notnull2.nit:30,12--25: Warning: expression is already not null, since it is a `G#0: Object`.
+base_as_notnull2.nit:50,12--25: Warning: expression is already not null, since it is a `G#0: Object`.
+1
+1
+2
+2
+3
+3
--- /dev/null
+alt/base_as_notnull2_alt1.nit:30,12--25: Warning: expression is already not null, since it is a `G#0: Object`.
+alt/base_as_notnull2_alt1.nit:50,12--25: Warning: expression is already not null, since it is a `G#0: Object`.
+alt/base_as_notnull2_alt1.nit:58,7--10: Type error: expected Object, got null
--- /dev/null
+alt/base_as_notnull2_alt2.nit:30,12--25: Warning: expression is already not null, since it is a `G#0: Object`.
+alt/base_as_notnull2_alt2.nit:50,12--25: Warning: expression is already not null, since it is a `G#0: Object`.
+Runtime error: Cast failed (alt/base_as_notnull2_alt2.nit:40)
+1
+1
+2
+2
--- /dev/null
+alt/base_as_notnull2_alt3.nit:30,12--25: Warning: expression is already not null, since it is a `G#0: Object`.
+alt/base_as_notnull2_alt3.nit:50,12--25: Warning: expression is already not null, since it is a `G#0: Object`.
+alt/base_as_notnull2_alt3.nit:64,7--10: Type error: expected Int, got null
+alt/base_as_notnull_alt1.nit:41,6--19: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt1.nit:42,6--20: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt1.nit:43,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt1.nit:50,6--19: Warning: expression is already not null, since it is a `A`.
alt/base_as_notnull_alt1.nit:50,6--19: Type error: expected B, got A
+alt/base_as_notnull_alt1.nit:52,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt2.nit:41,6--19: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt2.nit:42,6--20: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt2.nit:43,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt2.nit:51,6--20: Warning: expression is already not null, since it is a `A`.
alt/base_as_notnull_alt2.nit:51,6--20: Type error: expected B, got A
+alt/base_as_notnull_alt2.nit:52,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt3.nit:41,6--19: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt3.nit:42,6--20: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt3.nit:43,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt3.nit:52,6--19: Warning: expression is already not null, since it is a `B`.
alt/base_as_notnull_alt3.nit:53,6--20: Type error: expected B, got A
+alt/base_as_notnull_alt4.nit:41,6--19: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt4.nit:42,6--20: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt4.nit:43,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt4.nit:52,6--19: Warning: expression is already not null, since it is a `B`.
alt/base_as_notnull_alt4.nit:54,6--21: Type error: expected B, got A
+alt/base_as_notnull_alt5.nit:41,6--19: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt5.nit:42,6--20: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt5.nit:43,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt5.nit:52,6--19: Warning: expression is already not null, since it is a `B`.
Runtime error: Cast failed (alt/base_as_notnull_alt5.nit:59)
1
2
+alt/base_as_notnull_alt6.nit:41,6--19: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt6.nit:42,6--20: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt6.nit:43,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt6.nit:52,6--19: Warning: expression is already not null, since it is a `B`.
Runtime error: Cast failed (alt/base_as_notnull_alt6.nit:60)
1
2
+alt/base_as_notnull_alt7.nit:41,6--19: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt7.nit:42,6--20: Warning: expression is already not null, since it is a `A`.
+alt/base_as_notnull_alt7.nit:43,6--19: Warning: expression is already not null, since it is a `B`.
+alt/base_as_notnull_alt7.nit:52,6--19: Warning: expression is already not null, since it is a `B`.
alt/base_as_notnull_alt7.nit:61,1--17: Type error: as(not null) on null
--- /dev/null
+base_as_notnull_int.nit:18,1--14: Warning: expression is already not null, since it is a `Int`.
+base_as_notnull_int.nit:20,1--15: Warning: expression is already not null, since it is a `Object`.
+1
+1
+1
alt/error_expr_not_ok_alt4.nit:149,7--24: Type error: expected A, got String
alt/error_expr_not_ok_alt4.nit:150,7--18: Warning: Expression is already a Int.
alt/error_expr_not_ok_alt4.nit:150,7--18: Type error: expected A, got Int
+alt/error_expr_not_ok_alt4.nit:151,7--23: Warning: expression is already not null, since it is a `Int`.
alt/error_expr_not_ok_alt4.nit:151,7--23: Type error: expected A, got Int
alt/error_expr_not_ok_alt4.nit:152,7--18: Warning: Expression is already a Int.
alt/error_expr_not_ok_alt4.nit:152,7--18: Type error: expected A, got Bool
alt/error_expr_not_ok_alt5.nit:149,7--24: Type error: expected A, got String
alt/error_expr_not_ok_alt5.nit:150,7--18: Warning: Expression is already a Int.
alt/error_expr_not_ok_alt5.nit:150,7--18: Type error: expected A, got Int
+alt/error_expr_not_ok_alt5.nit:151,7--23: Warning: expression is already not null, since it is a `Int`.
alt/error_expr_not_ok_alt5.nit:151,7--23: Type error: expected A, got Int
alt/error_expr_not_ok_alt5.nit:152,7--18: Warning: Expression is already a Int.
alt/error_expr_not_ok_alt5.nit:152,7--18: Type error: expected A, got Bool
alt/error_expr_not_ok_alt6.nit:149,7--24: Type error: expected A, got String
alt/error_expr_not_ok_alt6.nit:150,7--18: Warning: Expression is already a Int.
alt/error_expr_not_ok_alt6.nit:150,7--18: Type error: expected A, got Int
+alt/error_expr_not_ok_alt6.nit:151,7--23: Warning: expression is already not null, since it is a `Int`.
alt/error_expr_not_ok_alt6.nit:151,7--23: Type error: expected A, got Int
alt/error_expr_not_ok_alt6.nit:152,7--18: Warning: Expression is already a Int.
alt/error_expr_not_ok_alt6.nit:152,7--18: Type error: expected A, got Bool