TeXLive 2024, Linux. Here is the MWE. Compile (command line) with pdflatex and lualatex, to see the difference in behavior:
\documentclass{article}
% Using default font Latin Modern Roman at 10pt.
% Advance width of .notdef character is .28em =28.pt.
\tracinglostchars=3 % Immediately throw error.
\begin{document}
\sbox0{WHAT}
\typeout{Width of WHAT = \the\wd0}
\sbox0{WHAT\char"4444} % Throws missing character error with pdflatex, but not lualatex.
\typeout{Box width before typeset = \the\wd0} % WHAT+.notdef
\usebox0 % Throws missing character error in lualatex, via fontspec.
\end{document}
The \sbox0 command does notice the missing character, because it places the .notdef character there. In LMR, that character width is .28em, here 2.8pt.
Compiled with pdflatex, the error is immediately flagged at \sbox0, before the box is used. But with lualatex, the error is not flagged at \sbox0. Instead it is flagged by fontspec at \usebox0. If the box is never used (or re-written before it is used) then lualatex does not notice.
Is this intentional, a bug, or too unimportant?
I see that package adjustbox has macros for first typesetting, then discarding a box. But that seems like a lot of code for a small nuisance. We will not even discuss what pgf does!
How I discovered this: My workflow places certain metadata in a PDF, which is robo-read later. Each item of metadata is a single line (if typeset), so it can fit in a box earlier.
Users are required to limit certain metadata to a very small character set. I had thought that putting each line of metadata in a box, very early in compile, would immediately detect any forbidden character problems. This would be done using a custom font that has
only the allowed characters. But alas, I use only lualatex, and do not wish to typeset (then discard) each metadata box.
Not a big problem. I can parse the metadata by other means (I do that now). But I was curious as to why there was a behavioral difference.
fontspec? sofontspeccan't be the source of the error.fontspectag, because there is a difference between pdflatex (without fontspec) and lualatex (which always uses fontspec).lualatexformat.lualatex (which always uses fontspec).which you wrote andlualatex (with which I always use fontspec).which you meant, are not the same thing. No harm done though:-)