Skip to content

Commit

Permalink
clean up FeatureCheck signature to move location to use time
Browse files Browse the repository at this point in the history
The point of a .use() function is because we don't always have the
information we need to use a feature check, so we allow creating the
feature and then storing it for later use. When implementing location
checks, although it is optional, actually using it violated that design.

Move the location out of the init method for FeatureCheck itself. It
remains compatible with all cases of .single_use(), but fix the rest up.
  • Loading branch information
eli-schwartz authored and dcbaker committed Mar 1, 2022
1 parent 2b04e8c commit d39b330
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def warn_if_has_name(self, name: str) -> None:
# Multiple append/prepend operations was not supported until 0.58.0.
if self.held_object.has_name(name):
m = f'Overriding previous value of environment variable {name!r} with a new one'
FeatureNew(m, '0.58.0', location=self.current_node).use(self.subproject)
FeatureNew(m, '0.58.0').use(self.subproject, self.current_node)

@typed_pos_args('environment.set', str, varargs=str, min_varargs=1)
@typed_kwargs('environment.set', ENV_SEPARATOR_KW)
Expand Down Expand Up @@ -480,7 +480,7 @@ def partial_dependency_method(self, args: T.List[TYPE_nvar], kwargs: 'kwargs.Dep
def variable_method(self, args: T.Tuple[T.Optional[str]], kwargs: 'kwargs.DependencyGetVariable') -> T.Union[str, T.List[str]]:
default_varname = args[0]
if default_varname is not None:
FeatureNew('Positional argument to dependency.get_variable()', '0.58.0', location=self.current_node).use(self.subproject)
FeatureNew('Positional argument to dependency.get_variable()', '0.58.0').use(self.subproject, self.current_node)
return self.held_object.get_variable(
cmake=kwargs['cmake'] or default_varname,
pkgconfig=kwargs['pkgconfig'] or default_varname,
Expand Down
24 changes: 11 additions & 13 deletions mesonbuild/interpreterbase/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,10 @@ class FeatureCheckBase(metaclass=abc.ABCMeta):
feature_registry: T.ClassVar[T.Dict[str, T.Dict[str, T.Set[T.Tuple[str, T.Optional['mparser.BaseNode']]]]]]
emit_notice = False

def __init__(self, feature_name: str, feature_version: str, extra_message: str = '', location: T.Optional['mparser.BaseNode'] = None):
def __init__(self, feature_name: str, feature_version: str, extra_message: str = ''):
self.feature_name = feature_name # type: str
self.feature_version = feature_version # type: str
self.extra_message = extra_message # type: str
self.location = location

@staticmethod
def get_target_version(subproject: str) -> str:
Expand All @@ -601,7 +600,7 @@ def get_target_version(subproject: str) -> str:
def check_version(target_version: str, feature_version: str) -> bool:
pass

def use(self, subproject: 'SubProject') -> None:
def use(self, subproject: 'SubProject', location: T.Optional['mparser.BaseNode'] = None) -> None:
tv = self.get_target_version(subproject)
# No target version
if tv == '':
Expand All @@ -616,7 +615,7 @@ def use(self, subproject: 'SubProject') -> None:
if self.feature_version not in register:
register[self.feature_version] = set()

feature_key = (self.feature_name, self.location)
feature_key = (self.feature_name, location)
if feature_key in register[self.feature_version]:
# Don't warn about the same feature multiple times
# FIXME: This is needed to prevent duplicate warnings, but also
Expand All @@ -626,7 +625,7 @@ def use(self, subproject: 'SubProject') -> None:
# Target version is new enough, don't warn even if it is registered for notice
if self.check_version(tv, self.feature_version):
return
self.log_usage_warning(tv)
self.log_usage_warning(tv, location)

@classmethod
def report(cls, subproject: str) -> None:
Expand All @@ -646,7 +645,7 @@ def report(cls, subproject: str) -> None:
if '\n' in warning_str:
mlog.warning(warning_str)

def log_usage_warning(self, tv: str) -> None:
def log_usage_warning(self, tv: str, location: T.Optional['mparser.BaseNode']) -> None:
raise InterpreterException('log_usage_warning not implemented')

@staticmethod
Expand All @@ -663,16 +662,15 @@ def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
node, _, _, subproject = get_callee_args(wrapped_args)
if subproject is None:
raise AssertionError(f'{wrapped_args!r}')
self.location = node
self.use(subproject)
self.use(subproject, node)
return f(*wrapped_args, **wrapped_kwargs)
return T.cast(TV_func, wrapped)

@classmethod
def single_use(cls, feature_name: str, version: str, subproject: 'SubProject',
extra_message: str = '', location: T.Optional['mparser.BaseNode'] = None) -> None:
"""Oneline version that instantiates and calls use()."""
cls(feature_name, version, extra_message, location).use(subproject)
cls(feature_name, version, extra_message).use(subproject, location)


class FeatureNew(FeatureCheckBase):
Expand All @@ -695,7 +693,7 @@ def get_warning_str_prefix(tv: str) -> str:
def get_notice_str_prefix(tv: str) -> str:
return ''

def log_usage_warning(self, tv: str) -> None:
def log_usage_warning(self, tv: str, location: T.Optional['mparser.BaseNode']) -> None:
args = [
'Project targeting', f"'{tv}'",
'but tried to use feature introduced in',
Expand All @@ -704,7 +702,7 @@ def log_usage_warning(self, tv: str) -> None:
]
if self.extra_message:
args.append(self.extra_message)
mlog.warning(*args, location=self.location)
mlog.warning(*args, location=location)

class FeatureDeprecated(FeatureCheckBase):
"""Checks for deprecated features"""
Expand All @@ -728,7 +726,7 @@ def get_warning_str_prefix(tv: str) -> str:
def get_notice_str_prefix(tv: str) -> str:
return 'Future-deprecated features used:'

def log_usage_warning(self, tv: str) -> None:
def log_usage_warning(self, tv: str, location: T.Optional['mparser.BaseNode']) -> None:
args = [
'Project targeting', f"'{tv}'",
'but tried to use feature deprecated since',
Expand All @@ -737,7 +735,7 @@ def log_usage_warning(self, tv: str) -> None:
]
if self.extra_message:
args.append(self.extra_message)
mlog.warning(*args, location=self.location)
mlog.warning(*args, location=location)


# This cannot be a dataclass due to https://github.com/python/mypy/issues/5374
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/modules/unstable_external_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(self,

def _configure(self, state: 'ModuleState') -> None:
if self.configure_command == 'waf':
FeatureNew('Waf external project', '0.60.0', location=state.current_node).use(self.subproject)
FeatureNew('Waf external project', '0.60.0').use(self.subproject, state.current_node)
waf = state.find_program('waf')
configure_cmd = waf.get_command()
configure_cmd += ['configure', '-o', str(self.build_dir)]
Expand Down Expand Up @@ -176,7 +176,7 @@ def _validate_configure_options(self, variables: T.List[T.Tuple[str, str, str]],
if key_format in option:
break
else:
FeatureNew('Default configure_option', '0.57.0', location=state.current_node).use(self.subproject)
FeatureNew('Default configure_option', '0.57.0').use(self.subproject, state.current_node)
self.configure_options.append(default)

def _format_options(self, options: T.List[str], variables: T.List[T.Tuple[str, str, str]]) -> T.List[str]:
Expand Down

0 comments on commit d39b330

Please sign in to comment.