Skip to content

Commit

Permalink
feat: only allow adding fields to non-sealed native structs
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Aug 12, 2024
1 parent a3e7d20 commit e6ede96
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions compiler/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ pub enum Diagnostic {
#[error("this cast is redundant, '{0}' is already a '{1}'")]
RedundantDynCast(Ident, Ident, Span),
#[error(
"'{0}' is a native struct which is not known to have a valid and complete script \
"'{0}' is a native struct which is not known to have a complete and valid script \
definition, passing arguments to its constructor might result in undefined behavior, \
it should be preferred to construct such types without arguments: 'new {0}()'"
)]
NonSealedStructConstruction(Ident, Span),
#[error("adding fields to this struct is not permitted")]
#[error("adding fields to sealed native structs is not permitted")]
AddingFieldToSealedStruct(Span),
#[error("adding fields to scripted structs is not permitted")]
AddingFieldToScriptedStruct(Span),
#[error("syntax error, expected {0}")]
SyntaxError(ExpectedSet, Span),
#[error("{0}")]
Expand Down Expand Up @@ -157,6 +159,7 @@ impl Diagnostic {
| Self::RedundantDynCast(_, _, span)
| Self::NonSealedStructConstruction(_, span)
| Self::AddingFieldToSealedStruct(span)
| Self::AddingFieldToScriptedStruct(span)
| Self::CompileError(_, span)
| Self::SyntaxError(_, span)
| Self::CteError(_, span) => *span,
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,9 @@ impl<'a> CompilationUnit<'a> {
&& SEALED_STRUCTS.contains(&*self.pool.def_name(target_type)?)
{
self.diagnostics.push(Diagnostic::AddingFieldToSealedStruct(decl.span));
} else if class.flags.is_struct() && !class.flags.is_native() {
self.diagnostics
.push(Diagnostic::AddingFieldToScriptedStruct(decl.span));
}

self.define_field(index, target_type, class.flags, visibility, source, scope)?;
Expand Down

0 comments on commit e6ede96

Please sign in to comment.