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

Pay more attention to types during constant folding #2338

Merged
merged 5 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Constant initializers must have the right type
  • Loading branch information
Mihai Budiu committed Apr 24, 2020
commit c030fe38f7f8379d67d4a06e5d60d1bf830193d8
13 changes: 7 additions & 6 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ const IR::Node* DoConstantFolding::postorder(IR::Declaration_Constant* d) {
// is not init, but (d->type)init. The typechecker inserts
// casts, but if we run this before typechecking we have to be
// more conservative.
if (init->is<IR::Constant>()) {
auto cst = init->to<IR::Constant>();
if (d->type->is<IR::Type_Bits>()) {
if (cst->type->is<IR::Type_InfInt>() ||
(cst->type->is<IR::Type_Bits>() &&
!(*d->type->to<IR::Type_Bits>() == *cst->type->to<IR::Type_Bits>())))
if (auto cst = init->to<IR::Constant>()) {
if (auto dtype = d->type->to<IR::Type_Bits>()) {
auto cstBits = cst->type->to<IR::Type_Bits>();
if (cstBits && !(*dtype == *cstBits))
::error(ErrorType::ERR_TYPE_ERROR, "%1%: initializer has wrong type %2%",
d, cst->type);
else if (cst->type->is<IR::Type_InfInt>())
init = new IR::Constant(init->srcInfo, d->type, cst->value, cst->base);
} else if (!d->type->is<IR::Type_InfInt>()) {
// Don't fold this yet, we can't evaluate the cast.
Expand Down
1 change: 1 addition & 0 deletions testdata/p4_16_errors/issue2332.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const bit<32> x = 1w1;
1 change: 1 addition & 0 deletions testdata/p4_16_errors_outputs/issue2332.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const bit<32> x = 1w1;
3 changes: 3 additions & 0 deletions testdata/p4_16_errors_outputs/issue2332.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
issue2332.p4(1): [--Werror=type-error] error: x: initializer has wrong type bit<1>
const bit<32> x = 1w1;
^^^