From 7b3082da29497cde2ce551967a6de6557a195708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Umut=20Yi=C4=9Fit=20Dural?= Date: Wed, 17 Dec 2025 03:08:52 +0100 Subject: [PATCH] annotations: fix shadowing issue --- src/front/resolve.rs | 18 +++++++++++++----- src/tyctx.rs | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/front/resolve.rs b/src/front/resolve.rs index d146ae08..64db83f8 100644 --- a/src/front/resolve.rs +++ b/src/front/resolve.rs @@ -3,6 +3,8 @@ //! in this place. Resolution is therefore concerned with creating and resolving //! variable scopes. +use std::rc::Rc; + use ariadne::ReportKind; use crate::{ @@ -215,15 +217,21 @@ impl<'tcx> VisitorMut for Resolve<'tcx> { self.with_subscope(|this| this.visit_block(block)) } StmtKind::Annotation(_, ref mut ident, ref mut args, ref mut inner_stmt) => { - self.visit_ident(ident)?; - - match self.tcx.get(*ident).as_deref() { - None => {} // Declaration not found + // Resolve the annotation ident to the declaration + // Here we only look through annotation declarations therefore other declarations do not shadow annotations + match self + .tcx + .get_statement_annotations() + .get(&ident.name) + .map(Rc::as_ref) + { Some(DeclKind::AnnotationDecl(intrin)) => { + *ident = intrin.name(); // Resolve to the declaration ident let intrin = intrin.clone(); // clone so we can mutably borrow self intrin.resolve(self, s.span, args)?; } - _ => {} // Not an annotation declaration + _ => return Err(ResolveError::NotFound(*ident)), // Either not declared or not declared as an annotation, + // This case is same as None since we only look through annotations } self.with_subscope(|this| this.visit_stmt(inner_stmt)) diff --git a/src/tyctx.rs b/src/tyctx.rs index 2352914d..9dba6e66 100644 --- a/src/tyctx.rs +++ b/src/tyctx.rs @@ -9,7 +9,7 @@ use std::{ rc::Rc, }; -use crate::ast::FuncDecl; +use crate::{ast::FuncDecl, intrinsic::annotations::AnnotationKind}; use crate::{ ast::{DeclKind, DeclRef, DomainDecl, Ident, LitKind, Span, Symbol, TyKind, VarKind}, intrinsic::distributions::DistributionProc, @@ -157,6 +157,19 @@ impl TyCtx { } } + /// Get all annotation declarations that can be used on top of statements. These are slicing and encoding annotations. + pub fn get_statement_annotations(&self) -> IndexMap> { + IndexMap::from_iter(self.declarations.borrow().iter().flat_map(|(ident, decl)| { + if let DeclKind::AnnotationDecl( + AnnotationKind::Slicing(_) | AnnotationKind::Encoding(_), + ) = decl.deref() + { + return Some((ident.name, decl.clone())); + } + None + })) + } + pub fn get_distributions(&self) -> IndexMap> { IndexMap::from_iter(self.declarations.borrow().iter().flat_map(|(ident, decl)| { if let DeclKind::ProcIntrin(intrin) = decl.deref() {