Commit Graph

108 Commits

Author SHA1 Message Date
Esteban Küber 19821ad234 Properly handle emojis as literal prefix in macros
Do not accept the following

```rust
macro_rules! lexes {($($_:tt)*) => {}}
lexes!(🐛"foo");
```

Before, invalid emoji identifiers were gated during parsing instead of lexing in all cases, but this didn't account for macro expansion of literal prefixes.

Fix #123696.
2024-04-10 23:19:27 +00:00
Esteban Küber ea1883d7b2 Silence redundant error on char literal that was meant to be a string in 2021 edition 2024-03-17 23:35:19 +00:00
Esteban Küber 982918f493 Handle str literals written with `'` lexed as lifetime
Given `'hello world'` and `'1 str', provide a structured suggestion for a valid string literal:

```
error[E0762]: unterminated character literal
  --> $DIR/lex-bad-str-literal-as-char-3.rs:2:26
   |
LL |     println!('hello world');
   |                          ^^^^
   |
help: if you meant to write a `str` literal, use double quotes
   |
LL |     println!("hello world");
   |              ~           ~
```
```
error[E0762]: unterminated character literal
  --> $DIR/lex-bad-str-literal-as-char-1.rs:2:20
   |
LL |     println!('1 + 1');
   |                    ^^^^
   |
help: if you meant to write a `str` literal, use double quotes
   |
LL |     println!("1 + 1");
   |              ~     ~
```

Fix #119685.
2024-03-17 23:35:18 +00:00
Nicholas Nethercote 0ac1195ee0 Invert diagnostic lints.
That is, change `diagnostic_outside_of_impl` and
`untranslatable_diagnostic` from `allow` to `deny`, because more than
half of the compiler has be converted to use translated diagnostics.

This commit removes more `deny` attributes than it adds `allow`
attributes, which proves that this change is warranted.
2024-02-06 13:12:33 +11:00
Nicholas Nethercote 6be2e5623c Use `unescape_unicode` for raw C string literals.
They can't contain `\x` escapes, which means they can't contain high
bytes, which means we can used `unescape_unicode` instead of
`unescape_mixed` to unescape them. This avoids unnecessary used of
`MixedUnit`.
2024-01-25 12:28:11 +11:00
Nicholas Nethercote 86f371ed59 Rename the unescaping functions.
`unescape_literal` becomes `unescape_unicode`, and `unescape_c_string`
becomes `unescape_mixed`. Because rfc3349 will mean that C string
literals will no longer be the only mixed utf8 literals.
2024-01-25 12:28:11 +11:00
Nicholas Nethercote 5e5aa6d556 Rename and invert sense of `Mode` predicates.
I find it easier if they describe what's allowed, rather than what's
forbidden. Also, consistent naming makes them easier to understand.
2024-01-25 12:28:11 +11:00
Nicholas Nethercote a1c07214f0 Rework `CStrUnit`.
- Rename it as `MixedUnit`, because it will soon be used in more than
  just C string literals.
- Change the `Byte` variant to `HighByte` and use it only for
  `\x80`..`\xff` cases. This fixes the old inexactness where ASCII chars
  could be encoded with either `Byte` or `Char`.
- Add useful comments.
- Remove `is_ascii`, in favour of `u8::is_ascii`.
2024-01-25 12:28:11 +11:00
Nicholas Nethercote ef1e2228cf Use `from` instead of `into` in unescaping code.
The `T` type in these functions took me some time to understand, and I
find the explicit `T` in the use of `from` makes the code easier to
read, as does the `u8` annotation in `scan_escape`.
2024-01-25 12:26:36 +11:00
Matthias Krüger a54c295665
Rollup merge of #118639 - fmease:deny-features-in-stable-rustc-crates, r=WaffleLapkin
Undeprecate lint `unstable_features` and make use of it in the compiler

See also #117937.

r? compiler
2024-01-22 16:54:56 +01:00
Nicholas Nethercote 9018d2c455 Detect `NulInCStr` error earlier.
By making it an `EscapeError` instead of a `LitError`. This makes it
like the other errors produced when checking string literals contents,
e.g. for invalid escape sequences or bare CR chars.

NOTE: this means these errors are issued earlier, before expansion,
which changes behaviour. It will be possible to move the check back to
the later point if desired. If that happens, it's likely that all the
string literal contents checks will be delayed together.

One nice thing about this: the old approach had some code in
`report_lit_error` to calculate the span of the nul char from a range.
This code used a hardwired `+2` to account for the `c"` at the start of
a C string literal, but this should have changed to a `+3` for raw C
string literals to account for the `cr"`, which meant that the caret in
`cr"` nul error messages was one short of where it should have been. The
new approach doesn't need any of this and avoids the off-by-one error.
2024-01-12 16:19:37 +11:00
León Orell Valerian Liehr c0a9f722c4
Undeprecate and use lint `unstable_features` 2023-12-20 18:16:28 +01:00
Nicholas Nethercote b900eb7317 Rename some unescaping functions.
`unescape_raw_str_or_raw_byte_str` only does checking, no unescaping.
And it also now handles C string literals.

`unescape_raw_str` is used for all the non-raw strings.
2023-12-13 14:17:50 +11:00
Nicholas Nethercote 29c5158ef5 Adjust `Mode::is_unicode_escape_disallowed`.
Some cases are unreachable.
2023-12-13 10:06:13 +11:00
Nicholas Nethercote 0a401b624b Tweak `Mode`.
- Add `use Mode::*` to avoid all the qualifiers.
- Reorder the variants. The existing order makes no particular sense,
  which has bugged me for some time. I've chosen an order that makes
  sense to me.
2023-12-09 09:30:32 +11:00
Nicholas Nethercote adc46e5c08 Remove an unnecessary `into`. 2023-12-09 09:30:32 +11:00
Nicholas Nethercote 119b1d0c63 Eliminate `is_byte: bool` args in unescaping code.
These don't really make sense since C string literals were added. This
commit removes them in favour for `mode: Mode` args. `ascii_check` still
has a `characters_should_be_ascii: bool` arg.

Also, `characters_should_be_ascii` is renamed to be shorter.
2023-12-09 09:30:32 +11:00
Nicholas Nethercote f883762970 Remove explicit `\n` and `\t` handling in `unescape_str_common`.
The fallback `_` case works for these chars, no need to treat them
specially.
2023-12-09 09:30:32 +11:00
Nicholas Nethercote 08b8ba0a32 Add some useful comments. 2023-12-09 09:30:30 +11:00
Nicholas Nethercote c6bbb376a2 Fix an out-of-date comment. 2023-12-07 07:33:12 +11:00
Nicholas Nethercote e290582abf Identify impossible cases in `ascii_escapes_should_be_ascii`.
Raw strings (of all kinds) don't support escapes, so this function
should never be called on them.
2023-12-07 07:33:12 +11:00
Nicholas Nethercote 856b55fb34 De-pub some functions. 2023-12-07 07:33:12 +11:00
Nicholas Nethercote 8ff624a9f2 Clean up `rustc_*/Cargo.toml`.
- Sort dependencies and features sections.
- Add `tidy` markers to the sorted sections so they stay sorted.
- Remove empty `[lib`] sections.
- Remove "See more keys..." comments.

Excluded files:
- rustc_codegen_{cranelift,gcc}, because they're external.
- rustc_lexer, because it has external use.
- stable_mir, because it has external use.
2023-10-30 08:46:02 +11:00
Michael Goulet 60c95448c3 Use v0.0.0 in compiler crates 2023-10-18 21:55:15 +00:00
Charles Lew bca79a26d8 Update lexer emoji diagnostics to Unicode 15.0 2023-07-29 08:47:21 +08:00
bors 23405bb123 Auto merge of #113476 - fee1-dead-contrib:c-str-lit, r=petrochenkov
Reimplement C-str literals

This reverts #113334, cc `@fmease.`

While converting lexer tokens to ast Tokens in `rustc_parse`, we check the edition of the span of the token. If the edition < 2021, we split the token into two, one being the identifier and other being the str literal.
2023-07-25 12:04:34 +00:00
Matthias Krüger ed4c5fef72 fix some clippy::style findings
comparison_to_empty
iter_nth_zero
for_kv_map
manual_next_back
redundant_pattern
2023-07-23 23:36:56 +02:00
Deadbeef df9bd80d74 reimplement C string literals 2023-07-23 06:54:07 +00:00
León Orell Valerian Liehr c6643b50ea
Revert the lexing of c_str_literals 2023-07-05 13:11:17 +02:00
Nicholas Nethercote e52794decd Don't try to eat non-existent decimal digits.
After seeing a `0`, if it's followed by any of `[0-9]`, `_`, `.`, `e`,
or `E`, we consume all the digits. But in the `.`, `e` and `E` cases
this is pointless because we know there aren't any digits.
2023-05-15 18:33:12 +10:00
Nicholas Nethercote 19967c5890 Make `Cursor::number` less DRY.
A tiny bit of repetition makes this easier to read, and avoids a test on
the "Not a base prefix" match arm.
2023-05-15 18:30:26 +10:00
Deadbeef 78e3455d37 address comments 2023-05-02 10:32:07 +00:00
Deadbeef 4c01d494b8 refactor unescape 2023-05-02 10:32:07 +00:00
Deadbeef a49570fd20 fix TODO comments 2023-05-02 10:32:07 +00:00
Deadbeef 8ff3903643 initial step towards implementing C string literals 2023-05-02 10:30:09 +00:00
Michael Goulet a047064d6b Revert "Don't recover lifetimes/labels containing emojis as character literals"
Reverts PR #108031
Fixes (doesnt close until beta backported) #109746

This reverts commit e3f9db5fc3.
This reverts commit 98b82aedba.
This reverts commit 380fa26413.
2023-04-10 06:52:41 +00:00
bohan d223c26bce fix(lexer): not skipped whitespace warning for '\x0c' 2023-03-09 22:44:58 +08:00
est31 5a02105fff Rustdoc-ify LiteralKind note 2023-03-03 08:39:36 +01:00
许杰友 Jieyou Xu (Joe) e3f9db5fc3
Update lexer lifetime test 2023-02-14 23:25:01 +08:00
许杰友 Jieyou Xu (Joe) 380fa26413
Don't recover lifetimes/labels containing emojis as character literals
Note that at the time of this commit, `unic-emoji-char` seems to have
data tables only up to Unicode 5.0, but Unicode is already newer than
this.

A newer emoji such as `🥺` will not be recognized as an emoji
but older emojis such as `🐱` will.
2023-02-14 17:31:58 +08:00
Maybe Waffle 6a28fb42a8 Remove double spaces after dots in comments 2023-01-17 08:09:33 +00:00
Michael Goulet aff403cf68 Recover `fn` keyword as `Fn` trait in bounds 2022-12-27 06:14:46 +00:00
Matthias Krüger 2ea368e53c minor code cleanups 2022-12-12 19:49:53 +01:00
KaDiWa 9bc69925cb
compiler: remove unnecessary imports and qualified paths 2022-12-10 18:45:34 +01:00
klensy ceee880b31 update cpufeatures, swap difference to dissimilar 2022-11-29 19:59:37 +03:00
bors 872631d0f0 Auto merge of #104507 - WaffleLapkin:asderefsyou, r=wesleywiser
Use `as_deref` in compiler (but only where it makes sense)

This simplifies some code :3

(there are some changes that are not exacly `as_deref`, but more like "clever `Option`/`Result` method use")
2022-11-24 00:17:35 +00:00
Maybe Waffle 94470f4efd Use `as_deref` in compiler (but only where it makes sense) 2022-11-16 21:58:58 +00:00
Nicholas Nethercote 358a603f11 Use `token::Lit` in `ast::ExprKind::Lit`.
Instead of `ast::Lit`.

Literal lowering now happens at two different times. Expression literals
are lowered when HIR is crated. Attribute literals are lowered during
parsing.

This commit changes the language very slightly. Some programs that used
to not compile now will compile. This is because some invalid literals
that are removed by `cfg` or attribute macros will no longer trigger
errors. See this comment for more details:
https://github.com/rust-lang/rust/pull/102944#issuecomment-1277476773
2022-11-16 09:41:28 +11:00
Dylan DPC 4b50fb3745
Rollup merge of #103919 - nnethercote:unescaping-cleanups, r=matklad
Unescaping cleanups

Some code improvements, and some error message improvements.

Best reviewed one commit at a time.

r? ````@matklad````
2022-11-09 19:21:22 +05:30
Nicholas Nethercote d6c97a32b4 Simplify `unescape_{char,byte}`.
The `usize` isn't needed in the error case.
2022-11-08 15:59:19 +11:00