about summary refs log tree commit diff
path: root/compiler/rustc_lint/src/non_local_def.rs
AgeCommit message (Collapse)AuthorLines
2025-09-21Port #[macro_export] to the new attribute parsing infrastructureJonathan Brouwer-3/+7
Co-authored-by: Anne Stijns <anstijns@gmail.com>
2025-08-13Rollup merge of #145153 - joshtriplett:macro-kinds-plural, r=petrochenkovGuillaume Gomez-2/+2
Handle macros with multiple kinds, and improve errors (I recommend reviewing this commit-by-commit.) Switch to a bitflags `MacroKinds` to support macros with more than one kind Review everything that uses `MacroKind`, and switch anything that could refer to more than one kind to use `MacroKinds`. Add a new `SyntaxExtensionKind::MacroRules` for `macro_rules!` macros, using the concrete `MacroRulesMacroExpander` type, and have it track which kinds it can handle. Eliminate the separate optional `attr_ext`, now that a `SyntaxExtension` can handle multiple macro kinds. This also avoids the need to downcast when calling methods on `MacroRulesMacroExpander`, such as `get_unused_rule`. Integrate macro kind checking into name resolution's `sub_namespace_match`, so that we only find a macro if it's the right type, and eliminate the special-case hack for attributes. This allows detecting and report macro kind mismatches early, and more precisely, improving various error messages. In particular, this eliminates the case in `failed_to_match_macro` to check for a function-like invocation of a macro with no function-like rules. Instead, macro kind mismatches now result in an unresolved macro, and we detect this case in `unresolved_macro_suggestions`, which now carefully distinguishes between a kind mismatch and other errors. This also handles cases of forward-referenced attributes and cyclic attributes. ---- In this PR, I've minimally fixed up `rustdoc` so that it compiles and passes tests. This is just the minimal necessary fixes to handle the switch to `MacroKinds`, and it only works for macros that don't actually have multiple kinds. This will panic (with a `todo!`) if it encounters a macro with multiple kinds. rustdoc needs further fixes to handle macros with multiple kinds, and to handle attributes and derive macros that aren't proc macros. I'd appreciate some help from a rustdoc expert on that. ---- r? ````````@petrochenkov````````
2025-08-12Switch to a bitflags `MacroKinds` to support macros with more than one kindJosh Triplett-2/+2
Review everything that uses `MacroKind`, and switch anything that could refer to more than one kind to use `MacroKinds`. Add a new `SyntaxExtensionKind::MacroRules` for `macro_rules!` macros, using the concrete `MacroRulesMacroExpander` type, and have it track which kinds it can handle. Eliminate the separate optional `attr_ext`, now that a `SyntaxExtension` can handle multiple macro kinds. This also avoids the need to downcast when calling methods on `MacroRulesMacroExpander`, such as `get_unused_rule`. Integrate macro kind checking into name resolution's `sub_namespace_match`, so that we only find a macro if it's the right type, and eliminate the special-case hack for attributes.
2025-08-11Propagate TraitImplHeader to hirCameron Steffen-2/+2
2025-05-30Reorder fields in `hir::ItemKind` variants.Nicholas Nethercote-1/+1
Specifically `TyAlias`, `Enum`, `Struct`, `Union`. So the fields match the textual order in the source code. The interesting part of the change is in `compiler/rustc_hir/src/hir.rs`. The rest is extremely mechanical refactoring.
2025-03-26Add environment variable tracking in places where it was convenientMads Marquart-2/+4
This won't work with Cargo's change tracking, but it should work with incremental.
2025-03-18Move `hir::Item::ident` into `hir::ItemKind`.Nicholas Nethercote-3/+3
`hir::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, Trait`, TraitAalis`. - It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`, `Impl`. - For `Use`, it is non-empty for `UseKind::Single` and empty for `UseKind::{Glob,ListStem}`. All of this is quite non-obvious; the only documentation is a single comment saying "The name might be a dummy name in case of anonymous items". Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out. The commit is large but it's mostly obvious plumbing work. Some notable things. - A similar transformation makes sense for `ast::Item`, but this is already a big change. That can be done later. - Lots of assertions are added to item lowering to ensure that identifiers are empty/non-empty as expected. These will be removable when `ast::Item` is done later. - `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does. - `lower_use_tree` is significantly simpler. No more confusing `&mut Ident` to deal with. - `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's used with `unwrap` in a few places; sometimes it's hard to tell exactly which item kinds might occur. None of these unwraps fail on the test suite. It's conceivable that some might fail on alternative input. We can deal with those if/when they happen. - In `trait_path` the `find_map`/`if let` is replaced with a loop, and things end up much clearer that way. - `named_span` no longer checks for an empty name; instead the call site now checks for a missing identifier if necessary. - `maybe_inline_local` doesn't need the `glob` argument, it can be computed in-function from the `renamed` argument. - `arbitrary_source_item_ordering::check_mod` had a big `if` statement that was just getting the ident from the item kinds that had one. It could be mostly replaced by a single call to the new `ItemKind::ident` method. - `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size, and that's what matters, because `ItemKind` only occurs within `Item`.
2025-02-08Rustfmtbjorn3-11/+15
2025-01-27Remove redundant to_ident_string callsMichael Goulet-1/+1
2025-01-23`visit_x_unambig`Boxy-1/+1
2025-01-23Split hir `TyKind` and `ConstArgKind` in two and update `hir::Visitor`Boxy-2/+2
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-11-01Also treat `impl` definition parent as transparent regarding modulesUrgau-2/+8
2024-10-13Also use outermost const-anon for impl items in `non_local_defs` lintUrgau-3/+7
2024-10-11Consider outermost const-anon in non_local_def lintUrgau-31/+59
2024-09-23Remove with/without trait and bounds considerationUrgau-1/+0
2024-09-23Point to every relevant types in the main diagUrgau-55/+17
2024-09-23Rework `non_local_definitions` lint to only be a syntactic heuristicUrgau-215/+24
2024-09-23Revert "Switch back `non_local_definitions` lint to allow-by-default"Urgau-1/+1
This reverts commit 0c0dfb88eeefbbaa4c10cfa4a7f0e16541e086eb.
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-23/+19
2024-09-02chore: Fix typos in 'compiler' (batch 1)Alexander Cyon-1/+1
2024-08-27Add `warn(unreachable_pub)` to `rustc_lint`.Nicholas Nethercote-1/+1
2024-07-29Reformat `use` declarations.Nicholas Nethercote-9/+8
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-25Support ?Trait bounds in supertraits and dyn Trait under a feature gateBryanskiy-2/+2
2024-07-08Move trait selection error reporting to its own top-level moduleMichael Goulet-2/+2
2024-06-27Switch back `non_local_definitions` lint to allow-by-defaultUrgau-1/+1
as request T-lang is requesting some major changes in the lint inner workings in #126768#issuecomment-2192634762
2024-06-21Rename a bunch of thingsMichael Goulet-1/+1
2024-06-19Rollup merge of #126422 - Urgau:doctest-impl-non-local-def, r=fmease许杰友 Jieyou Xu (Joe)-38/+45
Suggest using a standalone doctest for non-local impl defs This PR tweaks the lint output of the `non_local_definitions` lint to suggest using a standalone doctest instead of a moving the `impl` def to an impossible place as was already done with `macro_rules!` case in https://github.com/rust-lang/rust/pull/124568. Fixes #126339 r? ```@fmease```
2024-06-15Also simplify macro_rules doctest codeUrgau-2/+1
2024-06-15Suggest standalone doctest for non-local impl defsUrgau-38/+46
2024-06-15Auto merge of #125722 - Urgau:non_local_defs-macro-to-change, r=estebankbors-0/+8
Indicate in `non_local_defs` lint that the macro needs to change This PR adds a note to indicate that the macro needs to change in the `non_local_definitions` lint output. Address https://github.com/rust-lang/rust/pull/125089#discussion_r1616311862 Fixes #125681 r? `@estebank`
2024-06-03Fix up comments.Nicholas Nethercote-1/+1
Wrap overly long ones, etc.
2024-05-29non_local_defs: indicate that the macro needs to changeUrgau-0/+8
aaa
2024-05-29Don't require `visit_body` to take a lifetime that must outlive the function ↵Oli Scherer-2/+2
call
2024-05-27non_local_defs: point the parent item when appropriateUrgau-5/+17
2024-05-27non_local_defs: point to Self and Trait to give more contextUrgau-7/+76
2024-05-27non_local_defs: suggest removing leading ref/ptr to make the impl localUrgau-29/+51
2024-05-27non_local_defs: be more precise about what needs to be movedUrgau-2/+38
2024-05-27non_local_defs: use labels to indicate what may need to be movedUrgau-0/+3
2024-05-27non_local_defs: use span of the impl def and not the impl blockUrgau-1/+1
2024-05-27non_local_defs: improve some notes around trait, bounds, constsUrgau-3/+6
- Restrict const-anon exception diag to relevant places - Invoke bounds (and type-inference) in non_local_defs - Specialize diagnostic for impl without Trait
2024-05-27non_local_defs: move out from `#[derive(LintDiagnostic)]` to manual implUrgau-1/+0
2024-05-26Give EarlyBinder a tcx parameterMichael Goulet-1/+1
We are gonna need it to uplift EarlyBinder
2024-05-18Fix typos (taking into account review comments)blyxyas-1/+1
2024-05-10Use fewer origins when creating type variables.Nicholas Nethercote-2/+1
`InferCtxt::next_{ty,const}_var*` all take an origin, but the `param_def_id` is almost always `None`. This commit changes them to just take a `Span` and build the origin within the method, and adds new methods for the rare cases where `param_def_id` might not be `None`. This avoids a lot of tedious origin building. Specifically: - next_ty_var{,_id_in_universe,_in_universe}: now take `Span` instead of `TypeVariableOrigin` - next_ty_var_with_origin: added - next_const_var{,_in_universe}: takes Span instead of ConstVariableOrigin - next_const_var_with_origin: added - next_region_var, next_region_var_in_universe: these are unchanged, still take RegionVariableOrigin The API inconsistency (ty/const vs region) seems worth it for the large conciseness improvements.
2024-05-02Rollup merge of #124568 - Urgau:non-local-defs-doctest, ↵Guillaume Gomez-0/+9
r=michaelwoerister,GuillaumeGomez Adjust `#[macro_export]`/doctest help suggestion for non_local_defs lint This PR adjust the help suggestion of the `non_local_definitions` lint when encountering a `#[macro_export]` at top-level doctest. So instead of a non-sentential help suggestion to move the `macro_rules!` up above the `rustdoc`-generated function. We now suggest users to declare their own function. Fixes *(partially, needs backport)* #124534
2024-05-01Adjust `#[macro_export]`/doctest help suggestion for non_local_defs lintUrgau-0/+9
2024-05-01Auto merge of #124539 - Urgau:non-local-defs_modulo_modules, r=lcnrbors-5/+11
Consider inner modules to be local in the `non_local_definitions` lint This PR implements the [proposed fix](https://github.com/rust-lang/rust/issues/124396#issuecomment-2079553642) for #124396, that is to consider inner modules to be local in the `non_local_definitions` lint. This PR is voluntarily kept as minimal as possible so it can be backported easily. T-lang [nomination](https://github.com/rust-lang/rust/issues/124396#issuecomment-2079692820) will need to be removed before this can be merged. Fixes *(nearly, needs backport)* https://github.com/rust-lang/rust/issues/124396
2024-04-29Consider inner modules to be local in the `non_local_definitions` lintUrgau-5/+11
2024-04-29Remove `extern crate rustc_session` from `rustc_lint`.Nicholas Nethercote-0/+1