From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6476F3858C54; Mon, 19 Jun 2023 09:09:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6476F3858C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687165765; bh=uKLo2FGc8K/wb6Hfweh7HdnOpYuId8XB+w4u7y+G6SA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=vdfOkx8xwcQqJZAXNrF1z/YFjQPDnQRVEYkxYQZl+lQEUnkixrozqGFHmNtzBXAbY 43rOg1/6jd1/zPUWO+x4ggWhQqJpwuTtouW+meQu25AR49NzIAEudeTJcB3pzs7gBk f2Kp7J+M4GhzinyTAKsjTlhPhauZWvd/+AICyIVo= From: "mikael at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug fortran/92887] [F2008] Passing nullified/disassociated pointer or unalloc allocatable to OPTIONAL + VALUE dummy fails Date: Mon, 19 Jun 2023 09:09:25 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Version: 10.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: mikael at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D92887 Mikael Morin changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mikael at gcc dot gnu.org --- Comment #5 from Mikael Morin --- (In reply to anlauf from comment #4) >=20 > I'll need broader feedback, so unless someone adds to this pr, I'll submit > the present patch - with testcases - to get attention. >=20 Here you go: > diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc > index 45a984b6bdb..d9dcc11e5bd 100644 > --- a/gcc/fortran/trans-expr.cc > +++ b/gcc/fortran/trans-expr.cc > @@ -6396,7 +6399,28 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol *= sym, > && fsym->ts.type !=3D BT_CLASS > && fsym->ts.type !=3D BT_DERIVED) > { > - if (e->expr_type !=3D EXPR_VARIABLE > + /* F2018:15.5.2.12 Argument presence and > + restrictions on arguments not present. */ > + if (e->expr_type =3D=3D EXPR_VARIABLE > + && (e->symtree->n.sym->attr.allocatable > + || e->symtree->n.sym->attr.pointer)) Beware of expressions like derived%alloc_comp or derived%pointer_comp which don't match the above. > + { > + gfc_se argse; > + gfc_init_se (&argse, NULL); > + argse.want_pointer =3D 1; > + gfc_conv_expr (&argse, e); > + tmp =3D fold_convert (TREE_TYPE (argse.expr), > + null_pointer_node); > + tmp =3D fold_build2_loc (input_location, NE_EXPR, > + logical_type_node, > + argse.expr, tmp); > + vec_safe_push (optionalargs, > + fold_convert (boolean_type_node, > + tmp)); > + need_temp =3D true; > + cond_temp =3D tmp; > + } > + else if (e->expr_type !=3D EXPR_VARIABLE > || !e->symtree->n.sym->attr.optional > || e->ref !=3D NULL) > vec_safe_push (optionalargs, boolean_true_node); > @@ -7072,6 +7096,42 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol *= sym, > } > } >=20=20 > + /* F2023:15.5.3, 15.5.4: Actual argument expressions are evaluated > + before they are associated and a procedure is executed. */ > + if (e && e->expr_type !=3D EXPR_VARIABLE && !gfc_is_constant_expr = (e)) > + { > + /* Create temporary except for functions returning pointers that > + can appear in a variable definition context. */ Maybe explain *why* we have to create a temporary, that is some data refere= nces may become undefined by the procedure call (intent(out) dummies) so we hav= e to evaluate values depending on them beforehand (PR 92178). > + if (e->expr_type !=3D EXPR_FUNCTION > + || !(gfc_expr_attr (e).pointer || gfc_expr_attr (e).proc_pointer)) Merge with the outer condition? > + need_temp =3D true; > + } > + > + if (need_temp) > + { > + if (cond_temp =3D=3D NULL_TREE) > + parmse.expr =3D gfc_evaluate_now (parmse.expr, &parmse.pre); I'm not sure about this. The condition to set need_temp looks quite general (especially it matches non-scalar cases, doesn't it?), but gfc_conv_expr_reference should already take care of creating a variable, so that a temporary is missing only for value dummies, I think. I would rather move this to the place specific to value dummies. I think this PR is only about scalars with basic types, is there the same problem with derived types? with classes? I guess arrays are different as they are always by reference? > + else I would rather move the else part to the place above where cond_temp is set= , so that the code is easier to follow. > + { > + /* "Conditional temporary" to handle variables that possibly > + cannot be dereferenced. Use null value as fallback. */ > + tree dflt_temp; > + gcc_assert (e->ts.type !=3D BT_DERIVED && e->ts.type !=3D BT_CLAS= S); > + gcc_assert (e->rank =3D=3D 0); > + dflt_temp =3D gfc_create_var (TREE_TYPE (parmse.expr), "temp"); > + TREE_STATIC (dflt_temp) =3D 1; > + TREE_CONSTANT (dflt_temp) =3D 1; > + TREE_READONLY (dflt_temp) =3D 1; > + DECL_INITIAL (dflt_temp) =3D build_zero_cst (TREE_TYPE (dflt_temp= )); > + parmse.expr =3D fold_build3_loc (input_location, COND_EXPR, > + TREE_TYPE (parmse.expr), > + cond_temp, > + parmse.expr, dflt_temp); > + parmse.expr =3D gfc_evaluate_now (parmse.expr, &parmse.pre); > + } > + } > + > + > if (fsym && need_interface_mapping && e) > gfc_add_interface_mapping (&mapping, fsym, &parmse, e); >=