From f93f443a536c36b5a22284cb8d0bb276e766743c Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Wed, 14 Jun 2023 20:36:50 -0400 Subject: [PATCH] detect and warn on non-commutative int/bool operations an int only accepts operations on other ints, just like other primitive types only accept operations on values of the same type. But due to using isinstance in baseobjects "operator_call", an int primitive allowed operations on a bool, even though reversing the operator and having a bool perform operations on an int, would fail with a type error. Really, we should fail with a type error in both directions. But for stability reasons, make this a loud warning and break --fatal-meson-warnings builds. --- mesonbuild/interpreter/primitives/integer.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mesonbuild/interpreter/primitives/integer.py b/mesonbuild/interpreter/primitives/integer.py index f433f5775..b7f353210 100644 --- a/mesonbuild/interpreter/primitives/integer.py +++ b/mesonbuild/interpreter/primitives/integer.py @@ -3,13 +3,8 @@ from __future__ import annotations from ...interpreterbase import ( - ObjectHolder, - MesonOperator, - typed_operator, - noKwargs, - noPosargs, - - InvalidArguments + FeatureBroken, InvalidArguments, MesonOperator, ObjectHolder, + noKwargs, noPosargs, typed_operator, ) import typing as T @@ -53,6 +48,13 @@ class IntegerHolder(ObjectHolder[int]): def display_name(self) -> str: return 'int' + def operator_call(self, operator: MesonOperator, other: TYPE_var) -> TYPE_var: + if isinstance(other, bool): + FeatureBroken.single_use('int operations with non-int', '1.2.0', self.subproject, + 'It is not commutative and only worked because of leaky Python abstractions.', + location=self.current_node) + return super().operator_call(operator, other) + @noKwargs @noPosargs def is_even_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> bool: