From 4e460f04f3b2d767027c7769bab6eb8029b22422 Mon Sep 17 00:00:00 2001 From: fchin Date: Tue, 12 Nov 2019 12:11:12 +1000 Subject: [PATCH] Fixed issue that the key's value type wasn't checked correctly. Added two new failing tests. --- mesonbuild/interpreterbase.py | 2 +- mesonbuild/mparser.py | 7 ++++--- .../failing/97 add dict non string key/meson.build | 9 +++++++++ .../failing/98 add dict duplicate keys/meson.build | 9 +++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 test cases/failing/97 add dict non string key/meson.build create mode 100644 test cases/failing/98 add dict duplicate keys/meson.build diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py index a04ff38cd..46f578e6e 100644 --- a/mesonbuild/interpreterbase.py +++ b/mesonbuild/interpreterbase.py @@ -735,7 +735,7 @@ The result of this is undefined and will become a hard error in a future Meson r for (key, value) in addition.items(): if isinstance(key, str): new_addition[key] = value - elif isinstance(key, mparser.IdNode): + elif isinstance(key, mparser.IdNode) and isinstance(self.get_variable(key.value), str): FeatureNew('Adding dictionary entry using string variable as key', '0.53.0').use(self.subproject) new_addition[self.get_variable(key.value)] = value else: diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index 2c547d8f2..1baf051b0 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -676,16 +676,17 @@ class Parser: while not isinstance(s, EmptyNode): potential = self.current if self.accept('colon'): + key_value = self.statement() if isinstance(s, StringNode): if s.value in a.kwargs: # + 1 to colno to point to the actual string, not the opening quote raise ParseException('Duplicate dictionary key: {}'.format(s.value), self.getline(), s.lineno, s.colno + 1) - a.set_kwarg(s.value, self.statement()) - elif isinstance(s, IdNode) and isinstance(s.value, str): + a.set_kwarg(s.value, key_value) + elif isinstance(s, IdNode) and isinstance(key_value, StringNode): for key in a.kwargs: if s.value == key.value: raise ParseException('Duplicate dictionary variable key: {}'.format(s.value), self.getline(), s.lineno, s.colno) - a.set_kwarg(s, self.statement()) + a.set_kwarg(s, key_value) else: raise ParseException('Key must be a string or string variable', self.getline(), s.lineno, s.colno) potential = self.current diff --git a/test cases/failing/97 add dict non string key/meson.build b/test cases/failing/97 add dict non string key/meson.build new file mode 100644 index 000000000..c81a3f764 --- /dev/null +++ b/test cases/failing/97 add dict non string key/meson.build @@ -0,0 +1,9 @@ +project('add dictionary entry using non-string key') + +dict = {} + +# An integer variable to be used as a key +key = 1 + +# Add new entry using integer variable as key should fail +dict += {key : 'myValue'} \ No newline at end of file diff --git a/test cases/failing/98 add dict duplicate keys/meson.build b/test cases/failing/98 add dict duplicate keys/meson.build new file mode 100644 index 000000000..7a9b523b0 --- /dev/null +++ b/test cases/failing/98 add dict duplicate keys/meson.build @@ -0,0 +1,9 @@ +project('add dictionary entries with duplicate keys') + +dict = {} + +# A variable to be used as a key +key = 'myKey' + +# Add two entries with duplicate keys should fail +dict += {key : 'myValue1', key : 'myValue2'} \ No newline at end of file