From fa6e20f118542cc8852590a5215e98bf585164db Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 25 Mar 2025 11:52:25 +1300 Subject: Improve error reporting for macros invoked before definition These errors were only being correctly reported when the invocation was inside a macro body. The error report has been expanded to also report the location of the eventual matching macro definition. --- src/stages/semantic.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/stages/semantic.rs') diff --git a/src/stages/semantic.rs b/src/stages/semantic.rs index da804ec..8b5f4f4 100644 --- a/src/stages/semantic.rs +++ b/src/stages/semantic.rs @@ -64,9 +64,10 @@ pub fn parse_semantic(syntactic: Vec>) -> Result { if let Some(definition) = definitions.get_mut(&symbol) { definition.value.deep_references.push((i, j)); - } else if macro_names.contains(&symbol) { + } else if let Some(definition) = macro_names.get(&symbol) { let error = SemanticError::InvocationBeforeDefinition; - errors.push(Tracked::from(error, syn_token.source.clone())); + let source = syn_token.source.wrap(definition.source.clone()); + errors.push(Tracked::from(error, source)); } else { unreachable!("Uncaught undefined symbol '{symbol}'"); }; @@ -106,10 +107,15 @@ pub fn parse_semantic(syntactic: Vec>) -> Result SemanticToken::RawValue(value), SyntacticToken::Instruction(instruction) => SemanticToken::Instruction(instruction), SyntacticToken::Invocation(symbol) => { - let Some(definition) = definitions.get_mut(&symbol) else { + if let Some(definition) = definitions.get_mut(&symbol) { + definition.value.references.push(i); + } else if let Some(definition) = macro_names.get(&symbol) { + let error = SemanticError::InvocationBeforeDefinition; + let source = syn_token.source.wrap(definition.source.clone()); + errors.push(Tracked::from(error, source)); + } else { unreachable!("Uncaught undefined symbol '{symbol}'"); }; - definition.value.references.push(i); SemanticToken::Invocation(symbol) } -- cgit v1.2.3-70-g09d2