Skip to content
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

chore(ssa): Implement ir errors #2111

Closed
wants to merge 10 commits into from
Closed

Conversation

Ethan-000
Copy link
Contributor

Description

Problem*

towards #1473

Summary*

Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@Ethan-000 Ethan-000 marked this pull request as draft August 1, 2023 19:32
@jfecher
Copy link
Contributor

jfecher commented Aug 1, 2023

I don't think we should convert all panics in the compiler into Result errors. The SSA passes in particular are never supposed to error (unfortunately they sometimes do - this is always a bug). If an error is truly expected to be unreachable then I think it is fine to keep it as an expect/panic.

CC @TomAFrench, @phated for opinions here.

@Ethan-000
Copy link
Contributor Author

I don't think we should convert all panics in the compiler into Result errors. The SSA passes in particular are never supposed to error (unfortunately they sometimes do - this is always a bug). If an error is truly expected to be unreachable then I think it is fine to keep it as an expect/panic.

CC @TomAFrench, @phated for opinions here.

yeah it is not easy for me to decide which error should be returned and which ones should be leave as panic!() and unreachable!(). I changed errors in two files today and it require me to refactor the entire thing. not a very pleasant thing to do tbh

@TomAFrench
Copy link
Member

If an error is truly expected to be unreachable then I think it is fine to keep it as an expect/panic.

This is my opinion on errors vs panics (and is general rust best practices afaik), handling Results does cause extra development overhead so we should avoid doing so unnecessarily.

@TomAFrench
Copy link
Member

A lot of these errors seem to be something that a user can't trigger by themselves but would require us to have an incorrect compiler implementation so I think we can remove most/all of these.

}

/// Returns a mutable reference to the terminator of this block.
///
/// Once this block has finished construction, this is expected to always be Some.
pub(crate) fn unwrap_terminator_mut(&mut self) -> &mut TerminatorInstruction {
self.terminator.as_mut().expect("Expected block to have terminator instruction")
pub(crate) fn unwrap_terminator_mut(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"unwrap" implies we're not checking to make sure that the terminator exists so we can remove this error.

@Ethan-000
Copy link
Contributor Author

Ethan-000 commented Aug 2, 2023

I grouped it in a way that all Internal errors are due to an incorrect implementation of the compiler

I don't have any preference on how errors are handled but I should probably ask what the team wants before I do anything further

do internal (compiler implementation) errors need to be grouped together or leave as panics

are there general heuristics I should try to follow on which kind of errors should leave as it is

and should I continue working on this

@Ethan-000
Copy link
Contributor Author

Ethan-000 commented Aug 2, 2023

I left some unreachable errors are they are in the other acir_gen error pr cus I'm not sure what to do with them

@Ethan-000
Copy link
Contributor Author

should also mention that I only changed errors in basic_block and cfg so far and doing so implies that most functions needs to be modified accordingly

@jfecher
Copy link
Contributor

jfecher commented Aug 2, 2023

I think we should close this PR and commit to the "unrecoverable errors or core logic errors should be panics" ideology mentioned above. Since none of the SSA is meant to fail (yet at least) all of the errors in this PR would fall under this category and don't need to be changed to error enums.

are there general heuristics I should try to follow on which kind of errors should leave as it is

I think the most important heuristic is "can users of Noir trigger this by writing normal Noir code?" E.g. triggering a type error, using an undefined symbol, or messing around with crates imports that don't exist should never crash the compiler. On the other hand, internal assumptions like an SSA block always having a terminator instruction (once the block is completed at least) should be panics if they're violated because it means our assumptions writing the code was wrong. There is also nothing we can do besides report it and halt if we were to bubble it up as an error enum.

and should I continue working on this

I don't think so. I apologize for closing this PR when you've clearly put a lot of work into it but I think not trying to remove all panics from the code is healthier in the long term for the code base. That being said, if you see any panics which can be caused by user input, a PR to remove them would be greatly appreciated! If it would be a lot of work to remove and you're unsure if it is needed, you can always feel free to open an issue documenting it as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants