From 4b33c9cdb64f360c2ee19691baedfa4d0e32378f Mon Sep 17 00:00:00 2001 From: Volker Weißmann Date: Thu, 31 Jul 2025 16:52:35 +0200 Subject: rewriter: Accept UnknownValue() in more places Fixes #14840 --- mesonbuild/ast/interpreter.py | 7 +++++-- mesonbuild/ast/introspection.py | 8 +++++--- test cases/unit/56 introspection/meson.build | 15 +++++++++++++++ unittests/allplatformstests.py | 16 +++++++++++++++- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/mesonbuild/ast/interpreter.py b/mesonbuild/ast/interpreter.py index 62c4839a7..86b644259 100644 --- a/mesonbuild/ast/interpreter.py +++ b/mesonbuild/ast/interpreter.py @@ -692,8 +692,11 @@ class AstInterpreter(InterpreterBase): def func_get_variable(self, node: BaseNode, args: T.List[TYPE_var], kwargs: T.Dict[str, TYPE_var]) -> T.Any: assert isinstance(node, FunctionNode) var_name = args[0] - assert isinstance(var_name, str) - val = self.get_cur_value(var_name) + if isinstance(var_name, UnknownValue): + val: T.Union[UnknownValue, BaseNode] = UnknownValue() + else: + assert isinstance(var_name, str) + val = self.get_cur_value(var_name) self.dataflow_dag.add_edge(val, node) return val diff --git a/mesonbuild/ast/introspection.py b/mesonbuild/ast/introspection.py index decce4b3a..6ffc6adbe 100644 --- a/mesonbuild/ast/introspection.py +++ b/mesonbuild/ast/introspection.py @@ -203,9 +203,11 @@ class IntrospectionInterpreter(AstInterpreter): has_fallback = 'fallback' in kwargs required = kwargs.get('required', True) version = kwargs.get('version', []) - if not isinstance(version, UnknownValue): - if not isinstance(version, list): - version = [version] + if not isinstance(version, list): + version = [version] + if any(isinstance(el, UnknownValue) for el in version): + version = UnknownValue() + else: assert all(isinstance(el, str) for el in version) version = T.cast(T.List[str], version) assert isinstance(required, (bool, UnknownValue)) diff --git a/test cases/unit/56 introspection/meson.build b/test cases/unit/56 introspection/meson.build index 568d5ccd7..c7856fe2e 100644 --- a/test cases/unit/56 introspection/meson.build +++ b/test cases/unit/56 introspection/meson.build @@ -58,6 +58,21 @@ test('test case 1', t1) test('test case 2', t2, depends: t3) benchmark('benchmark 1', t3, args: [cus1, cus2, cus3]) +### BEGIN: Stuff to test the handling of `UnknownValue` +var_1 = 1 +var_2 = 2 +unknown_var = 'var_1' +if host_machine.system() == 'windows' + unknown_var = 'var_2' +endif +# The IntrospectionInterpreter can't know the value of unknown_var and use the `UnknownValue` class. + +message(get_variable(unknown_var)) + +dependency(unknown_var, version: [unknown_var], required: false) +dependency(unknown_var, version: unknown_var, required: false) +### END: Stuff to test the handling of `UnknownValue` + ### Stuff to test the AST JSON printer foreach x : ['a', 'b', 'c'] if x == 'a' diff --git a/unittests/allplatformstests.py b/unittests/allplatformstests.py index f158be777..04cbfc690 100644 --- a/unittests/allplatformstests.py +++ b/unittests/allplatformstests.py @@ -3834,7 +3834,21 @@ class AllPlatformTests(BasePlatformTests): 'version': ['>=1.0.0', '<=99.9.9'], 'has_fallback': True, 'conditional': True - } + }, + { + 'conditional': False, + 'has_fallback': False, + 'name': 'unknown', + 'required': False, + 'version': 'unknown' + }, + { + 'conditional': False, + 'has_fallback': False, + 'name': 'unknown', + 'required': False, + 'version': 'unknown' + }, ] self.maxDiff = None self.assertListEqual(res_nb, expected) -- cgit v1.2.3