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

Support non-base type fields in structs #634

Merged
merged 1 commit into from
Jan 19, 2022
Merged
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
7 changes: 7 additions & 0 deletions crates/analyzer/src/db/queries/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ pub fn contract_field_type(
scope.not_yet_implemented("contract field initial value assignment", value_node.span);
}

if matches!(typ, Ok(Type::Struct(Struct { id, .. })) if id.has_complex_fields(db)) {
scope.not_yet_implemented(
"structs in storage aren't yet allowed to contain fields with complex types",
node.span,
);
}

Analysis {
value: typ,
diagnostics: Rc::new(scope.diagnostics),
Expand Down
4 changes: 4 additions & 0 deletions crates/analyzer/src/db/queries/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ pub fn function_signature(
Ok(FixedSize::unit())
} else {
match type_desc(&mut scope, type_node)?.try_into() {
Ok(FixedSize::Struct(val)) if val.id.has_complex_fields(db) && function.is_public(db) => {
scope.not_yet_implemented("structs with complex fields can't be returned from public functions yet", type_node.span);
Ok(FixedSize::Struct(val))
}
Ok(typ) => Ok(typ),
Err(_) => Err(TypeError::new(scope.error(
"function return type must have a fixed size",
Expand Down
20 changes: 15 additions & 5 deletions crates/analyzer/src/db/queries/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,21 @@ pub fn struct_field_type(
scope.not_yet_implemented("struct field initial value assignment", field_data.ast.span);
}
let typ = match type_desc(&mut scope, typ) {
Ok(types::Type::Base(base)) => Ok(types::FixedSize::Base(base)),
Ok(_) => Err(TypeError::new(scope.not_yet_implemented(
"non-primitive type struct fields",
field_data.ast.span,
))),
Ok(typ) => match typ.try_into() {
Ok(FixedSize::Contract(contract)) => {
scope.not_yet_implemented(
"contract types aren't yet supported as struct fields",
field_data.ast.span,
);
Ok(FixedSize::Contract(contract))
}
Ok(typ) => Ok(typ),
Err(_) => Err(TypeError::new(scope.error(
"struct field type must have a fixed size",
field_data.ast.span,
"this can't be used as an struct field",
))),
},
Err(err) => Err(err),
};

Expand Down
6 changes: 6 additions & 0 deletions crates/analyzer/src/namespace/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,12 @@ impl StructId {
Some(self.field(db, name)?.typ(db))
}

pub fn has_complex_fields(&self, db: &dyn AnalyzerDb) -> bool {
self.fields(db)
.iter()
.any(|(_, field)| !matches!(field.typ(db), Ok(types::FixedSize::Base(_))))
}

pub fn fields(&self, db: &dyn AnalyzerDb) -> Rc<IndexMap<SmolStr, StructFieldId>> {
db.struct_field_map(*self).value
}
Expand Down
4 changes: 4 additions & 0 deletions crates/analyzer/src/namespace/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,10 @@ impl FixedSize {
self == &Self::Base(Base::Unit)
}

pub fn is_base(&self) -> bool {
matches!(self, FixedSize::Base(_))
}

/// Creates an instance of bool.
pub fn bool() -> Self {
FixedSize::Base(Base::Bool)
Expand Down
1 change: 1 addition & 0 deletions crates/analyzer/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ test_file! { return_call_to_fn_without_return }
test_file! { return_from_init }
test_file! { return_lt_mixed_types }
test_file! { return_type_undefined }
test_file! { return_complex_struct }
test_file! { return_type_not_fixedsize }
test_file! { undefined_type_param }

Expand Down
Loading