🐛 fix(mq-lang): improve NotDefined error token handling in eval.rs#1289
🐛 fix(mq-lang): improve NotDefined error token handling in eval.rs#1289
Conversation
harehare
commented
Feb 17, 2026
- Use func_name.token if available, fallback to get_token for better error reporting
- Also improve token handling for module_path in NotDefined error
- Use func_name.token if available, fallback to get_token for better error reporting - Also improve token handling for module_path in NotDefined error
There was a problem hiding this comment.
Pull request overview
Improves runtime diagnostics in mq-lang by attaching more accurate source tokens to RuntimeError::NotDefined when evaluating qualified module access, so error spans better match the user’s code.
Changes:
- Prefer
func_name.token(when present) overtoken_idforNotDefinederrors in qualified module function calls. - Improve
NotDefinedtoken selection for non-module values by using the lastmodule_pathsegment’s token/name when available.
| .token | ||
| .as_ref() | ||
| .cloned() | ||
| .unwrap_or(get_token(Shared::clone(&self.token_arena), token_id)); |
There was a problem hiding this comment.
unwrap_or(get_token(...)) eagerly calls get_token even when func_name.token is Some, which defeats the purpose of preferring the provided token and does unnecessary arena cloning/indexing. Use unwrap_or_else(|| get_token(...)) so the fallback token is only fetched when needed.
| .unwrap_or(get_token(Shared::clone(&self.token_arena), token_id)); | |
| .unwrap_or_else(|| get_token(Shared::clone(&self.token_arena), token_id)); |
| .last() | ||
| .map(|m| (m.token.clone(), m.name.to_string())) | ||
| .unwrap_or_default(); | ||
| let token = token.unwrap_or(get_token(Shared::clone(&self.token_arena), token_id)); |
There was a problem hiding this comment.
token.unwrap_or(get_token(...)) eagerly evaluates the fallback get_token(...) even when token is already Some. Prefer unwrap_or_else(|| get_token(...)) to avoid unnecessary arena cloning/indexing when the module path already carries a token.
| let token = token.unwrap_or(get_token(Shared::clone(&self.token_arena), token_id)); | |
| let token = | |
| token.unwrap_or_else(|| get_token(Shared::clone(&self.token_arena), token_id)); |
| .as_ref() | ||
| .cloned() | ||
| .unwrap_or(get_token(Shared::clone(&self.token_arena), token_id)); | ||
| Err(RuntimeError::NotDefined((*token).clone(), func_name.name.to_string()).into()) |
There was a problem hiding this comment.
This change alters which token is attached to RuntimeError::NotDefined for qualified module calls; there currently don't appear to be integration tests covering import ... | module::missing() / module::missing_ident cases. Add a regression test asserting the diagnostic points at the missing member token (and module path token where applicable) so future refactors don’t regress error location quality.