Missed Variant type errors can lead to hard-to-debug runtime crashes

Just to share something I learned today:

from std.collections import List
from std.utils import Variant

comptime EvalResult = Variant[List[Int]]

def _first_value(result: EvalResult) → EvalResult:
var r = result
ref nodes = r[List[Int]]
if len(nodes) > 0:
return EvalResult(nodes[0]) # This is a type error
return
result

def main() raises:
# Intentionally do not call `_first_value`; keeps the type error latent.
pass

The code above has a typing error: nodes[0] is a single Int which is not a member of the Variant.
I would expect the Mojo compiler to detect this, but when _first_value is not called, it happily accepts the latent issue.

In the full application, the code containing the type error is live but called indirectly - the compiler misses the type error, and it causes random runtime crashes that are hard to debug.

Thanks for reporting the bug!
There seems to be a tracking bug here: [BUG] SIGSEGV (exit 139) running minimal XPath-style evaluator repro — crash with STRING-only AST when load-bearing visit_call + Variant/Arc code is present · Issue #6401 · modular/modular · GitHub (which looks similar to your issue).
What’s odd is there are cases where you get the expected constrain compile time error, but in your case that is not manifesting :thinking:

Yes, that is the original bug I reported. I now found it’s triggered by a “hidden” type error.

It’s an insidious issue that’s been blocking me from exploring Mojo further over the past few weeks. As a user I trust the compiler to enforce strict typing, as promised. I hope the Mojo team can acknowledge the urgency of fixing this