Hi Tobias, > On 10.06.21 20:52, Harald Anlauf via Fortran wrote: > > +static bool > > +substring_has_constant_len (gfc_expr *e) > > +{ > > + ptrdiff_t istart, iend; > > + size_t length; > > + bool equal_length = false; > > + > > + if (e->ts.type != BT_CHARACTER > > + || !e->ref > > + || e->ref->type != REF_SUBSTRING > > Is there a reason why you do not handle: > > type t > character(len=5) :: str1 > character(len=:), allocatable :: str2 > end type > type(t) :: x > > allocate(x%str2, source="abd") > if (len (x%str)) /= 1) ... > if (len (x%str2(1:2) /= 2) ... > etc. > > Namely: Search the last_ref = expr->ref->next->next ...? > and then check that lastref? I was assuming that the argument passed to LEN() is already the ultimate component for the case of substrings, and I was unable to find a case which requires implementing that iteration. The cases you provided do not seem to apply here: - derived type component str1, which is a string of given length, poses no problem. I added a case to the testcase, see attached updated patch. - derived type component str2 has deferred length. I do not see that the simplification can be applied here, as the allocation could lead to str2 being too short, and we do not want to simplify invalid code, such as: type t character(len=:), allocatable :: str2 end type type(t) :: x allocate(x%str2, source="z") if (len (x%str2(1:2)) /= 2) stop 1 end If we want this to be catchable by bounds checking, we need to punt at simplification of this. The updated patch skips deferred strings. > * * * > > Slightly unrelated: I think the following does not violate > F2018's R916 / C923 – but is rejected, namely: > R916 type-param-inquiry is designator % type-param-name > the latter is 'len' or 'kind' for intrinsic types. And: > R901 designator is ... > or substring > But > > character(len=5) :: str > print *, str(1:3)%len > end > > fails with > > 2 | print *, str(1:3)%len > | 1 > Error: Syntax error in PRINT statement at (1) > > > Assuming you don't want to handle it, can you open a new PR? > Thanks! Good point. I'd rather open a separate PR for this, though. > > + istart = gfc_mpz_get_hwi (e->ref->u.ss.start->value.integer); > > + iend = gfc_mpz_get_hwi (e->ref->u.ss.end->value.integer); > > + length = gfc_mpz_get_hwi (e->ref->u.ss.length->length->value.integer); > > + > > + if (istart <= iend) > > + { > > + if (istart < 1) > > + { > > + gfc_error ("Substring start index (%ld) at %L below 1", > > + (long) istart, &e->ref->u.ss.start->where); > > As mentioned by Bernhard, you could use HOST_WIDE_INT_PRINT_DEC. > > (It probably only matters on Windows which uses long == int = 32bit for > strings longer than INT_MAX.) I am not familiar enough with Windows. What is HOST_WIDE_INT on that system? (As compared to e.g. size_t, ptrdiff_t). The (slightly) updated patch regtests fine. Thanks, Harald