From 9776597f710c407990523e0d9823e399c0d79f1a Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Fri, 11 Oct 2019 16:48:29 +0200 Subject: [PATCH] parser: `owned` is not allowed on Constants This avoids criticals and broken c-code. --- tests/Makefile.am | 2 ++ tests/parser/constant-local-owned.test | 5 +++++ tests/parser/constant-owned.test | 6 ++++++ vala/valaparser.vala | 8 ++++++++ 4 files changed, 21 insertions(+) create mode 100644 tests/parser/constant-local-owned.test create mode 100644 tests/parser/constant-owned.test diff --git a/tests/Makefile.am b/tests/Makefile.am index ba8f2b0c6..5b56bb89d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -538,6 +538,8 @@ TESTS = \ parser/assignment.vala \ parser/attribute-duplicate.test \ parser/attribute-wrong-number.test \ + parser/constant-owned.test \ + parser/constant-local-owned.test \ parser/constructor-class-exists.test \ parser/constructor-exists.test \ parser/constructor-no-new.test \ diff --git a/tests/parser/constant-local-owned.test b/tests/parser/constant-local-owned.test new file mode 100644 index 000000000..c8d696454 --- /dev/null +++ b/tests/parser/constant-local-owned.test @@ -0,0 +1,5 @@ +Invalid Code + +void main () { + const owned string FOO = "foo"; +} diff --git a/tests/parser/constant-owned.test b/tests/parser/constant-owned.test new file mode 100644 index 000000000..41e7ed200 --- /dev/null +++ b/tests/parser/constant-owned.test @@ -0,0 +1,6 @@ +Invalid Code + +const owned string FOO = "foo"; + +void main () { +} diff --git a/vala/valaparser.vala b/vala/valaparser.vala index 0a424333a..fe4e16915 100644 --- a/vala/valaparser.vala +++ b/vala/valaparser.vala @@ -1883,6 +1883,10 @@ public class Vala.Parser : CodeVisitor { expect (TokenType.ASSIGN); var initializer = parse_expression (); + if (type.value_owned) { + Report.error (src, "`owned' is not allowed on constants"); + } + return new Constant (id, type, initializer, src); } @@ -2642,6 +2646,10 @@ public class Vala.Parser : CodeVisitor { Report.warning (c.source_reference, "the modifier `static' is not applicable to constants"); } + if (type.value_owned) { + Report.error (c.source_reference, "`owned' is not allowed on constants"); + } + if (accept (TokenType.ASSIGN)) { c.value = parse_expression (); } -- 2.47.2