interpreter: Make comparisons of different types a hard error

pull/9174/head
Daniel Mensinger 3 years ago
parent b60bd0e299
commit a6c9a151d3
  1. 5
      docs/markdown/snippets/comp_error.md
  2. 12
      mesonbuild/interpreterbase/interpreterbase.py
  3. 11
      test cases/common/16 comparison/meson.build

@ -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.

@ -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 == '!=':

@ -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]''')

Loading…
Cancel
Save