From e6ede968bd1461ec2f3b4af5428f1ca8ebf9eb77 Mon Sep 17 00:00:00 2001 From: jekky Date: Mon, 12 Aug 2024 22:23:40 +0100 Subject: [PATCH] feat: only allow adding fields to non-sealed native structs --- compiler/src/diagnostics.rs | 7 +++++-- compiler/src/unit.rs | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/src/diagnostics.rs b/compiler/src/diagnostics.rs index b991f48..e227dc2 100644 --- a/compiler/src/diagnostics.rs +++ b/compiler/src/diagnostics.rs @@ -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}")] @@ -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, diff --git a/compiler/src/unit.rs b/compiler/src/unit.rs index 16e0c99..09c2ae6 100644 --- a/compiler/src/unit.rs +++ b/compiler/src/unit.rs @@ -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)?;