Skip to content

Commit 573cc47

Browse files
Properly implements the alias resolution - fix tests
1 parent 572a083 commit 573cc47

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

vm/src/hint_processor/builtin_hint_processor/hint_utils.rs

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,75 @@ pub fn get_reference_from_var_name<'a>(
175175
}
176176

177177
pub fn get_constant_from_var_name<'a>(
178-
var_name: &'static str,
178+
var_name: &str,
179179
identifiers: &'a HashMap<String, Identifier>,
180180
accessible_scopes: &[String],
181181
) -> Result<&'a Felt252, HintError> {
182-
accessible_scopes
183-
.iter()
184-
.rev()
185-
.find_map(|scope| {
186-
let identifier = identifiers.get(&format!("{}.{}", scope, var_name))?;
187-
if identifier.type_.as_ref()? == "const" {
188-
identifier.value.as_ref()
189-
} else {
190-
None
182+
for scope in accessible_scopes.iter().rev() {
183+
let full_path = format!("{}.{}", scope, var_name);
184+
let identifier = identifiers.get(&full_path);
185+
186+
let Some(identifier) = identifier else {
187+
continue;
188+
};
189+
190+
let identifier_type = identifier
191+
.type_
192+
.as_ref()
193+
.ok_or_else(|| HintError::MissingConstant(Box::new(var_name.to_string())))?;
194+
195+
match &identifier_type[..] {
196+
"const" => {
197+
return identifier
198+
.value
199+
.as_ref()
200+
.ok_or_else(|| HintError::MissingConstant(Box::new(var_name.to_string())))
201+
}
202+
"alias" => {
203+
let destination = identifier
204+
.destination
205+
.as_ref()
206+
.ok_or_else(|| HintError::MissingConstant(Box::new(var_name.to_string())))?;
207+
208+
return get_constant_from_alias(destination, identifiers);
191209
}
192-
})
193-
.ok_or_else(|| HintError::MissingConstant(Box::new(var_name)))
210+
_ => return Err(HintError::MissingConstant(Box::new(var_name.to_string()))),
211+
}
212+
}
213+
214+
Err(HintError::MissingConstant(Box::new(var_name.to_string())))
215+
}
216+
217+
pub fn get_constant_from_alias<'a>(
218+
destination: &str,
219+
identifiers: &'a HashMap<String, Identifier>,
220+
) -> Result<&'a Felt252, HintError> {
221+
let identifier = identifiers
222+
.get(destination)
223+
.ok_or_else(|| HintError::MissingConstant(Box::new(destination.to_string())))?;
224+
225+
let identifier_type = identifier
226+
.type_
227+
.as_ref()
228+
.ok_or_else(|| HintError::MissingConstant(Box::new(destination.to_string())))?;
229+
230+
match &identifier_type[..] {
231+
"const" => identifier
232+
.value
233+
.as_ref()
234+
.ok_or_else(|| HintError::MissingConstant(Box::new(destination.to_string()))),
235+
"alias" => {
236+
let destination = identifier
237+
.destination
238+
.as_ref()
239+
.ok_or_else(|| HintError::MissingConstant(Box::new(destination.to_string())))?;
240+
241+
get_constant_from_alias(destination, identifiers)
242+
}
243+
_ => Err(HintError::MissingConstant(Box::new(
244+
destination.to_string(),
245+
))),
246+
}
194247
}
195248

196249
#[cfg(test)]

vm/src/vm/errors/hint_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub enum HintError {
4545
#[error("Hint Error: {0}")]
4646
CustomHint(Box<str>),
4747
#[error("Missing constant: {0}")]
48-
MissingConstant(Box<&'static str>),
48+
MissingConstant(Box<String>),
4949
#[error("Fail to get constants for hint execution")]
5050
FailedToGetConstant,
5151
#[error("Arc too big, {} must be <= {} and {} <= {}", (*.0).0, (*.0).1, (*.0).2, (*.0).3)]

0 commit comments

Comments
 (0)