Skip to content

Commit

Permalink
Remove enums and strictly replace them with unions
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadfawaz committed Oct 3, 2024
1 parent 457cf5d commit 271ce49
Show file tree
Hide file tree
Showing 92 changed files with 906 additions and 1,250 deletions.
2 changes: 1 addition & 1 deletion docs/book/theme/pint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ hljs.registerLanguage("pint", (hljs) => ({
name: "Pint",
keywords: {
keyword:
"as bool b256 cond const constraint else enum exists false forall if in int interface macro mut nil predicate pub real satisfy self state storage string true type use var where",
"as bool b256 cond const constraint else exists false forall if in int interface macro mut nil predicate pub real satisfy self state storage string true type use union var where",
literal: "false true",
},
contains: [
Expand Down
2 changes: 1 addition & 1 deletion docs/specs/theme/pint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ hljs.registerLanguage("pint", (hljs) => ({
name: "Pint",
keywords: {
keyword:
"as bool b256 cond const constraint else enum exists false forall if in int interface macro maximize minimize nil predicate pub real satisfy self solve state storage string true type use var where",
"as bool b256 cond const constraint else exists false forall if in int interface macro maximize minimize nil predicate pub real satisfy self solve state storage string true type union use var where",
literal: "false true",
},
contains: [
Expand Down
2 changes: 1 addition & 1 deletion examples/backyard/src/garden/vegetables.pnt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
enum Asparagus = Green | White | Purple;
union Asparagus = Green | White | Purple;
2 changes: 1 addition & 1 deletion examples/ch_3_5.pnt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var user1: User = {
}

// ANCHOR: simple_enum
enum Token = DAI | USDC | USDT;
union Token = DAI | USDC | USDT;
// ANCHOR_END: simple_enum

predicate test2 {
Expand Down
2 changes: 1 addition & 1 deletion pint-pkg/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ predicate Increment {
}
"#;

const DEFAULT_LIBRARY_PNT: &str = r#"enum Animal = Cat | Dog;
const DEFAULT_LIBRARY_PNT: &str = r#"union Animal = Cat | Dog;
type Person = {
address: b256,
Expand Down
2 changes: 1 addition & 1 deletion pintc/src/asm_gen/asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl<'a> AsmBuilder<'a> {
) -> Result<Location, ErrorEmitted> {
fn compile_immediate(asm: &mut Asm, imm: &Immediate) -> usize {
match imm {
Immediate::Int(val) | Immediate::Enum(val, _) => {
Immediate::Int(val) => {
asm.push(Stack::Push(*val).into());
1
}
Expand Down
69 changes: 37 additions & 32 deletions pintc/src/error/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum CompileError {
NoFileFoundForPath {
path_full: String,
path_mod: String,
path_enum: String,
path_union: String,
span: Span,
},
#[error("macro {name} is declared multiple times")]
Expand Down Expand Up @@ -111,7 +111,7 @@ pub enum CompileError {
SymbolNotFound {
name: String,
span: Span,
enum_names: Vec<String>,
union_names: Vec<String>,
},
#[error("cannot find storage variable `{name}`")]
StorageSymbolNotFound { name: String, span: Span },
Expand Down Expand Up @@ -245,8 +245,6 @@ pub enum CompileError {
BadCastTo { ty: String, span: Span },
#[error("invalid cast")]
BadCastFrom { ty: String, span: Span },
#[error("invalid declaration outside a predicate")]
InvalidDeclOutsidePredicateDecl { kind: String, span: Span },
#[error("left and right types in range differ")]
RangeTypesMismatch {
lb_ty: String,
Expand Down Expand Up @@ -806,7 +804,8 @@ impl ReportableError for CompileError {

InvalidArrayRangeType { span, .. } => {
vec![ErrorLabel {
message: "array access must be of type `int` or `enum`".to_string(),
message: "array access must be of type `int` or enumeration `union`"
.to_string(),
span: span.clone(),
color: Color::Red,
}]
Expand Down Expand Up @@ -1002,19 +1001,13 @@ impl ReportableError for CompileError {
}],

BadCastTo { ty, span } => vec![ErrorLabel {
message: format!("illegal cast to a `{ty}`"),
message: format!("illegal cast to `{ty}`"),
span: span.clone(),
color: Color::Red,
}],

BadCastFrom { ty, span } => vec![ErrorLabel {
message: format!("illegal cast from a `{ty}`"),
span: span.clone(),
color: Color::Red,
}],

InvalidDeclOutsidePredicateDecl { kind, span } => vec![ErrorLabel {
message: format!("invalid {kind} declaration outside a predicate"),
message: format!("illegal cast from `{ty}`"),
span: span.clone(),
color: Color::Red,
}],
Expand Down Expand Up @@ -1118,7 +1111,8 @@ impl ReportableError for CompileError {

InRangeInvalid { found_ty, span } => vec![ErrorLabel {
message: format!(
"`in` operator range must be either an enum or an array, found `{found_ty}`"
"`in` operator range must be either an enumeration union or an array, \
found `{found_ty}`"
),
span: span.clone(),
color: Color::Red,
Expand Down Expand Up @@ -1263,10 +1257,10 @@ impl ReportableError for CompileError {

NoFileFoundForPath {
path_mod,
path_enum,
path_union,
..
} => Some(format!(
"one of the modules `{path_mod}` or `{path_enum}` must exist",
"one of the modules `{path_mod}` or `{path_union}` must exist",
)),

MacroDeclClash { name, .. } => Some(format!(
Expand All @@ -1290,7 +1284,8 @@ impl ReportableError for CompileError {
),

MacroSpliceArrayUnknownSize { .. } => Some(
"macro array splicing is currently limited to immediate integer sizes or enums"
"macro array splicing is currently limited to immediate integer sizes or \
enumeration unions"
.to_string(),
),

Expand Down Expand Up @@ -1338,10 +1333,6 @@ impl ReportableError for CompileError {
Some(format!("found access using type `{found_ty}`"))
}

InvalidDeclOutsidePredicateDecl { .. } => Some(
"only `enum` and `type` declarations are allowed outside a predicate".to_string(),
),

VarHasStorageType { ty, nested_ty, .. } => {
if ty != nested_ty {
Some(format!(
Expand Down Expand Up @@ -1440,10 +1431,10 @@ impl ReportableError for CompileError {
fn help(&self) -> Option<String> {
use CompileError::*;
match self {
SymbolNotFound { enum_names, .. } if !enum_names.is_empty() => Some(format!(
"this symbol is a variant of enum{} {} and may need a fully qualified path",
if enum_names.len() > 1 { "s" } else { "" },
pretty_join_strings(enum_names),
SymbolNotFound { union_names, .. } if !union_names.is_empty() => Some(format!(
"this symbol is a variant of union{} {} and may need a fully qualified path",
if union_names.len() > 1 { "s" } else { "" },
pretty_join_strings(union_names),
)),

MatchVariantUnknown {
Expand All @@ -1465,13 +1456,28 @@ impl ReportableError for CompileError {
"a macro named `{name}` found with a different signature"
))),

BadCastTo { .. } => Some("casts may only be made to an int or a real".to_string()),
BadCastTo { .. } => {
if cfg!(feature = "experimental-types") {
Some("casts may only be made to `int` or `real`".to_string())
} else {
Some("casts may only be made to `int`".to_string())
}
}

BadCastFrom { .. } => Some(
"casts may only be made from an int to a real, from a bool to an int or \
from an enum to an int"
.to_string(),
),
BadCastFrom { .. } => {
if cfg!(feature = "experimental-types") {
Some(
"casts may only be made from `bool`s,`int`s, and enuemration unions to `int`, \
or from `int`s, `real`s, and enumeration unions to `real`"
.to_string()
)
} else {
Some(
"casts may only be made from `bool`s,`int`s, and enuemration unions to `int`"
.to_string()
)
}
}

CompareToNilError { .. } => Some(
"only state variables and next state expressions can be compared to `nil`"
Expand Down Expand Up @@ -1623,7 +1629,6 @@ impl Spanned for CompileError {
}
| BadCastTo { span, .. }
| BadCastFrom { span, .. }
| InvalidDeclOutsidePredicateDecl { span, .. }
| RangeTypesMismatch { span, .. }
| RangeTypesNonNumeric { span, .. }
| InExprTypesMismatch { span, .. }
Expand Down
7 changes: 0 additions & 7 deletions pintc/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ pub enum Immediate {
Nil,
Real(f64),
Int(i64),
Enum(i64, Path),
Bool(bool),
String(String),
B256([u64; 4]),
Expand Down Expand Up @@ -199,11 +198,6 @@ impl Immediate {
span,
},

Immediate::Enum(_, path) => Type::Custom {
path: path.to_string(),
span,
},

Immediate::UnionVariant { ty_path, .. } => Type::Union {
path: ty_path.to_string(),
span,
Expand All @@ -221,7 +215,6 @@ impl Immediate {
Immediate::Error
| Immediate::Array { .. }
| Immediate::Tuple(_)
| Immediate::Enum(..)
| Immediate::UnionVariant { .. } => {
unreachable!()
}
Expand Down
1 change: 0 additions & 1 deletion pintc/src/expr/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ impl DisplayWithContract for super::Immediate {
super::Immediate::Nil => write!(f, "nil"),
super::Immediate::Real(n) => write!(f, "{n:e}"),
super::Immediate::Int(n) => write!(f, "{n}"),
super::Immediate::Enum(n, _) => write!(f, "{n}"),
super::Immediate::Bool(b) => write!(f, "{b}"),
super::Immediate::String(s) => write!(f, "{s:?}"),
super::Immediate::B256(val) => {
Expand Down
Loading

0 comments on commit 271ce49

Please sign in to comment.