===================================================================
@@ -5979,6 +5979,8 @@ gfc_match_end (gfc_statement *st)
const char *target;
int eos_ok;
match m;
+ gfc_namespace *parent_ns, *ns, *prev_ns;
+ gfc_namespace **nsp;
old_loc = gfc_current_locus;
if (gfc_match ("end") != MATCH_YES)
@@ -6204,6 +6206,35 @@ syntax:
cleanup:
gfc_current_locus = old_loc;
+
+ /* If we are missing an END BLOCK, we created a half-ready namespace.
+ Remove it from the parent namespace's sibling list. */
+
+ if (state == COMP_BLOCK)
+ {
+ parent_ns = gfc_current_ns->parent;
+
+ nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
+
+ prev_ns = NULL;
+ ns = *nsp;
+ while (ns)
+ {
+ if (ns == gfc_current_ns)
+ {
+ if (prev_ns == NULL)
+ *nsp = NULL;
+ else
+ prev_ns->sibling = ns->sibling;
+ }
+ prev_ns = ns;
+ ns = ns->sibling;
+ }
+
+ gfc_free_namespace (gfc_current_ns);
+ gfc_current_ns = parent_ns;
+ }
+
return MATCH_ERROR;
}
===================================================================
@@ -4291,6 +4291,7 @@ parse_module (void)
{
gfc_statement st;
gfc_gsymbol *s;
+ bool error;
s = gfc_get_gsymbol (gfc_new_block->name);
if (s->defined || (s->type != GSYM_UNKNOWN && s->type != GSYM_MODULE))
@@ -4304,6 +4305,7 @@ parse_module (void)
st = parse_spec (ST_NONE);
+ error = false;
loop:
switch (st)
{
@@ -4322,12 +4324,15 @@ loop:
gfc_error ("Unexpected %s statement in MODULE at %C",
gfc_ascii_statement (st));
+ error = true;
reject_statement ();
st = next_statement ();
goto loop;
}
- s->ns = gfc_current_ns;
+ /* Make sure not to free the namespace twice on error. */
+ if (!error)
+ s->ns = gfc_current_ns;
}