https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98016
--- Comment #5 from kargl at gcc dot gnu.org --- Index: gcc/gcc/fortran/expr.c =================================================================== --- gcc/gcc/fortran/expr.c (revision 280157) +++ gcc/gcc/fortran/expr.c (working copy) @@ -3380,24 +3382,41 @@ check_restricted (gfc_expr *e) This mechanism also does the same for the specification expressions of array-valued functions. */ if (e->error - || sym->attr.in_common - || sym->attr.use_assoc - || sym->attr.dummy - || sym->attr.implied_index - || sym->attr.flavor == FL_PARAMETER - || is_parent_of_current_ns (sym->ns) - || (sym->ns->proc_name != NULL - && sym->ns->proc_name->attr.flavor == FL_MODULE) - || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns))) + || sym->attr.in_common + || sym->attr.use_assoc + || sym->attr.dummy + || sym->attr.implied_index + || sym->attr.flavor == FL_PARAMETER + || is_parent_of_current_ns (sym->ns) + || (sym->ns->proc_name != NULL + && sym->ns->proc_name->attr.flavor == FL_MODULE) + || (gfc_is_formal_arg () && (sym->ns == gfc_current_ns))) { t = true; break; } - gfc_error ("Variable %qs cannot appear in the expression at %L", - sym->name, &e->where); - /* Prevent a repetition of the error. */ - e->error = 1; + for (gfc_namespace *ns = sym->ns->parent; ns; ns = ns->parent) + if (sym->ns == ns) + { + t = true; + break; + } + + for (gfc_namespace *ns = sym->ns->contained->parent; ns; ns = ns->parent) + if (sym->ns == ns) + { + t = true; + break; + } + + if (!t) + { + gfc_error ("Variable %qs cannot appear in the expression at %L", + sym->name, &e->where); + /* Prevent a repetition of the error. */ + e->error = 1; + } break; case EXPR_NULL: