about summary refs log tree commit diff
path: root/compiler/rustc_parse
AgeCommit message (Collapse)AuthorLines
2024-08-16Overhaul token collection.Nicholas Nethercote-275/+363
This commit does the following. - Renames `collect_tokens_trailing_token` as `collect_tokens`, because (a) it's annoying long, and (b) the `_trailing_token` bit is less accurate now that its types have changed. - In `collect_tokens`, adds a `Option<CollectPos>` argument and a `UsePreAttrPos` in the return type of `f`. These are used in `parse_expr_force_collect` (for vanilla expressions) and in `parse_stmt_without_recovery` (for two different cases of expression statements). Together these ensure are enough to fix all the problems with token collection and assoc expressions. The changes to the `stringify.rs` test demonstrate some of these. - Adds a new test. The code in this test was causing an assertion failure prior to this commit, due to an invalid `NodeRange`. The extra complexity is annoying, but necessary to fix the existing problems.
2024-08-16Add an assertion to `NodeRange::new`.Nicholas Nethercote-0/+1
2024-08-16Convert a bool to `Trailing`.Nicholas Nethercote-40/+52
This pre-existing type is suitable for use with the return value of the `f` parameter in `collect_tokens_trailing_token`. The more descriptive name will be useful because the next commit will add another boolean value to the return value of `f`.
2024-08-16Make visibilities minimal and consistent in `attr_wrapper.rs`.Nicholas Nethercote-6/+6
2024-08-16Remove size assertion on `AttrWrapper`.Nicholas Nethercote-1/+0
It's not an important type when it comes to memory use.
2024-08-14Use `impl PartialEq<TokenKind> for Token` more.Nicholas Nethercote-117/+112
This lets us compare a `Token` with a `TokenKind`. It's used a lot, but can be used even more, avoiding the need for some `.kind` uses.
2024-08-14Convert a `&mut self` to `&self`.Nicholas Nethercote-1/+1
2024-08-12Rollup merge of #128994 - nnethercote:fix-Parser-look_ahead-more, ↵Guillaume Gomez-4/+6
r=compiler-errors Fix bug in `Parser::look_ahead`. The special case was failing to handle invisible delimiters on one path. Fixes (but doesn't close until beta backported) #128895. r? `@davidtwco`
2024-08-12Rollup merge of #128978 - compiler-errors:assert-matches, r=jieyouxuGuillaume Gomez-2/+5
Use `assert_matches` around the compiler more It's a useful assertion, especially since it actually prints out the LHS.
2024-08-12Fix bug in `Parser::look_ahead`.Nicholas Nethercote-4/+6
The special case was failing to handle invisible delimiters on one path. Fixes #128895.
2024-08-11Use assert_matches around the compilerMichael Goulet-2/+5
2024-08-11Rollup merge of #128762 - fmease:use-more-slice-pats, r=compiler-errorsMatthias Krüger-10/+10
Use more slice patterns inside the compiler Nothing super noteworthy. Just replacing the common 'fragile' pattern of "length check followed by indexing or unwrap" with slice patterns for legibility and 'robustness'. r? ghost
2024-08-09parser: ensure let stmt compound assignment removal suggestion respect ↵许杰友 Jieyou Xu (Joe)-2/+6
codepoint boundaries Previously we would try to issue a suggestion for `let x <op>= 1`, i.e. a compound assignment within a `let` binding, to remove the `<op>`. The suggestion code unfortunately incorrectly assumed that the `<op>` is an exactly-1-byte ASCII character, but this assumption is incorrect because we also recover Unicode-confusables like `➖=` as `-=`. In this example, the suggestion code used a `+ BytePos(1)` to calculate the span of the `<op>` codepoint that looks like `-` but the mult-byte Unicode look-alike would cause the suggested removal span to be inside a multi-byte codepoint boundary, triggering a codepoint boundary assertion. Issue: <https://github.com/rust-lang/rust/issues/128845>
2024-08-07Use more slice patterns inside the compilerLeón Orell Valerian Liehr-10/+10
2024-08-07Stabilize `unsafe_attributes`carbotaniuman-30/+14
2024-08-03Rollup merge of #127921 - spastorino:stabilize-unsafe-extern-blocks, ↵Matthias Krüger-3/+0
r=compiler-errors Stabilize unsafe extern blocks (RFC 3484) # Stabilization report ## Summary This is a tracking issue for the RFC 3484: Unsafe Extern Blocks We are stabilizing `#![feature(unsafe_extern_blocks)]`, as described in [Unsafe Extern Blocks RFC 3484](https://github.com/rust-lang/rfcs/pull/3484). This feature makes explicit that declaring an extern block is unsafe. Starting in Rust 2024, all extern blocks must be marked as unsafe. In all editions, items within unsafe extern blocks may be marked as safe to use. RFC: https://github.com/rust-lang/rfcs/pull/3484 Tracking issue: #123743 ## What is stabilized ### Summary of stabilization We now need extern blocks to be marked as unsafe and items inside can also have safety modifiers (unsafe or safe), by default items with no modifiers are unsafe to offer easy migration without surprising results. ```rust unsafe extern { // sqrt (from libm) may be called with any `f64` pub safe fn sqrt(x: f64) -> f64; // strlen (from libc) requires a valid pointer, // so we mark it as being an unsafe fn pub unsafe fn strlen(p: *const c_char) -> usize; // this function doesn't say safe or unsafe, so it defaults to unsafe pub fn free(p: *mut core::ffi::c_void); pub safe static IMPORTANT_BYTES: [u8; 256]; pub safe static LINES: SyncUnsafeCell<i32>; } ``` ## Tests The relevant tests are in `tests/ui/rust-2024/unsafe-extern-blocks`. ## History - https://github.com/rust-lang/rust/pull/124482 - https://github.com/rust-lang/rust/pull/124455 - https://github.com/rust-lang/rust/pull/125077 - https://github.com/rust-lang/rust/pull/125522 - https://github.com/rust-lang/rust/issues/126738 - https://github.com/rust-lang/rust/issues/126749 - https://github.com/rust-lang/rust/issues/126755 - https://github.com/rust-lang/rust/pull/126757 - https://github.com/rust-lang/rust/pull/126758 - https://github.com/rust-lang/rust/issues/126756 - https://github.com/rust-lang/rust/pull/126973 - https://github.com/rust-lang/rust/pull/127535 - https://github.com/rust-lang/rustfmt/pull/6204 ## Unresolved questions I am not aware of any unresolved questions.
2024-08-04don't suggest turning crate-level attributes into outer styleyukang-12/+31
2024-08-03Rollup merge of #128483 - nnethercote:still-more-cfg-cleanups, r=petrochenkovMatthias Krüger-152/+168
Still more `cfg` cleanups Found while looking closely at `cfg`/`cfg_attr` processing code. r? `````````@petrochenkov`````````
2024-08-01Rollup merge of #128496 - clubby789:box-syntax-multipart, r=compiler-errorsMatthias Krüger-12/+23
Fix removed `box_syntax` diagnostic if source isn't available Fix #128442
2024-08-01Fix removed `box_syntax` diagnostic if source isn't availableclubby789-12/+23
2024-08-01Auto merge of #127543 - carbotaniuman:more_unsafe_attr_verification, ↵bors-60/+111
r=estebank,traviscross More unsafe attr verification This code denies unsafe on attributes such as `#[test]` and `#[ignore]`, while also changing the `MetaItem` parsing so `unsafe` in args like `#[allow(unsafe(dead_code))]` is not accidentally allowed. Tracking: - https://github.com/rust-lang/rust/issues/123757
2024-08-01Distinguish the two kinds of token range.Nicholas Nethercote-77/+123
When collecting tokens there are two kinds of range: - a range relative to the parser's full token stream (which we get when we are parsing); - a range relative to a single AST node's token stream (which we use within `LazyAttrTokenStreamImpl` when replacing tokens). These are currently both represented with `Range<u32>` and it's easy to mix them up -- until now I hadn't properly understood the difference. This commit introduces `ParserRange` and `NodeRange` to distinguish them. This also requires splitting `ReplaceRange` in two, giving the new types `ParserReplacement` and `NodeReplacement`. (These latter two names reduce the overloading of the word "range".) The commit also rewrites some comments to be clearer. The end result is a little more verbose, but much clearer.
2024-08-01Move a comment to a better spot.Nicholas Nethercote-2/+1
2024-08-01Streamline attribute stitching on AST nodes.Nicholas Nethercote-15/+10
It can be done more concisely.
2024-07-31Rollup merge of #126697 - vincenzopalazzo:macros/find_the_expression_tok, ↵Matthias Krüger-1/+10
r=eholk,compiler-errors [RFC] mbe: consider the `_` in 2024 an expression This commit is adding the possibility to parse the `_` as an expression inside the esition 2024. Link: https://rust-lang.zulipchat.com/#narrow/stream/404510-wg-macros/topic/supporting.20.60_.60.20expressions Issue https://github.com/rust-lang/rust/issues/123742 r? `@eholk`
2024-07-31tweak comment on `NonterminalKind::Expr`Michael Goulet-1/+1
Co-authored-by: Eric Holk <eric@theincredibleholk.org>
2024-07-31rustc_parser: consider the in 2024 an expressionVincenzo Palazzo-1/+10
This commit is adding the possibility to parse the `_` as an expression inside the esition 2024. Link: https://rust-lang.zulipchat.com/#narrow/stream/404510-wg-macros/topic/supporting.20.60_.60.20expressions Co-authored-by: Eric Holk <eric@theincredibleholk.org> Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2024-07-31Remove `LhsExpr`.Nicholas Nethercote-42/+28
`parse_expr_assoc_with` has an awkward structure -- sometimes the lhs is already parsed. This commit splits the post-lhs part into a new method `parse_expr_assoc_rest_with`, which makes everything shorter and simpler.
2024-07-31Inline and remove `parse_local_mk`.Nicholas Nethercote-16/+6
It has a single use. This makes the `let` handling case in `parse_stmt_without_recovery` more similar to the statement path and statement expression cases.
2024-07-30Add toggle for `parse_meta_item` unsafe parsingcarbotaniuman-45/+60
This makes it possible for the `unsafe(...)` syntax to only be valid at the top level, and the `NestedMetaItem`s will automatically reject `unsafe(...)`.
2024-07-30Rollup merge of #128376 - compiler-errors:finish-ur-vegetables, r=jieyouxuMatthias Krüger-11/+26
Mark `Parser::eat`/`check` methods as `#[must_use]` These methods return a `bool`, but we probably should either use these values or explicitly throw them away (e.g. when we just want to unconditionally eat a token if it exists). I changed a few places from `eat` to `expect`, but otherwise I tried to leave a comment explaining why the `eat` was okay. This also adds a test for the `pattern_type!` macro, which used to silently accept a missing `is` token.
2024-07-30Auto merge of #128083 - Mark-Simulacrum:bump-bootstrap, r=albertlarsan68bors-6/+1
Bump bootstrap compiler to new beta https://forge.rust-lang.org/release/process.html#master-bootstrap-update-t-2-day-tuesday
2024-07-30Auto merge of #127955 - ↵bors-3/+18
chenyukang:yukang-fix-mismatched-delimiter-issue-12786, r=nnethercote Add limit for unclosed delimiters in lexer diagnostic Fixes #127868 The first commit shows the original diagnostic, and the second commit shows the changes.
2024-07-29Deny unsafe on more builtin attributescarbotaniuman-56/+92
2024-07-29Mark Parser::eat/check methods as must_useMichael Goulet-11/+26
2024-07-29Reformat `use` declarations.Nicholas Nethercote-176/+185
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-28step cfg(bootstrap)Mark Rousskov-6/+1
2024-07-27Rollup merge of #128207 - folkertdev:asm-parser-generalize, r=AmanieuTrevor Gross-2/+5
improve error message when `global_asm!` uses `asm!` options specifically, what was error: expected one of `)`, `att_syntax`, or `raw`, found `preserves_flags` --> $DIR/bad-options.rs:45:25 | LL | global_asm!("", options(preserves_flags)); | ^^^^^^^^^^^^^^^ expected one of `)`, `att_syntax`, or `raw` is now error: the `preserves_flags` option cannot be used with `global_asm!` --> $DIR/bad-options.rs:45:25 | LL | global_asm!("", options(preserves_flags)); | ^^^^^^^^^^^^^^^ the `preserves_flags` option is not meaningful for global-scoped inline assembly mirroring the phrasing of the [reference](https://doc.rust-lang.org/reference/inline-assembly.html#options). This is also a bit of a refactor for a future `naked_asm!` macro (for use in `#[naked]` functions). Currently this sort of error can come up when switching from inline to global asm, or when a user just isn't that experienced with assembly. With `naked_asm!` added to the mix hitting this error is more likely.
2024-07-26Rollup merge of #128229 - tdittr:unsafe-extern-abi-error, r=compiler-errorsTrevor Gross-3/+6
Improve `extern "<abi>" unsafe fn()` error message These errors were already reported in #87217, and fixed by #87235 but missed the case of an explicit ABI. This PR does not cover multiple keywords like `extern "C" pub const unsafe fn()`, but I don't know what a good way to cover this would be. It also seems rarer than `extern "C" unsafe` which I saw happen a few times in workshops.
2024-07-26Rollup merge of #128224 - nnethercote:fewer-replace_ranges, r=petrochenkovTrevor Gross-25/+21
Remove unnecessary range replacements This PR removes an unnecessary range replacement in `collect_tokens_trailing_token`, and does a couple of other small cleanups. r? ````@petrochenkov````
2024-07-26Rollup merge of #128223 - nnethercote:refactor-collect_tokens, r=petrochenkovTrevor Gross-45/+43
Refactor complex conditions in `collect_tokens_trailing_token` More readability improvements for this complicated function. r? ````@petrochenkov````
2024-07-26Improve error message for `extern "C" unsafe fn()`Tamme Dittrich-3/+6
This was handled correctly already for `extern unsafe fn()`. Co-authored-by: Folkert <folkert@folkertdev.nl>
2024-07-26Remove an unnecessary block.Nicholas Nethercote-11/+9
2024-07-26Tweak a loop.Nicholas Nethercote-5/+7
A fully imperative style is easier to read than a half-iterator, half-imperative style. Also, rename `inner_attr` as `attr` because it might be an outer attribute.
2024-07-26Fix a comment.Nicholas Nethercote-3/+3
Imagine you have replace ranges (2..20,X) and (5..15,Y), and these tokens: ``` a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x ``` If we replace (5..15,Y) first, then (2..20,X) we get this sequence ``` a,b,c,d,e,Y,_,_,_,_,_,_,_,_,_,p,q,r,s,t,u,v,w,x a,b,X,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,u,v,w,x ``` which is what we want. If we do it in the other order, we get this: ``` a,b,X,_,_,_,_,_,_,_,_,_,_,_,_,p,q,r,s,t,u,v,w,x a,b,X,_,_,Y,_,_,_,_,_,_,_,_,_,_,_,_,_,_,u,v,w,x ``` which is wrong. So it's true that we need the `.rev()` but the comment is wrong about why.
2024-07-26Don't include inner attribute ranges in `CaptureState`.Nicholas Nethercote-6/+2
The current code is this: ``` self.capture_state.replace_ranges.push((start_pos..end_pos, Some(target))); self.capture_state.replace_ranges.extend(inner_attr_replace_ranges); ``` What's not obvious is that every range in `inner_attr_replace_ranges` must be a strict sub-range of `start_pos..end_pos`. Which means, in `LazyAttrTokenStreamImpl::to_attr_token_stream`, they will be done first, and then the `start_pos..end_pos` replacement will just overwrite them. So they aren't needed.
2024-07-26Invert the sense of `is_complete` and rename it as `needs_tokens`.Nicholas Nethercote-11/+13
I have always found `is_complete` an unhelpful name. The new name (and inverted sense) fits in better with the conditions at its call sites.
2024-07-26Move `is_complete` to the module that uses it.Nicholas Nethercote-13/+13
And make it non-`pub`.
2024-07-26Inline and remove `AttrWrapper::is_complete`.Nicholas Nethercote-8/+4
It has a single call site. This change makes the two `needs_collect` conditions more similar to each other, and therefore easier to understand.
2024-07-26Invert early exit conditions in `collect_tokens_trailing_token`.Nicholas Nethercote-29/+29
This has been bugging me for a while. I find complex "if any of these are true" conditions easier to think about than complex "if all of these are true" conditions, because you can stop as soon as one is true.