diff --git a/docs/markdown/snippets/fstrings.md b/docs/markdown/snippets/fstrings.md new file mode 100644 index 000000000..a797aaab5 --- /dev/null +++ b/docs/markdown/snippets/fstrings.md @@ -0,0 +1,7 @@ +## Introducing format strings to the Meson language + +In addition to the conventional `'A string @0@ to be formatted @1@'.format(n, m)` +method of formatting strings in the Meson DSL, there's now the additional +`f'A string @n@ to be formatted @m@'` notation that provides a non-positional +and clearer alternative. Meson's format strings are currently restricted to +identity-expressions, meaning `f'format {'m' + 'e'}'` will not parse. diff --git a/mesonbuild/interpreterbase.py b/mesonbuild/interpreterbase.py index bc0c93213..206a81e10 100644 --- a/mesonbuild/interpreterbase.py +++ b/mesonbuild/interpreterbase.py @@ -936,7 +936,7 @@ The result of this is undefined and will become a hard error in a future Meson r try: val = self.variables[var] if not isinstance(val, (str, int, float, bool)): - raise InvalidCode(f'Identifier {var} does not name a formattable variable.') + raise InvalidCode(f'Identifier "{var}" does not name a formattable variable.') return str(val) except KeyError: diff --git a/test cases/failing/113 invalid fstring/meson.build b/test cases/failing/113 invalid fstring/meson.build new file mode 100644 index 000000000..dd22f56b2 --- /dev/null +++ b/test cases/failing/113 invalid fstring/meson.build @@ -0,0 +1,4 @@ +project('invalid-fstring', 'c') + +dict = {'key': true} +s = f'invalid fstring: @dict@' diff --git a/test cases/failing/113 invalid fstring/test.json b/test cases/failing/113 invalid fstring/test.json new file mode 100644 index 000000000..3b3516556 --- /dev/null +++ b/test cases/failing/113 invalid fstring/test.json @@ -0,0 +1,7 @@ +{ + "stdout": [ + { + "line": "test cases/failing/113 invalid fstring/meson.build:4:0: ERROR: Identifier \"dict\" does not name a formattable variable." + } + ] +} diff --git a/test cases/failing/114 invalid fstring/meson.build b/test cases/failing/114 invalid fstring/meson.build new file mode 100644 index 000000000..973df3098 --- /dev/null +++ b/test cases/failing/114 invalid fstring/meson.build @@ -0,0 +1,3 @@ +project('invalid-fstring', 'c') + +z = f'invalid fstring: @foo@' diff --git a/test cases/failing/114 invalid fstring/test.json b/test cases/failing/114 invalid fstring/test.json new file mode 100644 index 000000000..2a8155c41 --- /dev/null +++ b/test cases/failing/114 invalid fstring/test.json @@ -0,0 +1,7 @@ +{ + "stdout": [ + { + "line": "test cases/failing/114 invalid fstring/meson.build:3:0: ERROR: Identifier \"foo\" does not name a variable." + } + ] +}