Improve is_doc_keyword.

This is part of the implementation of `#[doc(keyword = "match")]`
attributes used by `std` to provide documentation for keywords.

`is_doc_keyword` currently does a crude keyword range test that's
intended to catch all keywords but misses `kw::Yeet`. This commit
changes it to use `Symbol` methods, including the new `is_weak` method
(required for `union`). `Symbol` methods are much less prone to falling
out of date if new keywords are added.
This commit is contained in:
Nicholas Nethercote 2025-03-26 13:33:38 +11:00
parent 9fb0defa52
commit 929749d801
2 changed files with 7 additions and 3 deletions

View file

@ -35,7 +35,7 @@ use rustc_session::lint::builtin::{
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
}; };
use rustc_session::parse::feature_err; use rustc_session::parse::feature_err;
use rustc_span::{BytePos, DUMMY_SP, Span, Symbol, kw, sym}; use rustc_span::{BytePos, DUMMY_SP, Span, Symbol, edition, kw, sym};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs}; use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
use rustc_trait_selection::traits::ObligationCtxt; use rustc_trait_selection::traits::ObligationCtxt;
@ -1038,7 +1038,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
// FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we // FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we
// can remove the `SelfTy` case here, remove `sym::SelfTy`, and update the // can remove the `SelfTy` case here, remove `sym::SelfTy`, and update the
// `#[doc(keyword = "SelfTy")` attribute in `library/std/src/keyword_docs.rs`. // `#[doc(keyword = "SelfTy")` attribute in `library/std/src/keyword_docs.rs`.
s <= kw::Union || s == sym::SelfTy s.is_reserved(|| edition::LATEST_STABLE_EDITION) || s.is_weak() || s == sym::SelfTy
} }
let doc_keyword = match meta.value_str() { let doc_keyword = match meta.value_str() {

View file

@ -131,7 +131,7 @@ symbols! {
// tidy-alphabetical-end // tidy-alphabetical-end
// Weak keywords, have special meaning only in specific contexts. // Weak keywords, have special meaning only in specific contexts.
// Matching predicates: none // Matching predicates: `is_weak`
// tidy-alphabetical-start // tidy-alphabetical-start
Auto: "auto", Auto: "auto",
Builtin: "builtin", Builtin: "builtin",
@ -2725,6 +2725,10 @@ impl Symbol {
|| self.is_unused_keyword_conditional(edition) || self.is_unused_keyword_conditional(edition)
} }
pub fn is_weak(self) -> bool {
self >= kw::Auto && self <= kw::Yeet
}
/// A keyword or reserved identifier that can be used as a path segment. /// A keyword or reserved identifier that can be used as a path segment.
pub fn is_path_segment_keyword(self) -> bool { pub fn is_path_segment_keyword(self) -> bool {
self == kw::Super self == kw::Super