Commit Graph

47 Commits

Author SHA1 Message Date
Josh Soref cf9fd56bc9 fix various spelling issues
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2023-04-11 19:21:05 -04:00
Eli Schwartz 8fc4649bfe
propagate the most accurate node to error messages
During evaluation of codeblocks, we start off with an iteration of
nodes, and then while evaluating them we may update the global
self.current_node context. When catching and formatting errors, we
didn't take into account that the node might be updated from the
original top-level iteration.

Switch to formatting errors using self.current_node instead, to ensure
we can point at the likely most-accurate actual cause of an error.

Also update the current node in a few more places, so that function
calls always see the function call as the current node, even if the most
recently parsed node was an argument to the function call.

Fixes #11643
2023-04-04 15:06:17 -04:00
Eli Schwartz 314382d6ff
handle meson_version after parsing but before invalid project() kwargs
If we add new kwargs to a function invoked on the first line, we also
need to validate the meson_version before erroring out due to unknown
kwargs. Even if the AST was successfully built.

Amusingly, we also get to improve the error message a bit. By passing
the AST node instead of an interpreter node, we get not just line
numbers, but also column offsets of the issueful meson_version. That
broke the stdout of another failing test; adapt it.
2023-03-01 23:36:31 -05:00
Eli Schwartz 878c1604e6
handle meson_version even when the build file fails to parse
If the meson.build file is sufficiently "broken", even attempting to lex
and parse it will totally fail, and we error out without getting the
opportunity to evalaute the project() function. This can fairly easily
happen if we add new grammar to the syntax, which old versions of meson
cannot understand. Setting a minimum meson_version doesn't help, because
people with a too-old version of meson get parser errors instead of
advice about upgrading meson.

Examples of this include adding dict support to meson.

There are two general approaches to solving this issue, one of which
projects are empowered to do:

- refactor the project to place too-new syntax in a subdir() loaded
  build file, so the root file can be interpreted

- teach meson to catch errors in building the initial AST, and just load
  enough of the AST to check for meson_version advice

This implements the latter, allowing to future-proof the build
grammar.
2023-03-01 23:30:49 -05:00
Xavier Claessens f0dc61a764 interpreter: Add testcase..endtestcase clause support
This is currently only enabled when running unit tests to facilitate
writing failing unit tests.

Fixes: #11394
2023-03-01 20:13:34 -05:00
Charles Brunet 5dbe49aa33 prevent unhandled exception for operations on None
For instance, when writing `-subdir('a')` or `not subdir('a')`.

Fixes #11225.
2023-02-27 13:40:16 -05:00
Eli Schwartz a21af43200
micro-optimize: define typing-only objects in TYPE_CHECKING
Union types that exist solely for use as annotations don't need to be
created in normal runs.
2023-02-01 17:01:30 -05:00
Dylan Baker a5d547e0d9
pylint: enable useless-return 2022-11-29 23:26:05 -05:00
Eli Schwartz 78bf1ec56e
fix obscure crash on unbound variable
This can be triggered if someone tries to call a non-ID. The example
reproducer was:

```
if (var = dependency(...)).found()
```

This produced a traceback ending in

```
    raise InvalidArguments(f'Variable "{object_name}" is not callable.')
UnboundLocalError: local variable 'object_name' referenced before assignment
```

After this commit, the error is reported as:

```
ERROR: AssignmentNode is not callable.
```
2022-08-30 15:30:55 -04:00
Peter Lesslie d771fc7d0b Add support for multiline f-strings
+ Extend the parser to recognize the multiline f-strings, which the
documentation already implies will work.

The syntax is like:
```
x = 'hello'
y = 'world'

msg = f'''This is a multiline string.

Sending a message: '@x@ @y@'
'''
```

which produces:
```
This is a multiline string.

Sending a message: 'hello world'

```

+ Added some f-string tests cases to "62 string arithmetic" to exercise
the new behavior.
2022-05-01 12:47:37 -04:00
Eli Schwartz c231c4bd2a
move a bunch of imports into TYPE_CHECKING blocks
These are only used for type checking, so don't bother importing them at
runtime.

Generally add future annotations at the same time, to make sure that
existing uses of these imports don't need to be quoted.
2022-03-07 19:09:50 -05:00
Eli Schwartz fec7265b5c
flake8: use plain strings instead of f-strings when no variables used 2022-02-16 18:19:12 -05:00
Eli Schwartz c0b8e02d9f
FeatureNew: add mypy type annotations for subproject arg
Use a derived type when passing `subproject` around, so that mypy knows
it's actually a SubProject, not a str. This means that passing anything
other than a handle to the interpreter state's subproject attribute
becomes a type violation, specifically when the order of the *four*
different str arguments is typoed.
2022-02-14 20:40:41 -05:00
Eli Schwartz 3990dc6d64 flake8: fix indentation style 2022-01-27 10:48:01 -08:00
Eli Schwartz 038b31e72b
various manual conversion of percent-formatted strings to f-strings 2021-11-01 20:26:18 -04:00
Eli Schwartz f4b91c4306 Revert "mark a couple of typing-only imports as noqa, to appease pyflakes"
This reverts commit 6cc1b8441c.

The latest version of pyflakes learned to detect that correctly.
2021-10-27 09:51:52 -04:00
Daniel Mensinger 2d8da3cb11 interpreter: Fix missing featuer check (fixes #9425) 2021-10-24 09:58:20 -04:00
Daniel Mensinger af0587cb49 interpreter: Holderify arrays and dicts
This is the final refactoring for extracting the bultin object
logic out of Interpreterbase. I decided to do both arrays and
dicts in one go since splitting it would have been a lot more
confusing.
2021-10-06 22:37:18 +02:00
Daniel Mensinger 6b00c7dc81 Remove helpers.check_stringlist() 2021-09-25 12:44:11 +02:00
Daniel Mensinger d93d01b6c5 interpreter: Introduce StringHolder
Another commit in my quest to rid InterpreterBase from all higher
level object processing logic.

Additionally, there is a a logic change here, since `str.join` now
uses varargs and can now accept more than one argument (and supports
list flattening).
2021-09-25 12:44:11 +02:00
Eli Schwartz 6cc1b8441c
mark a couple of typing-only imports as noqa, to appease pyflakes
Since it cannot resolve `import typing as T` in order to figure out that
T.* is doing annotation-worthy stuff.

Since T.cast('Foo') is not actually using Foo except in an annotation
context (due to being a string) it requires extra work to resolve, and
the only thing that would currently work is actually using
'typing.cast'. However, we have decided to not use it except as T...

Since this import is only imported during mypy it's not so bad to noqa
it.
2021-09-14 15:55:03 -04:00
Daniel Mensinger 43302d3296 interpreter: Introduce BooleanHolder for the bool primitive 2021-09-01 19:17:01 +02:00
Daniel Mensinger 86eda3c812 interpreter: Simplify Disabler logic 2021-09-01 19:17:01 +02:00
Daniel Mensinger 8d92e6d865 interpreter: Add IntegerHolder 2021-08-31 23:01:21 +02:00
Daniel Mensinger 86f70c873a interpreter: Introduce operators support for InterpreterObjects 2021-08-31 23:01:21 +02:00
Daniel Mensinger a6c9a151d3 interpreter: Make comparisons of different types a hard error 2021-08-31 23:01:21 +02:00
Dylan Baker 4d7031437c pylint: turn on superflous-parens
We have a lot of these. Some of them are harmless, if unidiomatic, such
as `if (condition)`, others are potentially dangerous `assert(...)`, as
`assert(condtion)` works as expected, but `assert(condition, message)`
will result in an assertion that never triggers, as what you're actually
asserting is `bool(tuple[2])`, which will always be true.
2021-08-31 16:28:54 -04:00
Xavier Claessens f8cfd91d71 Simplify get_callee_args 2021-08-30 14:00:54 -04:00
Eli Schwartz 2a7125928e
use even more informative error message for invoking meson in a subdir
Follow-up on commit 5a7b8d86d0

Sometimes, we find a parent meson.build which is also malformed, and we
shouldn't suggest that maybe the user meant to use that, if it isn't a
valid project() either. Do a rough and dirty check to see if the very
first line is a project() declaration, and if not, don't try to be
clever and suggest using it.

The "invalid source tree" error suffices here, since we're not
absolutely sure meson can be successfully run in that parent directory
and actually advising people about the wrong location is a lot more
confusing than just saying "please figure this out yourself, here is
what to look for".

Granted, we miss cases where project() comes after blank lines and/or
comments, but doing a full AST parse here is excessively overkill and
probably too painful to do, and we don't need to be *that* clever. So
let's be content with merely going above and beyond the call of duty.
2021-08-22 12:12:26 -04:00
Eli Schwartz 5a7b8d86d0 use a more informative error message for invoking meson in a subdir
Explicitly mention that the project definition is invalid, and clarify
that project is `project()` -- a function.

Also try to walk the directory tree upward, and if there are parent
meson.build files, just say this isn't the project root, and "maybe you
meant to run meson there instead?"

This won't catch calls to subdir('foo/bar') but we can't be perfect,
only better than before and catch the *majority* of such cases, and
hopefully it's a lot more clear if meson protests that the project is
"invalid, there is no project() function", where the user should look
for a potential solution.

Fixes #3426
2021-08-22 12:49:11 +03:00
Tristan Partin 1dc13e9951 Add unset_variable()
This should be useful for helping to control variable scope within
Meson. CMake has something similar for controlling scope.
2021-08-16 14:05:13 -04:00
Daniel Mensinger 5c87167a34 interpreter: Fix list contains for Holders (fixes #9020 #9047) 2021-08-09 09:40:35 -04:00
Eli Schwartz bd6f46e723 condense lines 2021-07-05 17:55:04 +03:00
Eli Schwartz dd31891c1f more f-strings too complex to be caught by pyupgrade 2021-07-05 17:55:04 +03:00
Daniel Mensinger 971a0b1775 fix: get_variable default variables are not ObjectHolders (fixes #8936) 2021-07-02 17:14:44 +03:00
Daniel Mensinger 3e396b3782
fix: Always explicitly set encoding for text files (fixes #8263) 2021-06-29 11:28:08 +02:00
Daniel Mensinger 8f7343831b refactor: Refactor BothLibraries logic
This commit introduces a new type of `HoldableObject`: The
`SecondLevelHolder`. The primary purpose of this class is
to handle cases where two (or more) `HoldableObject`s are
stored at the same time (with one default object). The
best (and currently only) example here is the `BothLibraries`
class.
2021-06-26 12:49:35 +02:00
Daniel Mensinger 0358445b6e
fix: dicts and list need _holderify for fallback 2021-06-20 00:26:06 +02:00
Daniel Mensinger f1ac7db2e6
fix: Fix set_variable not holderifying (fixes #8904) 2021-06-20 00:26:02 +02:00
Daniel Mensinger c2c7f7c9d7 holders: Ensure that InterpreterBase is the sole instance for (un)holderifying 2021-06-18 23:48:33 +02:00
Daniel Mensinger d601227cb2 interpreter: Add FileHolder and remove specical case for File 2021-06-18 23:48:33 +02:00
Daniel Mensinger 7be172fe16 interpreter: Meson does not have floats --> remove them 2021-06-18 23:48:33 +02:00
Daniel Mensinger e686545fce interpreter: Split decorators from interpreterbase.py 2021-06-11 10:42:18 +02:00
Daniel Mensinger fc0397b309 interpreter: Split Disabler from interpreterbase.py 2021-06-11 10:42:18 +02:00
Daniel Mensinger 687eebee29 interpreter: Split base objects and helpers from interpreterbase.py 2021-06-11 10:42:18 +02:00
Daniel Mensinger 2e3550db14 interpreter: Split exception calsses from interpreterbase.py 2021-06-11 10:42:18 +02:00
Daniel Mensinger ad584f022d interpreter: Move interpreterbase.py into a new package 2021-06-11 10:42:18 +02:00