Implment #[cfg] and #[cfg_attr] in where clauses

This commit is contained in:
Frank King 2025-02-05 18:58:29 +08:00
parent 30508faeb3
commit 42f51d4fd4
38 changed files with 3296 additions and 88 deletions

View file

@ -779,6 +779,10 @@ passes_unstable_attr_for_already_stable_feature =
.item = the stability attribute annotates this item
.help = consider removing the attribute
passes_unsupported_attributes_in_where =
most attributes are not supported in `where` clauses
.help = only `#[cfg]` and `#[cfg_attr]` are supported
passes_unused =
unused attribute
.suggestion = remove this attribute

View file

@ -919,7 +919,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| Target::Arm
| Target::ForeignMod
| Target::Closure
| Target::Impl => Some(target.name()),
| Target::Impl
| Target::WherePredicate => Some(target.name()),
Target::ExternCrate
| Target::Use
| Target::Static
@ -2614,6 +2615,32 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
intravisit::walk_item(self, item)
}
fn visit_where_predicate(&mut self, where_predicate: &'tcx hir::WherePredicate<'tcx>) {
// FIXME(where_clause_attrs): Currently, as the following check shows,
// only `#[cfg]` and `#[cfg_attr]` are allowed, but it should be removed
// if we allow more attributes (e.g., tool attributes and `allow/deny/warn`)
// in where clauses. After that, only `self.check_attributes` should be enough.
const ATTRS_ALLOWED: &[Symbol] = &[sym::cfg, sym::cfg_attr];
let spans = self
.tcx
.hir()
.attrs(where_predicate.hir_id)
.iter()
.filter(|attr| !ATTRS_ALLOWED.iter().any(|&sym| attr.has_name(sym)))
.map(|attr| attr.span())
.collect::<Vec<_>>();
if !spans.is_empty() {
self.tcx.dcx().emit_err(errors::UnsupportedAttributesInWhere { span: spans.into() });
}
self.check_attributes(
where_predicate.hir_id,
where_predicate.span,
Target::WherePredicate,
None,
);
intravisit::walk_where_predicate(self, where_predicate)
}
fn visit_generic_param(&mut self, generic_param: &'tcx hir::GenericParam<'tcx>) {
let target = Target::from_generic_param(generic_param);
self.check_attributes(generic_param.hir_id, generic_param.span, target, None);

View file

@ -1909,3 +1909,11 @@ pub(crate) struct RustcConstStableIndirectPairing {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(passes_unsupported_attributes_in_where)]
#[help]
pub(crate) struct UnsupportedAttributesInWhere {
#[primary_span]
pub span: MultiSpan,
}