From 7b720228a4f68672595bdd588789768f9b4e564e Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 4 May 2016 10:25:50 -0400 Subject: [PATCH 1/2] nitc/engines: do not initialize optional attributes on the allocation Signed-off-by: Jean Privat --- src/compiler/abstract_compiler.nit | 2 +- src/interpreter/naive_interpreter.nit | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 9982dd1af3..9811228b2c 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -3162,7 +3162,7 @@ redef class AAttrPropdef fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable) do - if has_value and not is_lazy and not n_expr isa ANullExpr then evaluate_expr(v, recv) + if has_value and not is_lazy and not is_optional and not n_expr isa ANullExpr then evaluate_expr(v, recv) end # Evaluate, store and return the default value of the attribute diff --git a/src/interpreter/naive_interpreter.nit b/src/interpreter/naive_interpreter.nit index 99f235602f..539fb89a9b 100644 --- a/src/interpreter/naive_interpreter.nit +++ b/src/interpreter/naive_interpreter.nit @@ -1514,7 +1514,7 @@ redef class AAttrPropdef # Evaluate and set the default value of the attribute in `recv` private fun init_expr(v: NaiveInterpreter, recv: Instance) do - if is_lazy then return + if is_lazy or is_optional then return if has_value then var f = v.new_frame(self, mreadpropdef.as(not null), [recv]) evaluate_expr(v, recv, f) From fdeeac9fbf527bad84e488825e8afd60fe49e82a Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 4 May 2016 10:27:17 -0400 Subject: [PATCH 2/2] tests: add base_is_optional to check the evaluation order of attribute values Signed-off-by: Jean Privat --- tests/base_is_optional.nit | 68 +++++++++++++++++++++++++++++ tests/sav/base_is_optional.res | 33 ++++++++++++++ tests/sav/base_is_optional_alt1.res | 30 +++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 tests/base_is_optional.nit create mode 100644 tests/sav/base_is_optional.res create mode 100644 tests/sav/base_is_optional_alt1.res diff --git a/tests/base_is_optional.nit b/tests/base_is_optional.nit new file mode 100644 index 0000000000..ea13e28b8e --- /dev/null +++ b/tests/base_is_optional.nit @@ -0,0 +1,68 @@ +# 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 core::kernel + +fun foo(i: Int): Int +do + 'f'.output + i.output + return i +end + +class A + # needed on the new + var i: Int + + # initialized by the allocation + var j: Int = foo(2) + + # optional in the new, default value evaluated if `null` is given + var k: Int = foo(3) is optional + + # the `init` will initialize it + var l: Int is noautoinit + init do l = foo(4) + + # initialized if needed on the first `read` + var m: Int = foo(5) is lazy + + fun set + do + i = 10 + j = 20 + k = 30 + l = 40 + m = 50 + end + + fun test + do + #alt1#set + i.output + j.output + k.output + l.output + m.output + '\n'.output + end +end + +var a +a = new A(foo(100)) +a.test +a = new A(foo(100), null) +a.test +a = new A(foo(100), foo(300)) +a.test diff --git a/tests/sav/base_is_optional.res b/tests/sav/base_is_optional.res new file mode 100644 index 0000000000..3f89579dbd --- /dev/null +++ b/tests/sav/base_is_optional.res @@ -0,0 +1,33 @@ +f2 +f100 +f3 +f4 +100 +2 +3 +4 +f5 +5 + +f2 +f100 +f3 +f4 +100 +2 +3 +4 +f5 +5 + +f2 +f100 +f300 +f4 +100 +2 +300 +4 +f5 +5 + diff --git a/tests/sav/base_is_optional_alt1.res b/tests/sav/base_is_optional_alt1.res new file mode 100644 index 0000000000..12d180697c --- /dev/null +++ b/tests/sav/base_is_optional_alt1.res @@ -0,0 +1,30 @@ +f2 +f100 +f3 +f4 +10 +20 +30 +40 +50 + +f2 +f100 +f3 +f4 +10 +20 +30 +40 +50 + +f2 +f100 +f300 +f4 +10 +20 +30 +40 +50 +