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

Make fields for struct meta non-optional #164

Merged
merged 1 commit into from
Nov 13, 2020
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
18 changes: 5 additions & 13 deletions crates/rune/src/compiling/assemble/expr_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ impl Assemble for ast::ExprObject {

match &meta.kind {
CompileMetaKind::UnitStruct { .. } => {
check_object_fields(Some(&HashSet::new()), check_keys, span, &meta.item)?;
check_object_fields(&HashSet::new(), check_keys, span, &meta.item)?;

let hash = Hash::type_hash(&meta.item);
c.asm.push(Inst::UnitStruct { hash }, span);
}
CompileMetaKind::Struct { object, .. } => {
check_object_fields(object.fields.as_ref(), check_keys, span, &meta.item)?;
check_object_fields(&object.fields, check_keys, span, &meta.item)?;

let hash = Hash::type_hash(&meta.item);
c.asm.push(Inst::Struct { hash, slot }, span);
}
CompileMetaKind::StructVariant { object, .. } => {
check_object_fields(object.fields.as_ref(), check_keys, span, &meta.item)?;
check_object_fields(&object.fields, check_keys, span, &meta.item)?;

let hash = Hash::type_hash(&meta.item);
c.asm.push(Inst::StructVariant { hash, slot }, span);
Expand Down Expand Up @@ -90,20 +90,12 @@ impl Assemble for ast::ExprObject {
}

fn check_object_fields(
fields: Option<&HashSet<Box<str>>>,
fields: &HashSet<Box<str>>,
check_keys: Vec<(Box<str>, Span)>,
span: Span,
item: &Item,
) -> CompileResult<()> {
let mut fields = match fields {
Some(fields) => fields.clone(),
None => {
return Err(CompileError::new(
span,
CompileErrorKind::MissingItem { item: item.clone() },
));
}
};
let mut fields = fields.clone();

for (field, span) in check_keys {
if !fields.remove(&field) {
Expand Down
12 changes: 1 addition & 11 deletions crates/rune/src/compiling/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,17 +550,7 @@ impl<'a> Compiler<'a> {
}
};

let fields = match &object.fields {
Some(fields) => fields,
None => {
// NB: might want to describe that field composition is unknown because it is an external meta item.
return Err(CompileError::expected_meta(
span,
meta,
"type that can be used in an object pattern",
));
}
};
let fields = &object.fields;

for binding in &bindings {
if !fields.contains(binding.key()) {
Expand Down
4 changes: 1 addition & 3 deletions crates/rune/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1734,9 +1734,7 @@ fn struct_body_meta(
fields.insert(name.into());
}

let object = CompileMetaStruct {
fields: Some(fields),
};
let object = CompileMetaStruct { fields };

Ok(match enum_item {
Some(enum_item) => CompileMetaKind::StructVariant {
Expand Down
2 changes: 1 addition & 1 deletion crates/runestick/src/compile_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub struct CompileMetaEmpty {
#[derive(Debug, Clone)]
pub struct CompileMetaStruct {
/// Fields associated with the type.
pub fields: Option<HashSet<Box<str>>>,
pub fields: HashSet<Box<str>>,
}

/// The metadata about a variant.
Expand Down
4 changes: 3 additions & 1 deletion crates/runestick/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ impl Context {
item,
kind: CompileMetaKind::Struct {
type_hash,
object: CompileMetaStruct { fields: None },
object: CompileMetaStruct {
fields: Default::default(),
},
},
source: None,
})?;
Expand Down