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

Error casting bool const to other than bit<1> #4419

Merged
merged 1 commit into from
Feb 19, 2024
Merged

Conversation

ChrisDodd
Copy link
Contributor

Trivial corner case that should not be allowed according to the spec

@vlstill
Copy link
Contributor

vlstill commented Feb 13, 2024

I am quite surprised this is not checked in type checking though. That would seem like a better place to do it (especially if we want to boot out the constant folding out of frontend in future!). The type checker checks it for variable access, why not for constant?

@ChrisDodd
Copy link
Contributor Author

ChrisDodd commented Feb 18, 2024

I am quite surprised this is not checked in type checking though. That would seem like a better place to do it (especially if we want to boot out the constant folding out of frontend in future!). The type checker checks it for variable access, why not for constant?

The basic issue is that we do constant folding before type checking so we can deal with bit<N> where N is an arbitrary constant expression. It might be better to rearrange things to only do constant folding (and copy propagation, and inlining) into type args (and things that end up in type args) prior to type checking, but that would be trickier, and still might run into this kind of issue allowing things that should not be allowed.

@@ -798,6 +798,9 @@ const IR::Node *DoConstantFolding::postorder(IR::Cast *e) {
if (auto arg = expr->to<IR::Constant>()) {
return cast(arg, arg->base, type);
} else if (auto arg = expr->to<IR::BoolLiteral>()) {
if (type->isSigned || type->size != 1)
error(ErrorType::ERR_INVALID, "%1%: Cannot cast %1% driectly to %2% (use bit<1>)",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
error(ErrorType::ERR_INVALID, "%1%: Cannot cast %1% driectly to %2% (use bit<1>)",
error(ErrorType::ERR_INVALID, "%1%: Cannot cast %1% directly to %2% (use bit<1>)",

@ChrisDodd ChrisDodd merged commit a733780 into main Feb 19, 2024
16 checks passed
@ChrisDodd ChrisDodd deleted the cdodd-boolcast branch February 19, 2024 03:58
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