Commit Graph

50 Commits

Author SHA1 Message Date
Nilstrieb
427aceb9d4 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
729185338f 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
5bf6a46032 Replace some thens with some then_somes 2023-02-16 15:26:03 +00:00
Maybe Waffle
8751fa1a9a if $c:expr { Some($r:expr) } else { None } =>> $c.then(|| $r) 2023-02-16 15:26:00 +00:00
Matthias Krüger
5201bb53bb remove redundant clones 2023-02-10 18:08:25 +01:00
Matthias Krüger
2fdb3559c4
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
f922c8395d 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
0abf8a0617 Replace format flags u32 by enums and bools. 2023-01-27 08:53:39 +01:00
Nilstrieb
a8086cf9df Revert "Improve heuristics whether format_args string is a source literal"
This reverts commit e6c02aad93.

Keeps the code improvements from the PR and the test (as a known-bug).
2023-01-18 20:33:17 +01:00
nils
fd7a159710 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
31b490d8ba 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
e6c02aad93 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
731e0bf721 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
c44c82de2b Assert size of rustc_parse_format::Piece<'_> 2022-12-08 22:50:39 +00:00
Maybe Waffle
78060cb6de 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
35c7939ae5
Fix incorrect span when using byte-escaped rbrace
Fix #103826.
2022-11-21 20:37:09 -08:00
Takayuki Maeda
9857de218f shift no characters when using raw string literals
remove `find_skips`

remove unnecessary variables
2022-11-15 14:28:20 +09:00
Alex Macleod
f5e390e863 Fix rustc_parse_format spans following escaped utf-8 multibyte chars 2022-10-27 18:20:56 +00:00
Colin Baumgarten
b9e85bf60a 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
e5096d4cba
Fix span of byte-escaped left format args brace
Fix #102057.
2022-09-28 13:47:17 -07:00
Mara Bos
1b044da5bb Separate CountIsStar from CountIsParam in rustc_parse_format. 2022-08-25 14:49:09 +02:00
Alex Macleod
586c84a052 Fix rustc_parse_format precision & width spans 2022-08-21 20:21:45 +00:00
5225225
09ea9f0a87 Add diagnostic translation lints to crates that don't emit them 2022-08-18 19:29:02 +01:00
Takayuki Maeda
8c85c9936f add a comment about what we can parse now 2022-08-04 20:43:35 +09:00
Takayuki Maeda
dcd70c0995 return when captured argument is not a struct field 2022-08-04 11:51:25 +09:00
Takayuki Maeda
4233a13ceb suggest a positional formatting argument instead of a captured argument 2022-08-03 11:12:31 +09:00
Alex Macleod
2a0b51d852 Always include a position span in rustc_parse_format::Argument 2022-07-31 15:11:33 +00:00
Preston From
3330c7d1c3 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
62187b12c2
Add diagnostic width span when '0$' is used as width. 2022-07-20 13:39:56 +01:00
bjorn3
d33140d2dc 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
5160f8f843 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
91adb6ccd6 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
Mark Rousskov
c746be2219 Migrate to 2021 2021-09-20 22:21:42 -04:00
Jade
3cf820e17d rfc3052: Remove authors field from Cargo manifests
Since RFC 3052 soft deprecated the authors field anyway, hiding it from
crates.io, docs.rs, and making Cargo not add it by default, and it is
not generally up to date/useful information, we should remove it from
crates in this repo.
2021-07-29 14:56:05 -07:00
Guillaume Gomez
a2654fb64c Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
Pietro Albini
9e22b844dd remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
Yuki Okushi
973fb4b77f
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
5b9bac2ab6 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
d7216bae23
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
ae8ef70a49 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
db5629adcb stabilize or_patterns 2021-03-19 19:45:32 -05:00
David Hewitt
04a19b93c6 parse_format: treat r" as a literal 2021-02-06 15:01:07 +00:00
Ömer Sinan Ağacan
9111e9dd01 rustc_parse_format: Fix character indices in find_skips
Fixes #81006
2021-01-17 17:40:58 +03:00
LingMan
a56bffb4f9 Use Option::map_or instead of .map(..).unwrap_or(..) 2021-01-14 19:23:59 +01:00
Erik Hofmayer
138a2e5eaa /nightly/nightly-rustc 2020-09-23 21:51:56 +02:00
Erik Hofmayer
dd66ea2d3d Updated html_root_url for compiler crates 2020-09-23 21:14:43 +02:00
ecstatic-morse
dcf4d1f2be
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
c690c82ad4 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
ebdea01143 Remove redundant #![feature(...)] 's from compiler/ 2020-09-17 07:58:45 +02:00
mark
9e5f7d5631 mv compiler to compiler/ 2020-08-30 18:45:07 +03:00