From a6c9a151d3ce51d53fc7f34722a7422c3c0faff4 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Sun, 29 Aug 2021 10:25:40 +0200 Subject: [PATCH] interpreter: Make comparisons of different types a hard error --- docs/markdown/snippets/comp_error.md | 5 +++++ mesonbuild/interpreterbase/interpreterbase.py | 12 ++++++++---- test cases/common/16 comparison/meson.build | 11 ----------- 3 files changed, 13 insertions(+), 15 deletions(-) create mode 100644 docs/markdown/snippets/comp_error.md diff --git a/docs/markdown/snippets/comp_error.md b/docs/markdown/snippets/comp_error.md new file mode 100644 index 000000000..6d1bf4681 --- /dev/null +++ b/docs/markdown/snippets/comp_error.md @@ -0,0 +1,5 @@ +## Comparing two objects with different types is now an error + +Using the `==` and `!=` operators to compare objects of different (for instance +`[1] == 1`) types was deprecated and undefined behavior since 0.45.0 and is +now a hard error. diff --git a/mesonbuild/interpreterbase/interpreterbase.py b/mesonbuild/interpreterbase/interpreterbase.py index 2fa77ee7c..289e1d76e 100644 --- a/mesonbuild/interpreterbase/interpreterbase.py +++ b/mesonbuild/interpreterbase/interpreterbase.py @@ -47,6 +47,7 @@ from ._unholder import _unholder import os, copy, re, pathlib import typing as T +import textwrap if T.TYPE_CHECKING: from ..interpreter import Interpreter @@ -306,11 +307,14 @@ class InterpreterBase: valid = self.validate_comparison_types(val1, val2) # Ordering comparisons of different types isn't allowed since PR #1810 # (0.41.0). Since PR #2884 we also warn about equality comparisons of - # different types, which will one day become an error. + # different types, which is now an error. if not valid and (node.ctype == '==' or node.ctype == '!='): - mlog.warning('''Trying to compare values of different types ({}, {}) using {}. -The result of this is undefined and will become a hard error in a future Meson release.''' - .format(type(val1).__name__, type(val2).__name__, node.ctype), location=node) + raise InvalidArguments(textwrap.dedent( + f''' + Trying to compare values of different types ({type(val1).__name__}, {type(val2).__name__}) using {node.ctype}. + This was deprecated and undefined behavior previously and is as of 0.60.0 a hard error. + ''' + )) if node.ctype == '==': return val1 == val2 elif node.ctype == '!=': diff --git a/test cases/common/16 comparison/meson.build b/test cases/common/16 comparison/meson.build index bba01684a..1d250f9ca 100644 --- a/test cases/common/16 comparison/meson.build +++ b/test cases/common/16 comparison/meson.build @@ -127,17 +127,6 @@ test('equaltrue', exe14) test('nequaltrue', exe15) test('nequalfalse', exe16) -# Equality comparisons of different elementary types -# (these all cause warnings currently, will become an error in future) - -assert([] != 'st', 'not equal') -assert([] != 1, 'not equal') -assert(2 != 'st', 'not equal') - -assert(not ([] == 'st'), 'not equal') -assert(not ([] == 1), 'not equal') -assert(not (2 == 'st'), 'not equal') - # "in" and "not in" operators assert(1 in [1, 2], '''1 should be in [1, 2]''')