From f55bd10cb4b5ab0e9e6b2af0e4dce063786d9658 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Fri, 6 Aug 2021 14:16:30 +0200 Subject: [PATCH] :art: retrict usable types in global class --- src/parse/scan1.c | 29 ++++++++++++++++++- tests/error/not_global_inside_global_class.gw | 7 +++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/error/not_global_inside_global_class.gw diff --git a/src/parse/scan1.c b/src/parse/scan1.c index cc661dfc..2877a997 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -35,10 +35,37 @@ ANN static inline m_bool ensure_scan1(const Env env, const Type t) { return envset_run(&es, t); } +ANN static inline bool from_global_nspc(const Env env, const Nspc nspc) { + Nspc global = env->global_nspc; + while(global) { + if (nspc == global) + return true; + global = global->parent; + } + return false; +} + +ANN static m_bool check_global(const Env env, const Type t, const loc_t pos) { + const struct ValueFrom_ *from = t->info->value->from; + if(from->owner_class && isa(from->owner_class, env->class_def) > 0) + return true; + if(!GET_FLAG(t, global) || !from_global_nspc(env, from->owner)) { + gwerr_basic("can't use non-global type in a global class", NULL, NULL, env->name, pos, 0); + gwerr_secondary("not declared global", from->filename, from->loc); + const struct ValueFrom_ *ownerFrom = env->class_def->info->value->from; + gwerr_secondary("is global", ownerFrom->filename, ownerFrom->loc); + return false; + } + return true; +} + ANN static Type scan1_type(const Env env, Type_Decl *td) { DECL_OO(const Type, t, = known_type(env, td)); - if (!env->func && env->class_def && !GET_FLAG(td, late)) + if (!env->func && env->class_def && !GET_FLAG(td, late)) { CHECK_BO(type_cyclic(env, t, td)); + if(GET_FLAG(env->class_def, global) && !check_global(env, t, td->pos)) + return NULL; + } CHECK_BO(ensure_scan1(env, t)); return t; } diff --git a/tests/error/not_global_inside_global_class.gw b/tests/error/not_global_inside_global_class.gw new file mode 100644 index 00000000..6a5a8f19 --- /dev/null +++ b/tests/error/not_global_inside_global_class.gw @@ -0,0 +1,7 @@ +#! [contains] non-global type in a global class + +class State{} + +class global States extends State[16] { + var State current; +} -- 2.43.0