about summary refs log tree commit diff
path: root/compiler/rustc_expand/src/module.rs
AgeCommit message (Collapse)AuthorLines
2025-09-09Strip frontmatter in fewer placesLeón Orell Valerian Liehr-2/+7
2025-08-22Move validate_attr to `rustc_attr_parsing`Jonathan Brouwer-1/+2
2025-08-19Prevent impossible combinations in `ast::ModKind`.Nicholas Nethercote-1/+1
`ModKind::Loaded` has an `inline` field and a `had_parse_error` field. If the `inline` field is `Inline::Yes` then `had_parse_error` must be `Ok(())`. This commit moves the `had_parse_error` field into the `Inline::No` variant. This makes it impossible to create the nonsensical combination of `inline == Inline::Yes` and `had_parse_error = Err(_)`.
2025-08-09remove `P`Deadbeef-2/+1
2025-02-15Reject macro calls inside of `#![crate_name]`León Orell Valerian Liehr-6/+6
2024-12-19Speed up `Parser::expected_token_types`.Nicholas Nethercote-3/+3
The parser pushes a `TokenType` to `Parser::expected_token_types` on every call to the various `check`/`eat` methods, and clears it on every call to `bump`. Some of those `TokenType` values are full tokens that require cloning and dropping. This is a *lot* of work for something that is only used in error messages and it accounts for a significant fraction of parsing execution time. This commit overhauls `TokenType` so that `Parser::expected_token_types` can be implemented as a bitset. This requires changing `TokenType` to a C-style parameterless enum, and adding `TokenTypeSet` which uses a `u128` for the bits. (The new `TokenType` has 105 variants.) The new types `ExpTokenPair` and `ExpKeywordPair` are now arguments to the `check`/`eat` methods. This is for maximum speed. The elements in the pairs are always statically known; e.g. a `token::BinOp(token::Star)` is always paired with a `TokenType::Star`. So we now compute `TokenType`s in advance and pass them in to `check`/`eat` rather than the current approach of constructing them on insertion into `expected_token_types`. Values of these pair types can be produced by the new `exp!` macro, which is used at every `check`/`eat` call site. The macro is for convenience, allowing any pair to be generated from a single identifier. The ident/keyword filtering in `expected_one_of_not_found` is no longer necessary. It was there to account for some sloppiness in `TokenKind`/`TokenType` comparisons. The existing `TokenType` is moved to a new file `token_type.rs`, and all its new infrastructure is added to that file. There is more boilerplate code than I would like, but I can't see how to make it shorter.
2024-12-18Re-export more `rustc_span::symbol` things from `rustc_span`.Nicholas Nethercote-2/+1
`rustc_span::symbol` defines some things that are re-exported from `rustc_span`, such as `Symbol` and `sym`. But it doesn't re-export some closely related things such as `Ident` and `kw`. So you can do `use rustc_span::{Symbol, sym}` but you have to do `use rustc_span::symbol::{Ident, kw}`, which is inconsistent for no good reason. This commit re-exports `Ident`, `kw`, and `MacroRulesNormalizedIdent`, and changes many `rustc_span::symbol::` qualifiers in `compiler/` to `rustc_span::`. This is a 200+ net line of code reduction, mostly because many files with two `use rustc_span` items can be reduced to one.
2024-12-10Keep track of parse errors in `mod`s and don't emit resolve errors for paths ↵Esteban Küber-3/+7
involving them When we expand a `mod foo;` and parse `foo.rs`, we now track whether that file had an unrecovered parse error that reached the end of the file. If so, we keep that information around. When resolving a path like `foo::bar`, we do not emit any errors for "`bar` not found in `foo`", as we know that the parse error might have caused `bar` to not be parsed and accounted for. When this happens in an existing project, every path referencing `foo` would be an irrelevant compile error. Instead, we now skip emitting anything until `foo.rs` is fixed. Tellingly enough, we didn't have any test for errors caused by `mod` expansion. Fix #97734.
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-3/+3
2024-09-11rustc_expand: remember module #[path]s during expansiondianne-1/+1
this fixes cycle detection for modules that need a second invocation collection pass after parsing
2024-07-29Reformat `use` declarations.Nicholas Nethercote-8/+9
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-06-05Make top-level `rustc_parse` functions fallible.Nicholas Nethercote-2/+3
Currently we have an awkward mix of fallible and infallible functions: ``` new_parser_from_source_str maybe_new_parser_from_source_str new_parser_from_file (maybe_new_parser_from_file) // missing (new_parser_from_source_file) // missing maybe_new_parser_from_source_file source_str_to_stream maybe_source_file_to_stream ``` We could add the two missing functions, but instead this commit removes of all the infallible ones and renames the fallible ones leaving us with these which are all fallible: ``` new_parser_from_source_str new_parser_from_file new_parser_from_source_file source_str_to_stream source_file_to_stream ``` This requires making `unwrap_or_emit_fatal` public so callers of formerly infallible functions can still work. This does make some of the call sites slightly more verbose, but I think it's worth it for the simpler API. Also, there are two `catch_unwind` calls and one `catch_fatal_errors` call in this diff that become removable thanks this change. (I will do that in a follow-up PR.)
2024-03-15Use `do yeet ()` and `do yeet _` instead of `None?` and `Err(_)?` in compilerMaybe Waffle-1/+1
This prevents breakage when `?` no longer skews inference.
2024-03-05Rename all `ParseSess` variables/fields/lifetimes as `psess`.Nicholas Nethercote-10/+6
Existing names for values of this type are `sess`, `parse_sess`, `parse_session`, and `ps`. `sess` is particularly annoying because that's also used for `Session` values, which are often co-located, and it can be difficult to know which type a value named `sess` refers to. (That annoyance is the main motivation for this change.) `psess` is nice and short, which is good for a name used this much. The commit also renames some `parse_sess_created` values as `psess_created`.
2024-02-28Rename `DiagnosticBuilder` as `Diag`.Nicholas Nethercote-2/+2
Much better! Note that this involves renaming (and updating the value of) `DIAGNOSTIC_BUILDER` in clippy.
2024-01-08Make `DiagnosticBuilder::emit` consuming.Nicholas Nethercote-1/+1
This works for most of its call sites. This is nice, because `emit` very much makes sense as a consuming operation -- indeed, `DiagnosticBuilderState` exists to ensure no diagnostic is emitted twice, but it uses runtime checks. For the small number of call sites where a consuming emit doesn't work, the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will be removed in subsequent commits.) Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes consuming, while `delay_as_bug_without_consuming` is added (which will also be removed in subsequent commits.) All this requires significant changes to `DiagnosticBuilder`'s chaining methods. Currently `DiagnosticBuilder` method chaining uses a non-consuming `&mut self -> &mut Self` style, which allows chaining to be used when the chain ends in `emit()`, like so: ``` struct_err(msg).span(span).emit(); ``` But it doesn't work when producing a `DiagnosticBuilder` value, requiring this: ``` let mut err = self.struct_err(msg); err.span(span); err ``` This style of chaining won't work with consuming `emit` though. For that, we need to use to a `self -> Self` style. That also would allow `DiagnosticBuilder` production to be chained, e.g.: ``` self.struct_err(msg).span(span) ``` However, removing the `&mut self -> &mut Self` style would require that individual modifications of a `DiagnosticBuilder` go from this: ``` err.span(span); ``` to this: ``` err = err.span(span); ``` There are *many* such places. I have a high tolerance for tedious refactorings, but even I gave up after a long time trying to convert them all. Instead, this commit has it both ways: the existing `&mut self -> Self` chaining methods are kept, and new `self -> Self` chaining methods are added, all of which have a `_mv` suffix (short for "move"). Changes to the existing `forward!` macro lets this happen with very little additional boilerplate code. I chose to add the suffix to the new chaining methods rather than the existing ones, because the number of changes required is much smaller that way. This doubled chainging is a bit clumsy, but I think it is worthwhile because it allows a *lot* of good things to subsequently happen. In this commit, there are many `mut` qualifiers removed in places where diagnostics are emitted without being modified. In subsequent commits: - chaining can be used more, making the code more concise; - more use of chaining also permits the removal of redundant diagnostic APIs like `struct_err_with_code`, which can be replaced easily with `struct_err` + `code_mv`; - `emit_without_diagnostic` can be removed, which simplifies a lot of machinery, removing the need for `DiagnosticBuilderState`.
2023-12-24Remove `Session` methods that duplicate `DiagCtxt` methods.Nicholas Nethercote-4/+4
Also add some `dcx` methods to types that wrap `TyCtxt`, for easier access.
2023-12-23Give `DiagnosticBuilder` a default type.Nicholas Nethercote-1/+1
`IntoDiagnostic` defaults to `ErrorGuaranteed`, because errors are the most common diagnostic level. It makes sense to do likewise for the closely-related (and much more widely used) `DiagnosticBuilder` type, letting us write `DiagnosticBuilder<'a, ErrorGuaranteed>` as just `DiagnosticBuilder<'a>`. This cuts over 200 lines of code due to many multi-line things becoming single line things.
2023-11-21Fix `clippy::needless_borrow` in the compilerNilstrieb-2/+2
`x clippy compiler -Aclippy::all -Wclippy::needless_borrow --fix`. Then I had to remove a few unnecessary parens and muts that were exposed now.
2023-10-13Format all the let chains in compilerMichael Goulet-1/+3
2023-02-21Use `ThinVec` in a few more AST types.Nicholas Nethercote-2/+2
2022-12-10Migrate parts of `rustc_expand` to session diagnosticsnils-46/+34
This migrates everything but the `mbe` and `proc_macro` modules. It also contains a few cleanups and drive-by/accidental diagnostic improvements which can be seen in the diff for the UI tests.
2022-08-22Use `AttrVec` in more places.Nicholas Nethercote-4/+4
In some places we use `Vec<Attribute>` and some places we use `ThinVec<Attribute>` (a.k.a. `AttrVec`). This results in various points where we have to convert between `Vec` and `ThinVec`. This commit changes the places that use `Vec<Attribute>` to use `AttrVec`. A lot of this is mechanical and boring, but there are some interesting parts: - It adds a few new methods to `ThinVec`. - It implements `MapInPlace` for `ThinVec`, and introduces a macro to avoid the repetition of this trait for `Vec`, `SmallVec`, and `ThinVec`. Overall, it makes the code a little nicer, and has little effect on performance. But it is a precursor to removing `rustc_data_structures::thin_vec::ThinVec` and replacing it with `thin_vec::ThinVec`, which is implemented more efficiently.
2022-07-17avoid some `Symbol` to `String` conversionsTakayuki Maeda-3/+2
2022-05-20Remove `crate` visibility usage in compilerJacob Pratt-3/+3
2022-03-03Associate multiple with a crate too.Felix S. Klock II-3/+3
2022-03-03refactor: prepare to associate multiple spans with a module.Felix S. Klock II-2/+2
2022-03-02rename ErrorReported -> ErrorGuaranteedmark-3/+3
2022-02-23rustc_errors: let `DiagnosticBuilder::emit` return a "guarantee of emission".Eduard-Mihai Burtescu-3/+3
2022-02-19Adopt let else in more placesest31-17/+14
2021-12-15Remove unnecessary sigils around `Ident::as_str()` calls.Nicholas Nethercote-2/+2
2021-12-15Remove `SymbolStr`.Nicholas Nethercote-4/+6
By changing `as_str()` to take `&self` instead of `self`, we can just return `&str`. We're still lying about lifetimes, but it's a smaller lie than before, where `SymbolStr` contained a (fake) `&'static str`!
2021-09-25Check for macros in built-in attributes that don't support them.Eric Huss-1/+20
2021-08-25Use if-let guards in the codebaseLéo Lanteri Thauvin-6/+5
2021-05-06E0583: Include secondary path in error messageDeadbeef-4/+5
2021-05-03use full path for E0761hi-rustin-7/+5
2021-05-03use double quotes for E0761hi-rustin-1/+1
2021-03-14expand: Resolve and expand inner attributes on out-of-line modulesVadim Petrochenkov-19/+39
2021-03-05expand: Introduce enum for module loading errors and make module loading ↵Vadim Petrochenkov-87/+85
speculative
2021-03-05expand: Share some code between inline and out-of-line module treatmentVadim Petrochenkov-18/+18
2021-03-05expand: Some more consistent naming in module loadingVadim Petrochenkov-43/+43
2021-03-05expand: Less path cloning during module loadingVadim Petrochenkov-23/+29
2021-03-05expand: Determine module directory path directly instead of relying on spanVadim Petrochenkov-8/+3
2021-03-05expand: Move module file path stack from global session to expansion dataVadim Petrochenkov-13/+9
Also don't push the paths on the stack directly in `fn parse_external_mod`, return them instead.
2021-03-05expand: Remove obsolete `DirectoryOwnership::UnownedViaMod`Vadim Petrochenkov-52/+9
This ownership kind is only constructed in the case of path attributes like `#[path = ".."]` without a file name segment, which always represent some kind of directories and will produce and error on attempt to parse them as a module file.
2021-02-18ast: Keep expansion status for out-of-line module itemsVadim Petrochenkov-13/+10
Also remove `ast::Mod` which is mostly redundant now
2021-02-18ast: Stop using `Mod` in `Crate`Vadim Petrochenkov-3/+2
Crate root is sufficiently different from `mod` items, at least at syntactic level. Also remove customization point for "`mod` item or crate root" from AST visitors.
2020-09-10Syntactically permit unsafety on modsDavid Tolnay-4/+8
2020-09-04rustc_expand: remove pub visibility for rustfmtCaleb Cartwright-2/+1
2020-08-30mv compiler to compiler/mark-0/+312