Require a LintId for all proc-macro warnings · Issue #524 · rust-lang/libs-team (original) (raw)

Proposal

Problem statement

This proposal is designed as the minimal LintId integration that unblocks stabilization of a proc-macro warning API (rust-lang/rust#54140). Per rust-lang/rust#54140 (comment), macro-generated warnings should be associated with a namespaced identifier which can be used for setting lint level at the call site, and for rendering rustdoc documentation about the lints emitted by a macro.

Motivating examples or use cases

Solution sketch

Add a proc_macro::LintId type with Copy, Clone, and Debug impl. Add a 4th proc-macro attribute #[proc_macro_warning] for declaring a unique LintId at the crate root of a proc-macro crate. Update/rearrange existing Diagnostic functions in the proc_macro crate to require passing a LintId when creating a warning.

// from the openvm use case:

use proc_macro::{LintId, TokenStream};

#[proc_macro_warning] pub static limbs_too_small: LintId;

#[proc_macro] pub fn moduli_declare(input: TokenStream) -> TokenStream { ... if limbs < 32 { limbs = 32; proc_macro::warning(limbs_too_small, "limbs has been set to 32 because it was too small");

    // or: proc_macro::Diagnostic::warning(limbs_too_small, "`limbs` has been set to 32 because it was too small")
    //         .note("this is going to be changed once we support more flexible reads")
    //         .emit()
}
...

}

Within the same proc macro crate, the static (limbs_too_small) exists in the value namespace. Downstream of the proc macro crate, the same static exists in the macro namespace for the purpose of re-exports. The use of 2 namespaces in such a way is identical to how all proc macro items already work.

Alternatives

The 10 comments under rust-lang/rust#54140 (comment) explore a variety of designs.

Future work

The following enhancements would not need to block stabilization of a diagnostics API in proc_macro once LintId exists.

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

Second, if there's a concrete solution: