\documentclass{article}
\usepackage{soul}
\usepackage{color}
\begin{document}
Some \hl{te\discretionary{-}{-}{-}xt}.
\end{document}
The above does not compile due to Missing { inserted, Improper discretionary list, Incompatible list can't be unboxed and Improper \prevdepth errors. Removing \hl or \discretionary removes all errors.
How can I fix the code to allow nesting of \discretionary in soul’s \hl?
Update:
I would like to locally redefine \discretionary to return just its third argument, as the original command would not work within soul’s macros. I managed to go up to this point:
\documentclass{article}
\usepackage{soul}
\usepackage[usenames,dvipsnames]{xcolor}%\usepackage{color}
%https://tex.stackexchange.com/a/139500:
\soulregister\cite7
\soulregister\ref7
\soulregister\pageref7
\def\highlightcolor{yellow}
\DeclareRobustCommand{\hlc}[2][\highlightcolor]{{\sethlcolor{#1}\hl{{\renewcommand*\discretionary[3]{\mbox{####3}}#2}}}}
\begin{document}
\hlc{Some,te\discretionary{-}{-}{-}xt}.
\end{document}
However, this still throws error if highlighted text contains spaces. I think it is caused by double braces in \hl{{…}}, but I was unable to redefine \discretionary within \hl’s scope in other ways. Are there some alternatives to allow text highlighting without removing \discretionary commands – I have quite long pieces of text containing such commands that I need to highlight, but in the final version the highlighting is to be removed. So I do not want to remove \discretionary commands, but just to locally redefine them to something not causing errors when highlighting is in use. Returning just its third arguments would be enough for me.

souluses\discretionaryfor its inner working, so redefining it is out of question. – egreg Mar 11 '19 at 18:55