Nameref circular reference causes TeX capacity exceeded

capacitycaptionsnameref

Update: Thanks everyone for your help, looks like I just misunderstood how \nameref works. I didn't realise it printed the entire caption, so as Ulrike commented it looks like the code is simply infinitely/iteratively expanding self-references. I'll need to find another package/command to achieve what I wanted. Sorry for my mistake! I'll accept Simon's answer and close the question.

I'm trying to insert some blocks of code into my LaTeX document (I'm using TeXStudio, in case that's relevant) using the minted package. I'd like to provide some exposition as to how these different files relate to each other in their captions, and to do that I refer to the second code block in the first code block's caption, and vice versa (i.e. there's a circle of references). I'm using an additional environment ("code") just so that long code breaks across multiple pages.

When I use the \ref command (which just gives the code block's/listing's number), everything works as expected. MWE of working code:

\documentclass{article}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\newenvironment{code}{\captionsetup{type=listing}}{}

\begin{document}


\begin{code}
    \captionof{listing}{Some reference to \ref{code:secondcode}.}
    \label{code:firstcode}
\end{code}

\begin{code}
    \captionof{listing}{Some reference to \ref{code:firstcode}.}
    \label{code:secondcode}
\end{code}

\end{document}

However, if I swap \ref out for \nameref, an error is thrown at compilation time:
line 15: TeX capacity exceeded, sorry [grouping levels=255]. …e reference to \nameref{code:secondcode}.}

MWE of non-working code:

\documentclass{article}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\newenvironment{code}{\captionsetup{type=listing}}{}

\begin{document}


\begin{code}
    \captionof{listing}{Some reference to \nameref{code:secondcode}.}
    \label{code:firstcode}
\end{code}

\begin{code}
    \captionof{listing}{Some reference to \nameref{code:firstcode}.}
    \label{code:secondcode}
\end{code}

\end{document}

I'm happy to work around this somehow, but I'm not sure how, and solving the problem in the nameref subpackage is also beyond my LaTeX skills. I'm not sure whether it's in any way related to the caption or listing packages.

Any suggestions?

EDIT: Forgot to say – the code also works fine when either one of the \namerefs is removed, so it's somehow related to the circular referencing.

simpler example, just loading nameref

\documentclass{article}

\usepackage{nameref}

\begin{document}


\begin{figure}
    \caption{Some reference to \nameref{code:secondcode}.}
    \label{code:firstcode}
\end{figure}

\begin{figure}
    \caption{Some reference to \nameref{code:firstcode}.}
    \label{code:secondcode}
\end{figure}

\end{document}

Best Answer

Use \Cref instead of \nameref.

\documentclass{article}
\usepackage{minted}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\newenvironment{code}{\captionsetup{type=listing}}{}

\begin{document}
    
    
    \begin{code}
        \captionof{listing}{Some reference to \Cref{code:secondcode}.}
        \label{code:firstcode}
    \end{code}
    
    \begin{code}
        \captionof{listing}{Some reference to \Cref{code:firstcode}.}
        \label{code:secondcode}
    \end{code}
    
\end{document}

a