Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 14 additions & 2 deletions cpp/impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1891,8 +1891,20 @@ class PALConsumer : public ASTConsumer {

// Evaluate scrutinee
auto scrutRval = trRValue(sw->getCond());
auto scrutTy =
trQualType(sw->getCond()->getType(), sw->getCond()->getSourceRange());
// Clang applies the integral promotions to the switch condition. An
// enumeration is modeled by its underlying integer type, so that
// promotion translates to the identity and is elided from the rvalue.
// Annotating the binding with the promoted type would then leave the
// declared type and the translated value syntactically distinct, which
// the Pulse dereference tactic rejects. Use the enumeration's own type
// so the two agree; other promotions are genuine casts that trRValue
// preserves, so their promoted type remains correct.
auto *scrutExpr = sw->getCond();
auto scrutQualTy = scrutExpr->getType();
if (scrutExpr->IgnoreImpCasts()->getType()->isEnumeralType()) {
scrutQualTy = scrutExpr->IgnoreImpCasts()->getType();
}
auto scrutTy = trQualType(scrutQualTy, scrutExpr->getSourceRange());

// Bind the scrutinee once as an immutable value.
static int switchCounter = 0;
Expand Down
7 changes: 6 additions & 1 deletion src/pass/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ fn scan_inline_pulse_code(deps: &mut HashSet<DeclName>, code: &InlinePulseCode)

fn scan_expr(deps: &mut HashSet<DeclName>, rv: &Expr) {
match &rv.val {
ExprT::Var(_) => {}
// A bare name may refer to a global (an enum constant, say). Locals share the namespace,
// so this over-approximates -- which is the safe direction for a pruner: it can keep a
// declaration that is not needed, but must never drop one that is.
ExprT::Var(n) => {
deps.insert(DeclName::GlobalVar(n.val.clone()));
}
ExprT::Deref(v) => scan_expr(deps, v),
ExprT::Member(x, _a) => scan_expr(deps, x),
ExprT::VAttr(_, x) => scan_expr(deps, x),
Expand Down
1 change: 1 addition & 0 deletions test/enum_const_in_contract/Makefile
20 changes: 20 additions & 0 deletions test/enum_const_in_contract/colors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include "pal.h"

// These enumerators live in a header, so the pruner sees them as non-root
// declarations. The contract below is their only reference: the body uses a
// switch, whose case labels the C frontend folds to integer constants.
typedef enum _COLOR
{
Color_Red = 0,
Color_Green = 1,
Color_Blue = 2,
} COLOR;

_Bool
color_is_known(COLOR c)
_ensures(return == (c == Color_Red || c == Color_Green || c == Color_Blue));

COLOR
color_default(void)
_ensures(return == Color_Green);
26 changes: 26 additions & 0 deletions test/enum_const_in_contract/enum_const_in_contract.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "colors.h"

_Bool
color_is_known(COLOR c)
{
_Bool known = 0;

switch (c)
{
case Color_Red:
case Color_Green:
case Color_Blue:
known = 1;
break;
default:
break;
}

return known;
}

COLOR
color_default(void)
{
return Color_Green;
}
1 change: 1 addition & 0 deletions test/enum_const_in_contract/fstar.fst.config.json
1 change: 1 addition & 0 deletions test/enum_const_in_contract/pal.config.json
1 change: 1 addition & 0 deletions test/enum_const_in_contract/pal.h
Loading