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

Replace unnecessary validations with assertions #15185

Merged
merged 5 commits into from
Jun 13, 2024

Conversation

cameel
Copy link
Member

@cameel cameel commented Jun 6, 2024

CompilerStack has a lot of checks for things that are never meant to happen and do not need a user-facing validation. We normally enforce such expectations with assertions, because violating them is a bug, not a compilation error.

This has been bothering me for a long time, but became especially annoying now when I was reviewing #15168 and needed to figure out what kinds of runtime exceptions we throw and when.

I also fixed two somewhat related things:

  • Several cases of InternalCompilerError being thrown directly rather than as a result of a failed assert.
  • Several cases of unexpected FatalError being rethrown rather than immediately handled as a violated assumption. Now it's an ICE and the assert makes it clear that it's unexpected, not requiring extra comments.

@cameel cameel added the refactor label Jun 6, 2024
@cameel cameel self-assigned this Jun 6, 2024
@cameel cameel force-pushed the replace-unnecessary-validations-with-assertions branch from 9cd0682 to 57ba060 Compare June 7, 2024 17:33
@cameel cameel force-pushed the replace-unnecessary-validations-with-assertions branch from 57ba060 to 9068112 Compare June 12, 2024 19:59
nikola-matic
nikola-matic previously approved these changes Jun 12, 2024
matheusaaguiar
matheusaaguiar previously approved these changes Jun 12, 2024
@cameel cameel dismissed stale reviews from matheusaaguiar and nikola-matic via 6e165ed June 13, 2024 17:10
@cameel cameel force-pushed the replace-unnecessary-validations-with-assertions branch 3 times, most recently from 7ab08c3 to 00995d4 Compare June 13, 2024 17:29
@cameel cameel force-pushed the replace-unnecessary-validations-with-assertions branch from 00995d4 to 2694190 Compare June 13, 2024 18:33
Comment on lines -79 to +83
// This FatalError con occur if the errorReporter has too many errors.
yulAssert(!watcher.ok(), "Fatal error detected, but no error is reported.");
// NOTE: There's a cap on the number of reported errors, but watcher.ok() will work fine even if
// we exceed it because the reporter keeps counting (it just stops adding errors to the list).
// Note also that fact of exceeding the cap triggers a FatalError so one can get thrown even
// if we don't make any of our errors fatal.
yulAssert(!watcher.ok(), "Unreported fatal error: "s + error.what());
Copy link
Member Author

@cameel cameel Jun 13, 2024

Choose a reason for hiding this comment

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

I initially thought this was a bug and that the assert would fail with more than 256 errors. Turns out it doesn't because the error reporter keeps increasing the count. I added a test against this in any case - turns out we didn't have one yet.

@@ -63,7 +63,7 @@ bool NameAndTypeResolver::registerDeclarations(SourceUnit& _sourceUnit, ASTNode
}
catch (langutil::FatalError const& error)
{
solAssert(!m_errorReporter.errors().empty(), "Unreported fatal error: "s + error.what());
solAssert(m_errorReporter.hasErrors(), "Unreported fatal error: "s + error.what());
Copy link
Member Author

@cameel cameel Jun 13, 2024

Choose a reason for hiding this comment

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

This one is a bug though. hasErrors() is not the same as !errors.empty(). The latter will still count warnings and infos. This means we'll silence a buggy FatalError if some unrelated warning or info happened to be reported before it.

I don't think the results of this are visible to the user, since it only makes a difference when combined with another bug (throwing FatalError without reporting it), but nevertheless, it looks unintended.

@cameel cameel merged commit e5d60a2 into develop Jun 13, 2024
72 checks passed
@cameel cameel deleted the replace-unnecessary-validations-with-assertions branch June 13, 2024 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants