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

Structs: Fields check #1907

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 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
4 changes: 1 addition & 3 deletions ast/src/analyzed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ impl<T: Display> Display for Analyzed<T> {
if matches!(
definition,
Some(FunctionValueDefinition::TypeConstructor(_, _))
) || matches!(
definition,
Some(FunctionValueDefinition::TraitFunction(_, _))
| Some(FunctionValueDefinition::TraitFunction(_, _))
) {
// These are printed as part of the enum / trait.
continue;
Expand Down
12 changes: 9 additions & 3 deletions ast/src/parsed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,23 @@ pub enum SymbolCategory {
TypeConstructor,
/// A trait declaration, which can be used as a type.
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
TraitDeclaration,
/// A struct, which can be used as a type.
Struct,
}
impl SymbolCategory {
/// Returns if a symbol of a given category can satisfy a request for a certain category.
pub fn compatible_with_request(&self, request: SymbolCategory) -> bool {
match self {
SymbolCategory::Value => request == SymbolCategory::Value,
SymbolCategory::Type => request == SymbolCategory::Type,
SymbolCategory::Struct => {
// Structs can also satisfy requests for types.
request == SymbolCategory::Struct || request == SymbolCategory::Type
}
SymbolCategory::TypeConstructor => {
// Type constructors can also satisfy requests for values.
request == SymbolCategory::TypeConstructor || request == SymbolCategory::Value
}
SymbolCategory::Value => request == SymbolCategory::Value,
SymbolCategory::Type => request == SymbolCategory::Type,
SymbolCategory::TraitDeclaration => request == SymbolCategory::TraitDeclaration,
}
}
Expand Down Expand Up @@ -153,7 +159,7 @@ impl PilStatement {
),
),
PilStatement::StructDeclaration(_, StructDeclaration { name, .. }) => {
Box::new(once((name, None, SymbolCategory::Type)))
Box::new(once((name, None, SymbolCategory::Struct)))
}
PilStatement::TraitDeclaration(
_,
Expand Down
28 changes: 25 additions & 3 deletions pil-analyzer/src/expression_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use powdr_ast::{
parsed::{
self, asm::SymbolPath, types::Type, ArrayExpression, ArrayLiteral, BinaryOperation,
BlockExpression, IfExpression, LambdaExpression, LetStatementInsideBlock, MatchArm,
MatchExpression, NamespacedPolynomialReference, Number, Pattern, SelectedExpressions,
StatementInsideBlock, SymbolCategory, UnaryOperation,
MatchExpression, NamedExpression, NamespacedPolynomialReference, Number, Pattern,
SelectedExpressions, StatementInsideBlock, StructExpression, SymbolCategory,
UnaryOperation,
},
};

Expand Down Expand Up @@ -189,7 +190,28 @@ impl<'a, D: AnalysisDriver> ExpressionProcessor<'a, D> {
self.process_block_expression(statements, expr, src)
}
PExpression::FreeInput(_, _) => panic!(),
PExpression::StructExpression(_, _) => unimplemented!("Structs are not supported yet"),
PExpression::StructExpression(src, StructExpression { name, fields }) => {
let type_args = name
.type_args
.map(|args| args.into_iter().map(|t| self.process_type(t)).collect());

Expression::StructExpression(
src,
StructExpression {
name: Reference::Poly(PolynomialReference {
name: self.driver.resolve_ref(&name.path, SymbolCategory::Struct),
type_args,
}),
fields: fields
.into_iter()
.map(|named_expr| NamedExpression {
name: named_expr.name,
body: Box::new(self.process_expression(*named_expr.body)),
})
.collect(),
},
)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions pil-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod expression_processor;
mod pil_analyzer;
mod side_effect_checker;
mod statement_processor;
mod structural_checks;
mod traits_resolver;
mod type_builtins;
mod type_inference;
Expand Down
51 changes: 49 additions & 2 deletions pil-analyzer/src/pil_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::iter::once;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::structural_checks::check_structs_fields;
use itertools::Itertools;
use powdr_ast::parsed::asm::{
parse_absolute_path, AbsoluteSymbolPath, ModuleStatement, SymbolPath,
Expand All @@ -14,14 +15,13 @@ use powdr_ast::parsed::types::Type;
use powdr_ast::parsed::visitor::{AllChildren, Children};
use powdr_ast::parsed::{
self, FunctionKind, LambdaExpression, PILFile, PilStatement, SymbolCategory,
TraitImplementation,
TraitImplementation, TypedExpression,
};
use powdr_number::{FieldElement, GoldilocksField};

use powdr_ast::analyzed::{
type_from_definition, Analyzed, DegreeRange, Expression, FunctionValueDefinition,
PolynomialType, PublicDeclaration, Reference, StatementIdentifier, Symbol, SymbolKind,
TypedExpression,
};
use powdr_parser::{parse, parse_module, parse_type};
use powdr_parser_util::Error;
Expand Down Expand Up @@ -52,6 +52,7 @@ fn analyze<T: FieldElement>(files: Vec<PILFile>) -> Result<Analyzed<T>, Vec<Erro
let mut analyzer = PILAnalyzer::new();
analyzer.process(files)?;
analyzer.side_effect_check()?;
analyzer.validate_structs()?;
analyzer.type_check()?;
let solved_impls = analyzer.resolve_trait_impls()?;
analyzer.condense(solved_impls)
Expand Down Expand Up @@ -253,6 +254,18 @@ impl PILAnalyzer {
}
}

pub fn validate_structs(&self) -> Result<(), Vec<Error>> {
let structs_exprs = self.all_children().filter_map(|expr| {
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
if let Expression::StructExpression(sr, struct_expr) = expr {
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
Some((sr, struct_expr))
} else {
None
}
});

check_structs_fields(structs_exprs, &self.definitions)
}

pub fn type_check(&mut self) -> Result<(), Vec<Error>> {
let query_type: Type = parse_type("int -> std::prelude::Query").unwrap().into();
let mut expressions = vec![];
Expand Down Expand Up @@ -538,6 +551,40 @@ impl PILAnalyzer {
}
}

impl Children<Expression> for PILAnalyzer {
fn children(&self) -> Box<dyn Iterator<Item = &Expression> + '_> {
Box::new(
self.definitions
.values()
.filter_map(|(_, def)| def.as_ref())
.flat_map(|def| def.children())
.chain(
self.trait_impls
.values()
.flat_map(|impls| impls.iter())
.flat_map(|impl_| impl_.children()),
)
.chain(self.proof_items.iter()),
)
chriseth marked this conversation as resolved.
Show resolved Hide resolved
}

fn children_mut(&mut self) -> Box<dyn Iterator<Item = &mut Expression> + '_> {
Box::new(
self.definitions
.values_mut()
.filter_map(|(_, def)| def.as_mut())
.flat_map(|def| def.children_mut())
.chain(
self.trait_impls
.values_mut()
.flat_map(|impls| impls.iter_mut())
.flat_map(|impl_| impl_.children_mut()),
)
.chain(self.proof_items.iter_mut()),
)
}
}

#[derive(Clone, Copy)]
struct Driver<'a>(&'a PILAnalyzer);

Expand Down
114 changes: 114 additions & 0 deletions pil-analyzer/src/structural_checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use itertools::Itertools;
use std::collections::{HashMap, HashSet};

use powdr_ast::{
analyzed::{FunctionValueDefinition, PolynomialReference, Reference, Symbol},
parsed::{StructExpression, TypeDeclaration},
};
use powdr_parser_util::{Error, SourceRef};

/// Verifies that all struct instantiations match their corresponding declarations
/// (existence of field names, completeness) and ensures that both are correct.
pub fn check_structs_fields<'a>(
structs_exprs: impl Iterator<Item = (&'a SourceRef, &'a StructExpression<Reference>)>,
definitions: &HashMap<String, (Symbol, Option<FunctionValueDefinition>)>,
) -> Result<(), Vec<Error>> {
let mut errors = Vec::new();

for (source, st) in structs_exprs {
let StructExpression {
name: Reference::Poly(PolynomialReference { name, .. }),
fields,
} = st
else {
unreachable!()
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
};

errors.extend(check_struct_expression(definitions, name, fields, source));
}

errors.extend(check_struct_declarations(definitions));

if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}

fn check_struct_expression(
definitions: &HashMap<String, (Symbol, Option<FunctionValueDefinition>)>,
name: &String,
fields: &[powdr_ast::parsed::NamedExpression<Box<powdr_ast::parsed::Expression<Reference>>>],
source: &SourceRef,
) -> Vec<Error> {
let mut errors = Vec::new();
if let (
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this panic if it's not a FunctionValueDefinition::TypeDeclaration?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since names comes from StructExpressions, None shouldn't be a possible case, hence the direct unwrap.

_,
Some(FunctionValueDefinition::TypeDeclaration(TypeDeclaration::Struct(struct_decl))),
) = definitions.get(name).unwrap()
{
let declared_fields: HashSet<_> = struct_decl.fields.iter().map(|f| &f.name).collect();
let used_fields: HashSet<_> = fields.iter().map(|f| &f.name).collect();

errors.extend(
fields
.iter()
.filter(|f| !declared_fields.contains(&f.name))
.map(|f| {
source.with_error(format!("Struct '{name}' has no field named '{}'", f.name))
}),
);

errors.extend(declared_fields.difference(&used_fields).map(|&f| {
source.with_error(format!("Missing field '{f}' in initializer of '{name}'",))
}));

let duplicate_fields: Vec<_> = fields.iter().map(|f| &f.name).duplicates().collect();

errors.extend(
duplicate_fields
.into_iter()
.map(|f| source.with_error(format!("Field '{f}' specified more than once"))),
);
}

errors
}

fn check_struct_declarations(
definitions: &HashMap<String, (Symbol, Option<FunctionValueDefinition>)>,
) -> Vec<Error> {
let structs_decls = definitions.iter().filter(|(_, (_, def))| {
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
matches!(
def,
Some(FunctionValueDefinition::TypeDeclaration(
TypeDeclaration::Struct(_)
))
)
});

let mut errors = Vec::new();
for (_, (symbol, def)) in structs_decls {
let Some(FunctionValueDefinition::TypeDeclaration(TypeDeclaration::Struct(struct_decl))) =
def
else {
unreachable!()
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
};

let duplicate_declaration_fields: Vec<_> = struct_decl
.fields
.iter()
.map(|f| &f.name)
.duplicates()
.collect();

errors.extend(duplicate_declaration_fields.into_iter().map(|f| {
symbol
.source
.with_error(format!("Field '{f}' is declared more than once"))
}));
}

errors
}
72 changes: 72 additions & 0 deletions pil-analyzer/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,78 @@ fn prover_functions() {
type_check(input, &[("a", "", "int"), ("b", "", "()[]")]);
}

gzanitti marked this conversation as resolved.
Show resolved Hide resolved
#[test]
#[should_panic = "Struct symbol not found: NotADot"]
fn wrong_struct() {
let input = "
struct Dot { x: int, y: int }
let f: int -> Dot = |i| NotADot{x: 0, y: i};
";
type_check(input, &[]);
}

#[test]
#[should_panic = "Struct 'Dot' has no field named 'a'"]
fn struct_wrong_fields() {
let input = "
struct Dot { x: int, y: int }
let f: int -> Dot = |i| Dot{x: 0, y: i, a: 2};
let x = f(0);
";
type_check(input, &[]);
}

#[test]
#[should_panic = "Missing field 'z' in initializer of 'A'"]
fn test_struct_unused_fields() {
let input = " struct A {
x: int,
y: int,
z: int,
}
let x = A{ y: 0, x: 2 };
";

type_check(input, &[]);
}

#[test]
#[should_panic = "Field 'y' specified more than once"]
fn test_struct_repeated_fields_expr() {
let input = " struct A {
x: int,
y: int,
}
let x = A{ y: 0, x: 2, y: 1 };
";

type_check(input, &[]);
}

#[test]
#[should_panic = "Field 'x' is declared more than once"]
fn test_struct_repeated_fields_decl() {
let input = " struct A {
x: int,
y: int,
x: int,
}
let x = A{ y: 0, x: 2 };
";

type_check(input, &[]);
}

#[test]
#[should_panic(expected = "Expected symbol of kind Struct but got Type: A")]
fn enum_used_as_struct() {
let input = "
enum A { X }
let a = A{x: 8};
";
type_check(input, &[]);
}

#[test]
fn typed_literals() {
let input = "
Expand Down