This patch is about error diagnostic + an ICE for invalid code. Before the patch, gfortran/f951 showed: ... Error: 'ancestor' device modifier not preceded by 'requires' directive with 'reverse_offload' clause 18 | end block | 1 Error: Expecting END PROGRAM statement at (1) gfortran: internal compiler error: Segmentation fault signal terminated program f951 * The first error is for a user error and fine. * A follow-up error is expected (due to the now dangling '!$omp end target'), but the error location and wording is misleading * And the ICE is plainly wrong. With the patch, there is no ICE and the the second error reads: 16 | !$omp end target Error: Unexpected !$OMP END TARGET statement at (1) The problem was due to nesting '!$omp target' (= same directive), where the outer one was a strictly structured block - and parsing the inner '!$omp target' failed with MATCH_ERROR, which ignored that line - while '!$omp end target' remained. (So far, so good, but then the parsing-code did run into a bug.) For the blocks, the following applies. OpenMP permits either * strictly structured blocks (with optional END_ST == 'end target') !$omp target block ... end block !$omp end target ! << this line is optional * loosely structured block !$omp target ... ! may not start with 'block' (and hence cannot end with 'end block') !$omp end target ! << required The parsing issue is in the following code, which first takes care of the 'strictly': 'end block' + optional 'end target' and then of the 'loosely structured' case with just: 'end target': else if (block_construct && st == ST_END_BLOCK) ... st = next_statement (); if (st == omp_end_st) accept_statement (st); ... else if (st != omp_end_st) { unexpected_statement (st); st = next_statement (); } The fix is to change the second if condition to: else if (st != omp_end_st || (block_construct && st == omp_end_st)) or rather to the following equivalent code: else if (st != omp_end_st || block_construct) OK for mainline and GCC 12?* Tobias *strictly structured blocks were added in r12-4592. ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955