Also warn if an invalid `doc` attribute is used on a macro invocation
Fixesrust-lang/rust#160880.
So about this: I think the error message is good enough, but I can specialize it in case it's not a doc comment but a doc attribute.
Also: currently the ignored attributes are not kept in the AST, so when we reach `rustc_attr_parsing`, the attribute is not present and there won't trigger an error. I don't think it's worth changing this logic but in case someone thinks otherwise, it's mentioned. :)
r? @JonathanBrouwer
fix macro attribute feature-gate span
Fixesrust-lang/rust#147135.
Issue rust-lang/rust#147135 reports a misleading feature-gate diagnostic for cross-crate `macro_rules!` macro attributes. This misleading feature-gate diagnostic causes rustc to point at the macro definition instead of user’s attribute call site.
My PR makes sure externally defined `macro_rules!` attributes are gated at the invocation span. Regression test verifies that the primary error points at the downstream `#[foo::identity]` attribute use
This allows implementing EIIs that don't have a default impl without the
involvement of symbol aliases by simply changing the mangled symbol name
of the implementation, which would make them trivially compatible with
all backends and targets. This change will be left to a followup PR.
Split register_tool into register_attribute_tool and register_lint_tool
Tracking issue: rust-lang/rust#66079
This implements the attribute parsing part of RFC 3808. The resolution part is yet to be implemented.
cc @jyn514
This splits the functionality to two parts per RFC. The `#![register_tool]`
attribute registers the tool in both ways.
Semantics remain unchanged and will be adjusted in later commit.
Update thinvec to 0.2.19
It contains `may_dangle` Drop impl under a feature flag. This allows us to use it in a lot more places, notable in types that hold `&'tcx` references. This would previously fail in borrowck in arena code.
We have to enable its "unstable" feature for this. That also enables `TrustedLen` impl, which is behind unstable flag, too. I left the flag disabled, because it regressed next-solver benchmarks, but that will be fixed in PR that needs this flag (see the comment: https://github.com/rust-lang/rust/pull/159974#issuecomment-5091132427)
The existing API is preserved as `parse_limited_sym` as a simple helper.
Similarly, `parse_limited_should_emit` is preserved as
`parse_limited_sym_should_emit`. I've noted that all users targets crate,
so the `target_node_id` and `target` parameters are removed from it.
rustc_parse: Stop returning `Option` from statement parsing
`parse_stmt_without_recovery` had one corner case in which it returned `Ok(None)` - when parsing immediately encountered a closing brace `}`, possibly after parsing outer attributes.
It is simpler to never call `parse_stmt_without_recovery` in such contexts than deal with a possibility of no statement being returned without an error.
So this PR changes the function's return type from `PResult<'a, Option<Stmt>>` to `PResult<'a, Stmt>` and adjusts one call site to check for a closing brace.
Split non-local `semicolon_in_expressions_from_macros` into a separate lint
The new handling for non-local macros in
`semicolon_in_expressions_from_macros` produces new deny-by-default
lints on various existing crates, which have not previously received
warnings because prior Rust versions suppressed them. Split those out
into a new `semicolon_in_expressions_from_non_local_macros` lint, and
make that one warn-by-default (and FCW warn-in-deps), rather than
deny-by-default, to give the ecosystem time to adapt.
This reintroduces the `is_local` tracking from commit
9192337331.
Improve consistency of attribute error messages
This PR aims to improve how attributes are referred to in error messages. A lot of test outputs will change, so there will be follow-ups, but this PR has enough changes to cover a lot of cases and give a good feel for where things are headed.
r? @estebank
[rustdoc] Retrieve `cfg_attr` information for derived impls for `doc_cfg` feature
Fixes https://github.com/rust-lang/rust/issues/103300.
After long last, this PR finally allows the derive proc-macro `cfg_attr` cfg predicates information to be kept so rustdoc can use it for its `doc_cfg` feature (cc https://github.com/rust-lang/rust/issues/43781).
It works as follows: for `impl` generated by macro expansion, we then look for the attributes on the type for which the `impl` block is implemented. Then, to know which `cfg_attr` attribute we want to look at, we use `expn_that_defined` on the impl's `DefId` which returns the `Span` where the macro was expanded. If it's part of a `cfg_attr`, then the `cfg_attr`'s `Span` will contain the derive's, so from then, we just need to add the `cfg` information.
This PR also adds the cfg predicates into the `AttributeKind::CfgAttrTrace` variant so it can be reused by rustdoc (hence the `compiler/*` changes).
Thanks a lot @petrochenkov for the pointers here!
r? @petrochenkov
Remove outdated workaround for `P` type
P is long gone, so this workaround is no longer necessary.
The workaround took attrs out of node, inspected and possibly modified it and then put it back at the end of each branch of the following match.
Move `Limit` out of `rustc_hir`
It feels rather out of place in `rustc_hir`, so this PR moves it to `rustc_data_structures`. Part of hopefully moving attributes from `rustc_hir` to their own crate.
~~This also lets `rustc_type_ir` use it~~ actually rustc_type_ir needs to compile on stable and `rustc_data_structures` uses like 30 features 😢
The new handling for non-local macros in
`semicolon_in_expressions_from_macros` produces new deny-by-default
lints on various existing crates, which have not previously received
warnings because prior Rust versions suppressed them. Split those out
into a new `semicolon_in_expressions_from_non_local_macros` lint, and
make that one warn-by-default (and FCW warn-in-deps), rather than
deny-by-default, to give the ecosystem time to adapt.
This reintroduces the `is_local` tracking from commit
9192337331.
No matter how you say `#![cfg_attr]` -- "hash...", "pound...",
"cfg_attr..." -- it doesn't start with a vowel sound and therefore
should be preceded with "a" rather than "an".
proc_macro: preserve file module spans for inner attrs
fixesrust-lang/rust#157881
out-of-line mods with custom inner attrs were sending proc macros a pretty-printed module wrapper. the awkward bit: tokens inside the module picked up the span from the parent mod foo;, so later diags pointed at the decl instead of the file where the code actually came from.
this keeps the wrapper synthetic, but puts the loaded file body back into the token stream with its own spans. it also skips the attr currently being expanded, so we don't replay it by accident. added a small ui repro too, plus the older span-debug output changed because those spans are now real.
imo this is the least annoying fix for now. fwiw, it keeps the old fake-wrapper path instead of trying to make full token capture for file mods happen in this PR.
They are meant to be specialized methods for particular cases, not
general-purpose construction methods. They can be replaced with vanilla
`Vec<TokenTree>` construction, which this commit does.
Currently the method computes the span from the path's span and the
args' span. That used to be fine but then `unsafe(..)`/`safe(..)` were
added without it being changed. So it doesn't account for those optional
keywords and parentheses. The only way to handle those reliably is with
a dedicated span for the entire `AttrItem`.
Consequences:
- `NormalAttr` grows from 72 bytes to 80. (It's the only AST node that
contains an `AttrItem`.) Not ideal, but non-doc-comment attributes
aren't that common (not compared to expressions, for example). Also,
`NormalAttr` is always boxed and jemalloc will round up 72 byte
allocations to 80 bytes anyway.
- In `lint-unsafe-code.rs` various spans now point to `unsafe(foo)`
rather than just `foo`. (It makes sense to include the `unsafe` keyword
in the complaint about unsafety.)
AST attributes use "early parsed"/"parsed" terminology to refer to the
`CfgTrace` and `CfgAttrTrace` attributes. I think this terminology is
meant to echo the terminology used for HIR attributes, i.e.
`hir::Attribute::{Parsed,Unparsed}`, probably because
`hir::Attribute::Parsed` is used for attributes that aren't stored in a
token-based form.
But this naming is misleading. "Early parsed" attributes aren't parsed
at all because they are inserted by the compiler and cannot be written
in source code. There are also two comments that claim that these
attributes are kept in parsed form "so they don't have to be reparsed
every time they're used, for performance", which is simply incorrect.
This commit renames these as "synthetic" attributes, which better
reflects their nature. The commit also fixes the incorrect comments.
Note that `is_parsed_attribute` is unchanged, because it refers to the
HIR attribute meaning. (And the removal of the synthetic attributes from
it in the previous commit is now more obviously correct.)
This commit pushes further than the previous one, relying more heavily
on looking directly at `AttrKind::Parsed` to handle various cases.
Note that the removed clippy code was actually dead.
`AttrItem` currently contains an `AttrItemKind` which is either `Parsed`
or `Unparsed`. In the `Parsed` case the surrounding `AttrItem` is
basically fake, with a meaningless `unsafety` field, a synthetic
(non-ident) symbol, and an empty `tokens`.
This commit moves `Parsed` up one level to become a new variant of
`AttrKind`. It no longer needs to pretend to be an `AttrKind::Normal`,
and makes a number of things simpler:
- No `unparsed_ref` calls needed.
- Replaces the `attr_into_trace` and `Attribute::replace_args` mess with
the much simpler `convert_normal_to_parsed`.
The commit converts a numberof non-exhaustive `AttrKind` matches (those
that don't involve matching a single attribute) to exhaustive, for
future-proofing. It uses `unreachable!` on all the `AttrKind::Parsed`
cases that aren't hit by the full test suite.
semicolon_in_expressions_from_macros: Lint on non-local macros too
The `semicolon_in_expressions_from_macros` lint previously suppressed
warnings about non-local macros. This masks a lint that will
subsequently become a hard error.
Stop suppressing it, dropping the `is_local` flag and check.
Fix the test for this case to now expect the error. Drop a separate test
that depended on the lack of error (and that specifically notes it
should be dropped once the lint becomes a hard error).
Rename ModDefId to ModId and use more
Addresses some FIXMEs.
The `ModDefId` was used pretty sparingly, and honestly I wondered if it should exist at all. After making these changes, I think the type is worth it. Most notably it is now used in `Visibility::Restricted`.
I first renamed `ModDefId` to `ModId` since I think the former is over-specific, and also this is more in line with `BodyId` or `OwnerId`.
I'll wait for initial feedback before fixing rustdoc/clippy.