From 202acea3154f4314323b27cead1ab1d33ab7b429 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Fri, 3 Jul 2009 17:34:11 -0400 Subject: [PATCH] syntax: move varctx initialization to the PClassdef variable 'self' can now be accessed (even implicitly) in default attribute definition. A test base_attr_init_val3 is added. Signed-off-by: Jean Privat --- src/syntax/typing.nit | 12 +++++++++--- tests/base_attr_init_val3.nit | 32 ++++++++++++++++++++++++++++++++ tests/sav/base_attr_init_val3.sav | 2 ++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/base_attr_init_val3.nit create mode 100644 tests/sav/base_attr_init_val3.sav diff --git a/src/syntax/typing.nit b/src/syntax/typing.nit index 7730152..738fa7a 100644 --- a/src/syntax/typing.nit +++ b/src/syntax/typing.nit @@ -151,6 +151,8 @@ end redef class PClassdef redef fun accept_typing(v) do + v.variable_ctx = new RootVariableContext(v, self) + v.base_variable_ctx = v.variable_ctx v.self_var = new ParamVariable("self".to_symbol, self) v.self_var.stype = local_class.get_type super @@ -160,10 +162,13 @@ end redef class AAttrPropdef redef fun accept_typing(v) do + var old_var_ctx = v.variable_ctx + v.variable_ctx = old_var_ctx.sub(self) super if n_expr != null then v.check_conform_expr(n_expr.as(not null), prop.signature.return_type.as(not null)) end + v.variable_ctx = old_var_ctx end end @@ -172,15 +177,16 @@ redef class AMethPropdef var _self_var: nullable ParamVariable redef fun accept_typing(v) do - v.variable_ctx = new RootVariableContext(v, self) - v.base_variable_ctx = v.variable_ctx + var old_var_ctx = v.variable_ctx + v.variable_ctx = old_var_ctx.sub(self) _self_var = v.self_var super + v.variable_ctx = old_var_ctx end end redef class AConcreteMethPropdef - redef fun accept_typing(v) + redef fun after_typing(v) do super if v.variable_ctx.unreash == false and method.signature.return_type != null then diff --git a/tests/base_attr_init_val3.nit b/tests/base_attr_init_val3.nit new file mode 100644 index 0000000..a01ae58 --- /dev/null +++ b/tests/base_attr_init_val3.nit @@ -0,0 +1,32 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2009 Jean Privat +# +# 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 A + readable var _i: Int = foo + fun foo: Int do return 1 + init do end +end + +class B + readable var _a: A = bar + fun bar: A do return new A + init do end +end + +(new A).i.output +(new B).a.i.output diff --git a/tests/sav/base_attr_init_val3.sav b/tests/sav/base_attr_init_val3.sav new file mode 100644 index 0000000..6ed281c --- /dev/null +++ b/tests/sav/base_attr_init_val3.sav @@ -0,0 +1,2 @@ +1 +1 -- 1.7.9.5