<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/106705>106705</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang][Semantics] Semantic error in Fortran code due to recursive reference in specification expression
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          inaki-amatria
      </td>
    </tr>
</table>

<pre>
    - `flang-new` version is: `flang-new version 20.0.0git (https://github.com/llvm/llvm-project.git 46fe36a4295f05d5d3731762e31fc4e6e99863e9)`
- Reduced test case is:
```fortran
! foo.f90

module foo
  real :: qux(10, 10, 10)

  interface
    module subroutine bar(i)
    end
 module function baz()
    end
  end interface
end

submodule (foo) foo_impl
  contains
    module procedure bar
      qux(i, i, i) = baz()
    end
    module procedure baz
 end
end
```
- `flang-new` invocation is: `flang-new foo.f90`
- Expected behavior is: The code compiles sucessfully without errors, as `baz` should be accessible through host association in `bar`.
- Actual behavior: Compilation fails with the following error:
```txt
error: Semantic errors in /foo.f90
foo.f90:17:22: error: The module function 'baz' may not be referenced recursively in a specification expression
        qux(i, i, i) = baz()
                       ^^^^^
```
- Additional information: Reordering the definitions of the procedures in the submodule seems to work around the issue for now.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVduS2ygTfhp802UVQgdLF7pw4vgB8v_3WwhaFhsEXg72TJ5-Cx3szGSmtjKlYgx94OuvP4B7ry4GsSPVF1KddjyG0bpOGf5D7fnEg1N811v52u2B1HTQ3Fz2Bu-kpnBD55U1oDwpjm-sDxOjGc3oRQUgrBlDuCZXws6EnS8qjLHPhJ0IO2t92_7tr87-jSJkKaqsByxqXrK2GmglK1kcivxQMyzyQZRYY9s2dYEtYS2pKaEnQo97-I4yCpQQ0AcQ3OMCcTEnv_kbrAuOm3WV5TBYmw3tmmUZJyujxmRZ5gAO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