-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement #[proc_macro_warning] to generate LintId for macro-generated warnings #135432
base: master
Are you sure you want to change the base?
Conversation
r? @Nadrieril rustbot has assigned @Nadrieril. Use |
Some changes occurred in src/tools/clippy cc @rust-lang/clippy HIR ty lowering was modified cc @fmease |
This comment has been minimized.
This comment has been minimized.
e76936f
to
9cd8b2d
Compare
HIR ty lowering was modified cc @fmease Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
r? compiler |
9cd8b2d
to
3fbd1b4
Compare
rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead. cc @rust-lang/rust-analyzer |
This comment has been minimized.
This comment has been minimized.
3fbd1b4
to
5b19998
Compare
@rustbot ready |
☔ The latest upstream changes (presumably #135896) made this pull request unmergeable. Please resolve the merge conflicts. |
5b19998
to
ab550d9
Compare
This PR unblocks an initial round of stabilizations of #54140 as discussed in #54140 (comment).
An id for a procedural macro warning is declared using
#[proc_macro_warning]
on apub static
contained in the crate root with typeproc_macro::LintId
. The attribute fills in a unique value for the static's value:Within the same proc macro crate, the static (
ambiguous_thing
) exists in the value namespace. It can be used as a value for passing to public APIs provided by theproc_macro
crate. The value's type isproc_macro::LintId
, which implementsCopy
andDebug
.Downstream of the proc macro crate, the same static exists in the macro namespace. It can be re-exported in the macro namespace using
pub use
. Currently it is not useful for anything else.The use of 2 namespaces in such a way is identical to how all proc macros already work. For example, inside a proc macro crate containing
#[proc_macro] pub fn foo(input: TokenStream) -> TokenStream
, this function exists in the value namespace and is callable as a function. In downstream crates, the same function exists in the macro namespace and is callable as a function-like macro.Future work
Some of the public unstable API of
proc_macro
needs to be redesigned to require that aLintId
must always be provided when a macro creates a warning. In this PR, I have made this change only forDiagnostic::span_warning
andDiagnostic::warning
. There is another constructorDiagnostic::new
which takes aproc_macro::Level
and can be passedLevel::Warning
. In this PR I have not touched that function, which means it is not on track for stabilizing. This is fine because it has already fallen out of favor in the tracking issue discussion and was not suggested for stabilization. See for example Tracking Issue: Procedural Macro Diagnostics (RFC 1566) #54140 (comment).Procedural macro
LintId
needs to be integrated into theallow
/warn
/deny
lint level system. If a cratefoo_macros
defines aLintId
calledambiguous_thing
, and it is re-exported in the crate root of a cratefoo
, then users offoo
need to be able to writeallow(foo::ambiguous_thing)
ordeny(foo::ambiguous_thing)
.Procedural macro
LintId
needs to be integrated with theunknown_lints
lint. If a user writesdeny(foo::ambiguous_thing)
whenfoo
is not a proc macro crate declaring aambiguous_thing
lint id, nor is this resolved as a re-export of such a lint id, this needs to triggerunknown_lints
.Rustdoc will need to render documentation for lint ids.
Mechanism for a proc macro crate to set a default level for each of its lints. By default they are warn-by-default, but some lints might be better as allow-by-default or deny-by-default.
A style lint that enforces a case convention for lint ids (snake_case).
Rust-analyzer will want to provide autocomplete for proc macro lint ids inside
allow
/warn
/deny
.Importantly, none of the above blocks stabilization of warning diagnostics APIs in
proc_macro
(as long as we do it carefully, i.e. notDiagnostic::new
).