https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119054
Bug ID: 119054 Summary: ICE on passing optional array to elemental procedure with -pedantic Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: zed.three at gmail dot com Target Milestone: --- The following valid code triggers an ICE when compiled with -pedantic: program elemental_optional implicit none contains function outer(o) result(l) integer, intent(in), optional :: o(:) integer :: l(5) l = inner(o, [1,2,3,4,5]) end function outer elemental function inner(a,b) result(x) integer, intent(in), optional :: a integer, intent(in) :: b integer :: x if(present(a)) then x = a*b else x = b endif end function inner end program elemental_optional f951: internal compiler error: Segmentation fault 0x1f3390c internal_error(char const*, ...) ???:0 0x84e037 gfc_resolve_code(gfc_code*, gfc_namespace*) ???:0 0x82e210 gfc_parse_file() ???:0 Please submit a full bug report, with preprocessed source (by using -freport-bug). Please include the complete backtrace with any bug report. See <https://gcc.gnu.org/bugs/> for instructions. Compiler returned: 1 Note that this requires passing one optional array and one expression that isn't a dummy argument. The offending code is in `resolve_elemental_actual` in `resolve.cc`: /* Scan the argument list for a non-optional argument with the same rank as arg. */ for (a = arg0; a; a = a->next) if (a != arg && a->expr->rank == arg->expr->rank && !a->expr->symtree->n.sym->attr.optional) { t = true; break; } `a->expr->symtree` is NULL for array literals. My proposed fix is to only check for the `optional` attribute for variables: /* Scan the argument list for a non-optional argument with the same rank as arg. */ for (a = arg0; a; a = a->next) if (a != arg && a->expr->rank == arg->expr->rank && (a->expr->expr_type != EXPR_VARIABLE || (a->expr->expr_type == EXPR_VARIABLE && !a->expr->symtree->n.sym->attr.optional))) { t = true; break; } The ICE occurs for versions 11.1+ (when the check was added)