Commit Graph

49 Commits

Author SHA1 Message Date
DaniPopes 20b0d20135 Fix typos in compiler 2023-04-10 22:02:52 +02:00
Nilstrieb efbf7cdc44 Improve heuristics for format_args literal being suggestable
Sometimes, we want to create subspans and point at code in the literal
if possible. But this doesn't always make sense, sometimes the literal
may come from macro expanded code and isn't actually there in the
source. Then, we can't really make these suggestions.

This now makes sure that the literal is actually there as we see it so
that we will not run into ICEs on weird literal transformations.
2023-03-14 13:20:39 +00:00
Nilstrieb b674bbeb26 Properly allow macro expanded format_args invocations to uses captures
Originally, this was kinda half-allowed. There were some primitive
checks in place that looked at the span to see whether the input was
likely a literal. These "source literal" checks are needed because the
spans created during `format_args` parsing only make sense when it is
indeed a literal that was written in the source code directly.

This is orthogonal to the restriction that the first argument must be a
"direct literal", not being exanpanded from macros. This restriction was
imposed by [RFC 2795] on the basis of being too confusing. But this was
only concerned with the argument of the invocation being a literal, not
whether it was a source literal (maybe in spirit it meant it being a
source literal, this is not clear to me).

Since the original check only really cared about source literals (which
is good enough to deny the `format_args!(concat!())` example), macros
expanding to `format_args` invocations were able to use implicit
captures if they spanned the string in a way that lead back to a source
string.

The "source literal" checks were not strict enough and caused ICEs in
certain cases (see # 106191 (the space is intended to avoid spammy
backreferences)). So I tightened it up in # 106195 to really only work
if it's a direct source literal.

This caused the `indoc` crate to break. `indoc` transformed the source
literal by removing whitespace, which made it not a "source literal"
anymore (which is required to fix the ICE). But since `indoc` spanned
the literal in ways that made the old check think that it's a literal,
it was able to use implicit captures (which is useful and nice for the
users of `indoc`).

This commit properly seperates the previously introduced concepts of
"source literal" and "direct literal" and therefore allows `indoc`
invocations, which don't create "source literals" to use implicit
captures again.

[RFC 2795]: https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html#macro-hygiene
2023-03-14 13:16:52 +00:00
Maybe Waffle bb96767409 Replace some thens with some then_somes 2023-02-16 15:26:03 +00:00
Maybe Waffle 76cf68c470 if $c:expr { Some($r:expr) } else { None } =>> $c.then(|| $r) 2023-02-16 15:26:00 +00:00
Matthias Krüger de1fe6fad1 remove redundant clones 2023-02-10 18:08:25 +01:00
Matthias Krüger afb42d445c Rollup merge of #106805 - madsravn:master, r=compiler-errors
Suggest `{var:?}` when finding `{?:var}` in inline format strings

Link to issue: https://github.com/rust-lang/rust/issues/106572

This is my first PR to this project, so hopefully I can get some good pointers with me from the first PR.

Currently my idea was to test out whether or not this is the correct solution to this issue and then hopefully expand upon the idea to not only work for Debug formatting but for all of  them. If this is a valid solution, I will create a new issue to give a better error message to a broader range of wrong-order formatting.
2023-02-03 06:30:23 +01:00
Mads Ravn 7acb6bf2ed PR fixing wrong order of format parameters in strings. Issue #106572
Adding

Adding

Fixing small issues for PR

Adding tests

Removing unused binding

Changing the wording on note

Fixing PR comment
2023-02-02 12:56:04 +01:00
Mara Bos 925eb6ee8f Replace format flags u32 by enums and bools. 2023-01-27 08:53:39 +01:00
Nilstrieb 2c4ed1e31c Revert "Improve heuristics whether format_args string is a source literal"
This reverts commit df074824ac.

Keeps the code improvements from the PR and the test (as a known-bug).
2023-01-18 20:33:17 +01:00
nils ab1b1b2231 Fix uninlined_format_args for some compiler crates
Convert all the crates that have had their diagnostic migration
completed (except save_analysis because that will be deleted soon and
apfloat because of the licensing problem).
2023-01-05 19:01:12 +01:00
Nilstrieb 9b366ff031 Add enum for find_width_map_from_snippet
This makes the relationship between the vec and the boolean clearer.
2022-12-28 17:43:35 +01:00
Nilstrieb df074824ac Improve heuristics whether format_args string is a source literal
Previously, it only checked whether there was _a_ literal at the span of
the first argument, not whether the literal actually matched up. This
caused issues when a proc macro was generating a different literal with
the same span.

This requires an annoying special case for literals ending in `\n`
because otherwise `println` wouldn't give detailed diagnostics anymore
which would be bad.
2022-12-28 17:43:18 +01:00
bors 6d34d42d7b Auto merge of #103828 - cassaundra:fix-format-args-span2, r=cjgillot
Fix incorrect span when using byte-escaped rbrace

Fix #103826, a format args span issue introduced in #102214.

The current solution for tracking skipped characters made it so that certain situations were ambiguous enough that the original span couldn't be worked out later. This PR improves on the original solution by keeping track of groups of skipped characters using a map, and fixes the previous bug. See an example of this ambiguity in the [previous PR's discussion](https://github.com/rust-lang/rust/pull/102214#issuecomment-1258711015).
2022-12-26 12:03:47 +00:00
Maybe Waffle bf4e3cb32d Assert size of rustc_parse_format::Piece<'_> 2022-12-08 22:50:39 +00:00
Maybe Waffle d4ac9dc896 Box rustc_parse_format::Piece::NextArgument
This makes both variants closer together in size (previously they were
different by 208 bytes -- 16 vs 224). This may make things worse, but
it's worth a try.
2022-12-06 12:02:56 +00:00
Cassaundra Smith 0b414ecb3c Fix incorrect span when using byte-escaped rbrace
Fix #103826.
2022-11-21 20:37:09 -08:00
Takayuki Maeda c0bb2ec92a shift no characters when using raw string literals
remove `find_skips`

remove unnecessary variables
2022-11-15 14:28:20 +09:00
Alex Macleod 3e9fca746f Fix rustc_parse_format spans following escaped utf-8 multibyte chars 2022-10-27 18:20:56 +00:00
Colin Baumgarten df0423cc1c Detect and reject out-of-range integers in format string literals
Until now out-of-range integers in format string literals
were silently ignored. They wrapped around to zero at
usize::MAX, producing unexpected results.

When using debug builds of rustc, such integers in format string
literals even cause an 'attempt to add with overflow' panic in
rustc.

Fix this by producing an error diagnostic for integers in format
string literals which do not fit into usize.

Fixes #102528
2022-10-01 01:05:01 +02:00
Cassaundra Smith 88119a6289 Fix span of byte-escaped left format args brace
Fix #102057.
2022-09-28 13:47:17 -07:00
Mara Bos adb0278b1d Separate CountIsStar from CountIsParam in rustc_parse_format. 2022-08-25 14:49:09 +02:00
Alex Macleod 502a50548a Fix rustc_parse_format precision & width spans 2022-08-21 20:21:45 +00:00
5225225 71951986d5 Add diagnostic translation lints to crates that don't emit them 2022-08-18 19:29:02 +01:00
Takayuki Maeda aaec2dad12 add a comment about what we can parse now 2022-08-04 20:43:35 +09:00
Takayuki Maeda ddeb2b8d95 return when captured argument is not a struct field 2022-08-04 11:51:25 +09:00
Takayuki Maeda a0e50d0bc4 suggest a positional formatting argument instead of a captured argument 2022-08-03 11:12:31 +09:00
Alex Macleod 79598e4498 Always include a position span in rustc_parse_format::Argument 2022-07-31 15:11:33 +00:00
Preston From 8521e468c9 Generate correct suggestion with named arguments used positionally
Address issue #99265 by checking each positionally used argument
to see if the argument is named and adding a lint to use the name
instead. This way, when named arguments are used positionally in a
different order than their argument order, the suggested lint is
correct.

For example:
```
println!("{b} {}", a=1, b=2);
```
This will now generate the suggestion:
```
println!("{b} {a}", a=1, b=2);
```

Additionally, this check now also correctly replaces or inserts
only where the positional argument is (or would be if implicit).
Also, width and precision are replaced with their argument names
when they exists.

Since the issues were so closely related, this fix for issue #99265
also fixes issue #99266.

Fixes #99265
Fixes #99266
2022-07-25 00:00:27 -06:00
miam-miam100 3575bb3d1c Add diagnostic width span when '0$' is used as width. 2022-07-20 13:39:56 +01:00
bjorn3 37dcd426f2 Make rustc_parse_format compile on stable
This allows it to be used by lightweight formatting systems and may
allow it to be used by rust-analyzer.
2022-05-03 11:26:58 +02:00
Yuri Astrakhan 4b2b8734c7 Spellchecking compiler comments
This PR cleans up the rest of the spelling mistakes in the compiler comments. This PR does not change any literal or code spelling issues.
2022-03-30 15:14:15 -04:00
Chayim Refael Friedman 1b206f31a6 Correctly mark the span of captured arguments in format_args!()
It should only include the identifier, or misspelling suggestions will be wrong.
2022-02-16 07:34:06 +00:00
Guillaume Gomez 5257da3d08 Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
Pietro Albini cfbc4671af remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
Yuki Okushi e4ad96394f Rollup merge of #83348 - osa1:issue83344, r=jackh726
format macro argument parsing fix

When the character next to `{}` is "shifted" (when mapping a byte index
in the format string to span) we should avoid shifting the span end
index, so first map the index of `}` to span, then bump the span,
instead of first mapping the next byte index to a span (which causes
bumping the end span too much).

Regression test added.

Fixes #83344

---

r? ```@estebank```
2021-03-28 01:33:13 +09:00
Ömer Sinan Ağacan 16b1878c02 format macro argument parsing fix
When the character next to `{}` is "shifted" (when mapping a byte index
in the format string to span) we should avoid shifting the span end
index, so first map the index of `}` to span, then bump the span,
instead of first mapping the next byte index to a span (which causes
bumping the end span too much).

Regression test added.

Fixes #83344
2021-03-27 13:06:36 +03:00
Yuki Okushi 8113e50bf6 Rollup merge of #83343 - osa1:issue83340, r=jackh726
Simplify and fix byte skipping in format! string parser

Fixes '\\' handling in format strings.

Fixes #83340
2021-03-27 12:37:19 +09:00
Ömer Sinan Ağacan 18abd1b4e3 Simplify and fix byte skipping in format! string parser
Fixes '\\' handling in format strings.

Fixes #83340
2021-03-21 14:42:27 +03:00
mark 8b7973863f stabilize or_patterns 2021-03-19 19:45:32 -05:00
David Hewitt 6d23448a13 parse_format: treat r" as a literal 2021-02-06 15:01:07 +00:00
Ömer Sinan Ağacan f624cdef3b rustc_parse_format: Fix character indices in find_skips
Fixes #81006
2021-01-17 17:40:58 +03:00
LingMan 382fe93c51 Use Option::map_or instead of .map(..).unwrap_or(..) 2021-01-14 19:23:59 +01:00
Erik Hofmayer de60043a75 /nightly/nightly-rustc 2020-09-23 21:51:56 +02:00
Erik Hofmayer 6785e43937 Updated html_root_url for compiler crates 2020-09-23 21:14:43 +02:00
ecstatic-morse acaaafaa83 Rollup merge of #76888 - matthiaskrgr:clippy_single_match_2, r=Dylan-DPC
use if let instead of single match arm expressions

use if let instead of single match arm expressions to compact code and reduce nesting (clippy::single_match)
2020-09-21 20:40:55 -07:00
Matthias Krüger de88da8426 use if let instead of single match arm expressions to compact code and reduce nesting (clippy::single_match) 2020-09-20 11:42:52 +02:00
est31 36c042edbf Remove redundant #![feature(...)] 's from compiler/ 2020-09-17 07:58:45 +02:00
mark c11a3894e6 mv compiler to compiler/ 2020-08-30 18:45:07 +03:00