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 16 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
7 changes: 6 additions & 1 deletion ast/src/parsed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ 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
Struct,
}
impl SymbolCategory {
/// Returns if a symbol of a given category can satisfy a request for a certain category.
Expand All @@ -54,6 +56,9 @@ impl SymbolCategory {
request == SymbolCategory::TypeConstructor || request == SymbolCategory::Value
}
SymbolCategory::TraitDeclaration => request == SymbolCategory::TraitDeclaration,
SymbolCategory::Struct => {
request == SymbolCategory::Struct || request == SymbolCategory::Type
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand Down Expand Up @@ -153,7 +158,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
56 changes: 54 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 @@ -56,6 +56,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.struct_fields_check()?;
analyzer.type_check()?;
let solved_impls = analyzer.resolve_trait_impls()?;
analyzer.condense(solved_impls)
Expand Down Expand Up @@ -257,6 +258,23 @@ impl PILAnalyzer {
}
}

pub fn struct_fields_check(&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
}
});

let errors = check_structs_fields(structs_exprs, &self.definitions);
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}

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 @@ -542,6 +560,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
93 changes: 93 additions & 0 deletions pil-analyzer/src/structural_checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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>)>,
) -> Vec<Error> {
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
let mut errors = Vec::new();
let mut visited_structs = HashSet::new();
gzanitti marked this conversation as resolved.
Show resolved Hide resolved

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

if let Some((
_,
Some(FunctionValueDefinition::TypeDeclaration(TypeDeclaration::Struct(struct_decl))),
)) = definitions.get(name)
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
{
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| {
sr.with_error(format!("Struct '{name}' has no field named '{}'", f.name))
}),
);

errors.extend(declared_fields.difference(&used_fields).map(|&f| {
sr.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| sr.with_error(format!("Field '{f}' specified more than once"))),
);
}

visited_structs.insert(name);
}

let structs_decls = definitions.iter().filter(|(_, (_, def))| {
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
matches!(
def,
Some(FunctionValueDefinition::TypeDeclaration(
TypeDeclaration::Struct(_)
))
)
});

for (_, (symbol, def)) in structs_decls {
if let Some(FunctionValueDefinition::TypeDeclaration(TypeDeclaration::Struct(
struct_decl,
))) = def
{
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"))
}));
} else {
unreachable!()
gzanitti marked this conversation as resolved.
Show resolved Hide resolved
}
}

errors
}
72 changes: 72 additions & 0 deletions pil-analyzer/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,75 @@ 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, &[]);
}