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.
pull/11879/head
Eli Schwartz 1 year ago
parent d558291abe
commit f93f443a53
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 16
      mesonbuild/interpreter/primitives/integer.py

@ -3,13 +3,8 @@
from __future__ import annotations from __future__ import annotations
from ...interpreterbase import ( from ...interpreterbase import (
ObjectHolder, FeatureBroken, InvalidArguments, MesonOperator, ObjectHolder,
MesonOperator, noKwargs, noPosargs, typed_operator,
typed_operator,
noKwargs,
noPosargs,
InvalidArguments
) )
import typing as T import typing as T
@ -53,6 +48,13 @@ class IntegerHolder(ObjectHolder[int]):
def display_name(self) -> str: def display_name(self) -> str:
return 'int' 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 @noKwargs
@noPosargs @noPosargs
def is_even_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> bool: def is_even_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> bool:

Loading…
Cancel
Save