-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: improve error message for local memory index bounds check #1640
base: next
Are you sure you want to change the base?
Conversation
return Err(AssemblyError::Other( | ||
Report::msg( | ||
format!( | ||
"invalid local memory index: {} is out of bounds for procedure with {} locals (valid range: 0..{})", | ||
index, num_proc_locals, max | ||
) | ||
) | ||
.into(), | ||
)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually already have the machinery to output very nice error messages (built on top of miette
). The desired solution would output an error variant that is similar to InvalidSyscallTarget
(in that it takes a SourceSpan
and a SourceFile
).
@plafer hi Philippe! made some changes, what do you think abt them? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some suggestions on how to fix the problems.
Could you also fix the merge conflicts by merging in the next
branch?
Also please make sure that the code compiles, as well as removing any compiler warnings (i.e. cargo check
should not print any warning).
Thank you!
span: block_builder.get_current_span(), | ||
source_file: block_builder.get_source_file(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This currently doesn't compile, right? These methods don't exist on BasicBlockBuilder
.
Let rewrite the method to:
pub fn local_to_absolute_addr(
block_builder: &mut BasicBlockBuilder,
index: u16,
proc_ctx: &ProcedureContext,
) -> Result<(), AssemblyError> {
let num_proc_locals = proc_ctx.num_locals();
if num_proc_locals == 0 {
return Err(AssemblyError::Other(
Report::msg(
"number of procedure locals was not set (or set to 0), but local values were used"
.to_string(),
)
.into(),
));
}
let max_index = num_proc_locals - 1;
if index > max_index {
let span = proc_ctx.span();
return Err(AssemblyError::InvalidLocalMemoryIndex {
span,
source_file: proc_ctx.source_manager().get(span.source_id()).ok(),
index,
num_locals: num_proc_locals,
max_index,
});
}
push_felt(block_builder, -Felt::from(max_index - index));
block_builder.push_op(FmpAdd);
Ok(())
}
and adjust every caller to pass in the ProcedureContext
instead of num_proc_locals
assembly/src/errors.rs
Outdated
#[error("invalid local word index: {local_addr}")] | ||
#[diagnostic()] | ||
InvalidLocalWordIndex { | ||
#[label] | ||
span: SourceSpan, | ||
#[source_code] | ||
source_file: Option<Arc<SourceFile>>, | ||
local_addr: u16, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove this as it's unused
assembly/src/errors.rs
Outdated
source_file: Option<Arc<SourceFile>>, | ||
index: u16, | ||
num_locals: u16, | ||
max: u16, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
max: u16, | |
max_index: u16, |
assembly/src/errors.rs
Outdated
#[error("invalid local memory index: {index} is out of bounds (valid range: 0..{max})")] | ||
#[diagnostic(help("procedure has {num_locals} locals available"))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[error("invalid local memory index: {index} is out of bounds (valid range: 0..{max})")] | |
#[diagnostic(help("procedure has {num_locals} locals available"))] | |
#[error("invalid local memory index: {index} is out of bounds")] | |
#[diagnostic(help("procedure has {num_locals} locals available, so the valid range for the index is 0..{max_index}"))] |
@plafer made an update |
@crStiv this branch still doesn't compile (i.e. make sure |
Description:
Example of new error message:
"invalid local memory index: 5 is out of bounds for procedure with 3 locals (valid range: 0..2)"
Fixes #1619